If block

Nested if statement in C programming language

Nested if statement in C programming language

We will learn about Nested if statement in C programming language

In C Programming Language, the nested if-else statement is one if-else statement is used inside another if- else statement.

Nested if statements in C language

Declaration

The syntax of Nested if in C Language

if(condition1)  //outer if
{//executes when condition1 is true
             if(condition1)// inner if
                      {  //executes when condition2 is true
                     }
}

Explanation of nested if with else statements

if(Test_expression1)
{   //outer if
    statement(s);
    //when the test_expression1 is true
    //executes this statements
    if(Test_expression2)
      {   //inner if
    statement(s);
    //when the test_expression2 is true
    //executes this statements
       }
       else{
        statement(s);
        //when the test_expression2 is false
        //executes this statements
    }
}
else{
        statement(s);
        //when the test_expression1 is false
        //executes this statements
    }

 

Explain the nested if – else loop

Nested if statements

 

Flow diagram of nested if  else in C Language

flow diagram of if – else

How works nested if statements

In nested if statements, initially, the test expression of outer if the loop is evaluated.When the condition of outer if becomes true, the if part statement is executed and the output displayed. Then, the flow of control jumps to the inner– if loop and evaluates test expression of inner if

the test expression of inner if the loop is evaluated. when the condition of inner if becomes true, the statements of if is executed.  When the condition is false, else part is executed then else statement is displayed. Then nested if loop may be an exit or terminated.

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Enter your age Here\n");
    scanf("%d",&age); //get input

    if(age<13){ // if statement
    printf("you are a child!\n");
    } else 
    if(age<35){  //elseif statement
    printf("you are a younger!\n");
    }
    else{  //else statement
    printf("you are a older!\n");
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter your age
23
you are a younger

 

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c;
printf("Enter the first number for a..:\n");
scanf("%d",&a);
printf("Enter the second number for b..:\n");
scanf("%d",&b);
printf("Enter the third number for c..:\n");
scanf("%d",&c);

if(a>b){
    if(a>c){
      printf("%d is a large number\n",a);
}
else{
    printf("%d is less than number %d and  %d\n",a,c);
}

}
else{
    printf("%d is a less than number %d\n",a,b);
}
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following results

Enter the first number a..
1
Enter the second number b..
2
Enter the third number c..
3
1 is less than 2

 

 

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    float Zscore;
    printf("Enter your age\n");
    scanf("%d",&age);
    printf("Enter your Zscore\n");
    scanf("%f",&Zscore);

    if(age>18){//outer if
        printf("eligible for university\n");
          if(Zscore>1.0){ //inner if
           printf("qualified for university\n");
    }
          else{  //inner else
            printf("Not qualified for university\n");
         }
     }
    else{ // outer else
        printf("Not eligible for university\n");
    }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following results

Enter your age
19
Enter your Zscore
1.01
eligible for university
qualified for university

 

 

Suggested for you

If statement in Java language

If statement in C language

If statement in C++ language

If statement in Python language

        

The if keyword in Java programming language
if statement in Python programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago