Branching statements

Branchinng statements in C

A branching statements is also known as selection of control statements.
C languagec executes the program statements in a sequence,branching statements are used to the alter the flow of the sequence of statements. branching statements also known as control statements. prgrammer can jump from one part of the program to other with the help of branching statements.

Branching statements are as follows:
  1. if statement
  2. if else statement
  3. nested if
  4. if else ladder
  5. switch case

1.  if statement

syntax of if statement:
            if (condition) 
            { 
            //body of if statement 
            statements;
            } 
        

in "if" statement, respective block of code is executed when condition is true.

Sample program
Enter any two numbers and check which number is maximum between these two.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
         void main()
         {
           int a, b;
           printf(“Enter the frist number\n”);
           scanf(“%d ”,&a);
           printf(“Enter the second number\n”);
           scanf(“%d ”,&b);

          if(a>b)
           {
             printf(“a is max and value of max is %d”, a);
           }
          else
           {
             printf(“b is max and value of max is %d”, b);
           }
          getch();
        }
         
     

Output-

         Enter the frist numbers
         14
         Enter the second number
         27
         b is max and value of max is 27
                
                

2.  if else statement

syntax of if else statement:
          if (condition) 
          { 
            //body of if statement 
            statements;
          } 
          else 
          { 
            //body of else statement   
          } 
          

in "if else " statement, if block of code is executed when condition is true, otherwise else block of code executed.

Sample program
Check wheather the entered number is odd or even.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
                
                        
         void main()
         {
           int n;
           printf(“Enter the number\n”);
           scanf(“%d ”,&n);

          if(n%2==0)
           {
             printf(“Entered number is even”);
           }
          else
           {
             printf(“Entered number is odd”);
           }
          getch();
        }
         
     

Output-

         Enter the  number
         14
         Entered number is even
                  
                

3. nested if else statement

syntax of nested if else :
            if(condition1) 
            {
            //body of else Statements; 
                       
              if(condition2) 
              { 
                if(condition3) 
                { 
                //body of if Statements; 
                } 
                else 
                { 
                  Statements; 
                } 
              else 
              {	
                //body of else Statements; 
              } 
            else 
            {   
              //body of else Statements; 
            } 
        

In “nested if else ” statement, if condition 1 is false, then condition 2 is checked and if condition 2 is true then statements of that respective if bock are executed.
If condition 2 also gets false, then else part is executed.

Sample program
Write a program to find the type of a triangle.
     
              
        #include<stdio.h> 
        #include<conio.h> 
     
                
     
        void main()
        {
         float  a, b, c;
         clrscr();
         printf(“Enter the three sides of a triangle\n”);
         scanf(“ %f %f %f”, &a, &b, &c);
         printf(“ The  given sides  are  \n”);
         printf(“% 8.2f  %8.2f   %8.2f  \n”, a, b, c);
         if(((a+b) > c)  &&  ((a+c) > b)  && ((b+c) > a))
          {
            if(((a==b) && (a==c))
             {
              printf(“Equilateral  triangle  \n”);
             }
            else 
             {
               if((a==b)   !!  (a==c)  !!  (b==c))
               {
                 printf(“Isosceles  triangle  \n  ”);
               }
               else
               {
                printf(“ Simple  triangle  \n”);
               }
            }
         }
        else
         {
           printf(“Triangle is not possible”);
         }
        getch();
        }
            
     

Output-

        Enter the three sides of a triangle
        3.0
        3.0
        3.0
        The given sides are 3.0  3.0  3.0
        Equilateral  triangle  

                

4.  if else if ladder

syntax of if else if ladder statement:
        if (condition1) 
        {	
        Statement1;
        }
        else if (condition2) 
        {
        Statement2;
        }
        else if (condition3) 
        {
        Statement3; 
        }
        .
        .
        else    
        {
        Default Statement; 
        }
        

In “if else if ladder” every if statement having condition, if that condition get executed and result of condition is true then body of respective if block get executed and runtime cursor get of that if else if ladder and next statement after that ladder get executed.

Sample program
Implement a program to print the week day using if else if ladder.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
          void main()
          {
          int day ;
          clrscr();
          printf(“Enter the week day number  between 1  to7  \n”);
          scanf(“%d ”,&day);
          if(day==1) {
              printf(“\n Week day  is  Monday”);}
          else if(day==2){
              printf(“\n Week day is Tuesday”);}
          else if(day==3) {
              printf(“\n Week day is Wednesday”);}
          else if(day==4){
              printf(“\n Week day is Thursday”); }
          else if(day==5){
              printf(“\n Week day is Friday”); }
          else if(day==6) {
              printf(“\n Week day is Saturday”); }
          else if(day==7) {
              printf(“\n Week day is Sunday”); }
          else {
                   printf(“\n Wrong  choice. Please enter correct week day.”); }
          } 
      
     

Output-

        Enter the week day number  between 1  to7 
        4
        Week day is Thursday
      
                

5.  switch case statement

syntax of switch case statement:
           switch(control  expression) 
           { 
            case constant1 :  
            statements; 
             break; 
            case constant2 : 
            statements; 
             break; 
             
             ……… 
             ……… 
            default : 
            statements;
           } 
       

In switch case statement, there is no condition instead of that there is control expression. If control expression value does not match with any of case constant then last default case get executed. Here case ends with break statement. break statement breaks current switch case and cursor goes out of switch case.
Switch case statements are used to execute only specific case statements based on the switch expression

Sample program
Implement a program to print the week day using if else if ladder.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
          void main()
          {
            int n;
            clrscr();
            printf("Enter the number of day\n");
            scanf("%d",&n);

            switch(a)
            {
              case 1:
              printf("the day is monday");
              break;
              case 2:
              printf("the day is tuesday");
              break;
              case 3:
              printf("the day is wensday");
              break;
              case 4:
              printf("the day is thrusday");
              break;
              case 5:
              printf("the day is friday");
              break;
              case 6:
              printf("the day is saturday");
              break;
              case 7:
              printf("the day is sunday");
              break;
              default:
              printf("enter correct number");
           }
          getch();
          }
     
      
     

Output-

        Enter the number of day 
        4
        the day is thursday
      
                
Examples For Practice
  1. Check whether user entered character is vowel or consonant.
  2. Input three numbers and find maximum between them.
  3. Implement a program to perform mathematical calculation i.e +,-,*,/ (Take input & operator from user and by using else if ladder perform operation).
  4. Write a C program to check whether a number is even or odd using switch case.