-->

Facebook

Jasper Roberts - Blog

Wednesday, October 1, 2014

Nested if-else statement:

-   The if-else statement provides two way decisions only.

-   Sometimes, decisions must be made between more than two possibilities.
  
-   At that time, we can use nested-if else statement.

-   C language provides nested-if else. Nested if else means loop within loop.

-   In, nested if else statement, if the condition is true than statement(s)-1 (or statement(s)-2) is executed.

-   If the condition is false then statement(s)-3 (or statement(s)-4) is executed.




-   Syntax:
               if(condition)
               {
                   if(condition)
                   {
                          Statement(s)-1;
                   }
                   else
                   {
                        Statement(s)-2;
                }
              else
              {
                   if(condition)
                   {
                        Statement(s)-3;
                   }
                   else
                   {
                        Statement(s)-4;
                   }
              }


- Example: A C program to find out the largest number among three numbers.

#include<stdio.h>
#include<conio.h>

void main()
{
      int a,b,c;
      clrscr();

      printf("Enter the values of a: ");
      scanf("%d",&a);
     
 printf("Enter the values of b: ");
      scanf("%d",&b);
     
 printf("Enter the values of c: ");
      scanf("%d",&c);

     if(a>b)
     {
             if(a>c)
             {
                       printf(" a is largest number.");
             }
             else
            {
                        printf(" c is largest number.");
            }
     }
    
    else
    {
            if(b>c)
           {
                       printf(" b is largest number.");
           }
           else
           {
                       printf(" c is largest number.");
           }
    }

getch();

}

OUTPUT:

Enter the values of a:15
Enter the values of b:25
Enter the values of c:45
c is largest number.



DOWNLOAD          DOWNLOAD
download nested if else statement            download nested if else statement

if else statement

-   In, if else statement, if the condition is true than statement(s)-1 is executed.

-   If the condition is false then statement(s)-2 is executed.




-   Syntax:
               if(condition)
               {
                          Statement(s)-1;
                }
              else
              {
                        Statement(s)-2;

              }

-   Flow chart:


if else statement
if else statement


- Example: A C program to check the student is pass or fail.

#include<conio.h>
#include<stdio.h>

void main()
{
     float per;
     clrscr();
    
     printf(“enter the percentage:”);              
     scanf(“%f”,&per);

     if(per>35)
     {
          printf(“Student is pass.”);
     }
     else
     {
          Printf(“Student is fail.”);
     }
    
     getch();
}


OUTPUT:
enter the percentage: 28
Student is fail.


DOWNLOAD          DOWNLOAD
           




if statement

if statement(s):

- in if statement, if the condition is true than it will execute the block of statements.

- if the condition is false then it will not execute the block of statement.




-   Syntax:
               if(condition)
               {
                          statement(s);
                }




-   Flow chart:
if statement
if statement




   - Example: A C program to check the student is pass.

#include<conio.h>
#include<stdio.h>

void main()
{
     float per;
     clrscr();
    
     printf(“enter the percentage:”);              
     scanf(“%f”,&per);

     if(per>35)
     {
          printf(“Student is pass.”);
     }
    
     getch();
}


OUTPUT:
enter the percentage: 65
Student is pass.


DOWNLOAD          DOWNLOAD