Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, October 10, 2020

C nested if...else statement

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.

Nested if...else | welcome2protec.com


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.


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."








Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us