Nested if or nested if...else: Having more than one if...else statement inside the body of another if...else statement is called nested if...else statement.
- Simple if statement in C programming
- if...else and ladder if...else statement in C programming
Already discussed Read more about-
Syntax
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Example 1: Nested if...else
Having more than one if...else statement inside the body of another if...else statement is called nested if...else.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) { //Nested if...else
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers:12 10
Result:12 > 10
Enter two integers: 10 100
Result: 10 < 100
Example 2: Nested if...else
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output
Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1
let's see another example to find maximum between given three numbers.
Example 3: Nested if...else
/* C program to find maximum between three numbers */
#include
int main()
{
/* Declare three integer variables */
int num1, num2, num3;
/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
/* If num1>num2 and num1>num3 */
printf("Num1 is max.");
}
else
{
/* If num1>num2 but num1 num3)
{
/* If num1num3 */
printf("Num2 is max.");
}
else
{
/* If num1
Output
Enter three numbers: 10
20
30
Num3 is max.
Now let's understand the working flow of above program.
- Suppose user inputs three numbers as num1=10, num3=20 and num3=30.
- The first outer if condition if(num1 > num2) is false since 10 > 20 is false. Hence, outer if statement is skipped, executing the outer else part.
- Inside the outer else, condition if(num2 > num3) is also false, since 20 > 30 is false. Hence, the inner if statement is skipped, executing inner else part.
- Inside the inner else there is nothing much to do. Just a simple printf() statement, printing "Num3 is max."
- if...else programming exercises in C - Practice exercise
Before moving to next tutorial, must try some exercises based on if...else statement.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.