Case Control Statements

   The statements which are used to execute only specific block of statements in a series of blocks are called case control statements.

There are 4 types of case control statements in C language. They are,

  1. switch
  2. break
  3. continue
  4. goto

Switch case statement

Switch case statements are used to execute only specific case statements based on the switch expression.

Write a program that asks potion and two operands and perform the corresponding calculation on the operands.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int a, b, choice ;

  clrscr ( ) ;

  Printf ( “ enter a, b values\n ”) ;

  Scanf ( ” % d %d ”, &a, &b );

  Printf ( “ 1. Sum \n 2. Difference \n 3.Product \n 4.Quotient \n 5. Remainder \n ”) ;

  Printf ( “ enter your choice \n ”) ;

  Scanf ( ” % d ”, &choice ) ;

  Switch (choice)

                                {

                                   case 1 :

                                   Printf ( “ The sum is %d \n ”, a + b ) ;

                                   Break ;

                                            case 2 :

                                   Printf ( “ The difference is %d \n ”, a - b ) ;

                                   Break ;

                                            case 3 :

                                   Printf ( “ The Product is %d \n ”, a * b ) ;

                                   Break ;

                                            case 4 :

                                   Printf ( “ The Quotient is %d \n ”, a / b ) ;

                                   Break ;

                                            case 5 :

                                   Printf ( “ The remainder is %d \n ”, a % b ) ;

                                   Break ;

                                            Default  :

                                   Printf ( “ It is wrong choice \n ” ) ;

                                            Break ;

                                         }

     Getch ( ) ;

}

2. Accept weekday number then display its related weekday name.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int choice ;

  clrscr ( ) ;

  Printf ( “ 1. Sunday \n 2. Monday \n 3. Tuesday\n 4. Wednesday\n 5. Thursday\n 6. Friday \n 7. Saturday \n ”) ;

  Printf ( “ enter your choice \n ”) ;

  Scanf ( ” % d ”, &choice ) ;

  Switch (choice)

                                {

                                   case 1 :

                                   Printf ( “ The day is Sunday \n ” ) ;

                                   Break ;

                                            case 2 :

                                   Printf ( “ The day is monday \n ” ) ;

                                   Break ;

                                            case 3 :

                                   Printf ( “ The day is Tuesday \n ” ) ;

                                   Break ;

                                            case 4 :

                                   Printf ( “ The day is Wednesday \n ” ) ;

                                   Break ;

                                            case 5 :

                                   Printf ( “ The day is Thursday \n ” ) ;

                                   Break ;

                                            case 6 :

                                   Printf ( “ The day is Friday \n ” ) ;

                                   Break ;

                                            case 7 :

                                   Printf ( “ The day is Saturday\n ” ) ;

                                   Break ;

                                            Default  :

                                   Printf ( “ It is wrong choice \n ” ) ;

                                            Break ;

                                         }

     Getch ( ) ;

}

3. Accept Student Grade then display its related Grade report .

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  Char grade ;

  clrscr ( ) ;

  Printf ( “ Grades are \ t 1. A \n 2. B \n 3. C \n 4. D \n  ”) ;

  Printf ( “ enter your Grade \n ”) ;

  Scanf ( ” % c ”, & grade ) ;

  Switch ( grade )

                                {

                                   case 'A' :

                                   Printf ( “ Excellent \n ”) ;

                                   Break ;

                                            case 'B' :

                                   Printf ( “ Welldone \n ”) ;

                                   Break ;

                                            case 'C' :

                                   Printf ( “ Passed \n ”) ;

                                   Break ;

                                            case 'D' :

                                   Printf ( “ Better try again \n ” ) ;

                                   Break ;                                       

                                            Default  :

                                   Printf ( “ It is wrong choice \n ” ) ;

                                            Break ;

                                         }

     Getch ( ) ;

}

 

 

 

 

Break statement

break is used in terminating the loop immediately after it is encountered.Break statement is used to terminate the while loops, switch case loops and for loops from the subsequent execution.

1. write a program to print 1 to 5 numbers using break statement.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int i ;

  clrscr ( ) ;

  For ( i = 0 ; i < = 10 ;  i + +  )

       {

           if (  i = = 5 )

           Break ;

            Printf ( ” % d \n ”, i ) ;

       }

orGetch ( ) ; ;

}

2. write a program to demonstrate the working of break statement by terminating a loop, if user inputs negative number.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int i, n, num, avg, sum ;

  clrscr ( ) ;  

  Printf ( “ enter no.of inputs \n ”) ;

  Scanf ( ” % d ”, &n ) ;

  For ( i = 1 ; i < = n ;  + +i  )

       {

           Printf ( “ enter n%d :  \n ” , i ) ;

           Scanf ( ” % d ”, &num ) ;

           if (  num < 0.0 )

           Break ;

            sum = sum + num ;

       }

    avg = sum / ( i - 1) ;

    Printf ( “ average is %d ” , avg ) ;  

orGetch ( ) ; ;

}

 

 

 

 

 

 


 

 

 

Continue statement

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.

1. write a program to print 1 to 10 numbers except 5.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int i ;

  clrscr ( ) ;

  For ( i = 0 ; i < = 10 ;  i + +  )

       {

           if (  i = = 5 )

           Continue ;

            Printf ( ” % d \n ”, i ) ;

       }

orGetch ( ) ; ;

}

2. write a program to print 1 to 10 numbers except 2, 5, 8.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int i ;

  clrscr ( ) ;

  For ( i = 0 ; i < = 10 ;  i + +  )

       {

           if (  i = = 2 || i = = 5 || i = = 8 )

           Continue ;

            Printf ( ” % d \n ”, i ) ;

       }

orGetch ( ) ; ;

}

3.Write a program to find the product of 4 integers entered by a user. If user enters 0 skip it.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int i , num, product  ;

  clrscr ( ) ;

  For ( i = 1, product = 1 ; i < = 4 ;   + + i  )

           {

                Printf ( ”enter num % d \n ”, i ) ;

                Scanf ( ”%d ”, &num ) ;

                 if (  num = = 0 )

                 Continue ;

                 product  = product * num ;

          }       

    Printf ( ” product = % d  ”, product ) ;

    Getch ( ) ;
}

 

Goto statement

goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

1. write a program to print numbers 100 using goto statement .

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int  n = 0 ;

  clrscr ( ) ;

  a :

      n = n + 1 ;

      Printf ( ”  % d \ t ”, n ) ;

      if (  n > = 100 )

             {

                  goto b ;

              }

   goto a ;

   b :

   Printf ( ” end of the program ” ) ;

  Getch ( ) ;

}

2. write a program to print 1 to 20 numbers except 15.

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  int  a = 10 ;

  clrscr ( ) ;

  loop : do

       {

           if (  a = = 15 )

               {

                      a = a + 1 ;

                      goto loop ;

              }

              Printf ( ” value of a is % d \n ”, a ) ;

              a++;

       } while ( a < = 20 ) ;

orGetch ( ) ; ;

}

3. write a program to demonstrate goto statement .

/*******************************************************************/

//******************************************************************

# include < stdio.h >

# include < conio.h >

void main ( )

{

  clrscr ( ) ;

  Printf ( ” one \n ” ) ;

  Printf ( ” two \n ” ) ;

  goto sai ;

  Printf ( ” three \n ” ) ;

  Printf ( ” four \n ” ) ;

  sai :

  Printf ( ” five \ n  ” ) ;

   Getch ( ) ;

}