Showing posts with label cases. Show all posts
Showing posts with label cases. Show all posts

Thursday, 9 June 2011

Switch Case in C


    The basic form of a switch statement is:
    switch (variable) { case expression1: do something 1; break; case expression2: do something 2; break; .... default: do default processing; }
     In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.
    Another Example of Switch Case Program:
    switch( marks )      {         case 'A' : printf( "A++" );                    break;         case 'B' : printf( "A+" );            break; case 'C' : printf( "A" );            break;         case 'D' : printf( "B" );            break; case 'F' : printf( "C" );               break; default  : printf( "YOU FAILED" );                    break; }
    The keyword break must be included at the end of each case statement. The default clause is optional
    We also have a continue statement which is used with switch case.
    A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

Switch Case in C


    The basic form of a switch statement is:
    switch (variable) { case expression1: do something 1; break; case expression2: do something 2; break; .... default: do default processing; }
     In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.
    Another Example of Switch Case Program:
    switch( marks )      {         case 'A' : printf( "A++" );                    break;         case 'B' : printf( "A+" );            break; case 'C' : printf( "A" );            break;         case 'D' : printf( "B" );            break; case 'F' : printf( "C" );               break; default  : printf( "YOU FAILED" );                    break; }
    The keyword break must be included at the end of each case statement. The default clause is optional
    We also have a continue statement which is used with switch case.
    A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

Thursday, 26 May 2011

The Switch Statement

The switch statement is similar to the else-if construct but has more flexibility and a clearer format. Let's create a four function calculator using switch statement:


included files
void main()
{
 float num1=1.0,num2=1.0;
 char operator;
 while(!(num1==0.0 && num2==0.0))
 {
  printf("Type number, operator, number\n");
  scanf("%f %c %f",&num1,&operator,&num2);
  switch(operator)
   {
    case'+':
    printf(" = %f",num1 + num2);
    break;
    case'-':
    printf(" = %f",num1 - num2);
    break;
    case'*':
    printf(" = %f",num1 * num2);
    break;
    case'/':
    printf(" = %f"num1 / num2);
    break;
    default:
     printf("Unknown Operator!");
   } 
 printf("\n\n")
 }
getch();
}


In the above code, the statement 'case' is working as 'if', while 'default' is working as 'else' statement...


If you have any question, you can ask me freely!