Showing posts with label Switch statement. Show all posts
Showing posts with label Switch statement. Show all posts

Sunday, 23 October 2011

Predict the output or error(s) for the following:

 main()

{

int i=3;


            switch(i)


             {


                default:printf("zero");


                case 1: printf("one");


                           break;


               case 2:printf("two");


                          break;


              case 3: printf("three");


                          break;


              } 


}

Answer :

three

Explanation :

Wednesday, 19 October 2011

Can the last case of a switch statement skip including the break?

Even though the last case of a switch statement does not require a break statement at the end, you should add break statements to all cases of the switch statement, including the last case. You should do so primarily because your program has a strong chance of being maintained by someone other than you who might add cases but neglect to notice that the last case has no break statement. This oversight would cause what would formerly be the last case statement

Tuesday, 18 October 2011

Is a default case necessary in a switch statement?

No, but it is not a bad idea to put default statements in switch statements for error- or logic-checking purposes. For instance, the following switch statement is perfectly normal:

switch (char_code)
{
case ‘Y’:
case ‘y’: printf(“You answered YES!\n”);
break;
case ‘N’:
case ‘n’: printf(“You answered NO!\n”);
break;
}


Consider, however, what would happen if an unknown character

Friday, 14 October 2011

Is a default case necessary in a switch statement?

No, but it is not a bad idea to put default statements in switch statements for error- or logic-checking purposes. For instance, the following switch statement is perfectly normal:

switch (char_code)
{
case ‘Y’:
case ‘y’: printf(“You answered YES!\n”);
break;
case ‘N’:
case ‘n’: printf(“You answered NO!\n”);
break;
}


Consider, however, what would happen if an unknown character code were passed to this switch statement. The program would not print anything. It would be a good idea, therefore, to insert a default case where this condition would be taken care of:


...
default: printf(“Unknown response: %d\n”, char_code);
break;
...


Additionally, default cases come in handy for logic checking. For instance, if your switch statement handled a fixed number of conditions and you considered any value outside those conditions to be a logic error, you could insert a default case which would flag that condition. Consider the following example:

void move_cursor(int direction)
{
switch (direction)
{
case UP: cursor_up();
break;
case DOWN: cursor_down();
break;
case LEFT: cursor_left();
break;
case RIGHT: cursor_right();
break;
default: printf(“Logic error on line number ld!!!\n”,  __LINE__);
break;
}
}

Thursday, 13 October 2011

When is a switch statement better than multiple if statements?

A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type. For instance, rather than the code