The Tips and Traps Case Control Statement in C Programming

  • A couple of helpful hints about the utilization of switch and a couple of traps to be kept away from. The prior program that utilized switch may give you the wrong impression that you can utilize just cases organized in ascending order, 1, 2, 3 and default. The real truth can be told, put the cases in any order. Here is a case of mixed case arrange program:
main( )
{
int i = 22 ;
switch ( i )
{
case 121 :
printf ( "This will be case 332 \n" ) ;
break ;
case 7 :
printf ( "This will be case 45 \n" ) ;
break ;
case 22 :
printf ( "This will be case 22 \n" ) ;
break ;
default :
printf ( "This is default case \n" ) ;
}
}
                               The output of this program would be:
                                    This will be case 22

  • We are additionally be permitted to utilize char value for a situation and switch has appeared in the accompanying project. Let’s take a look on below program.
main( )
{
char c = 'x' ;
switch ( c )
{
case 'v' :
printf ( "case v \n" ) ;
break ;
case 'a' :
printf ( "case a \n" ) ;
break ;
case 'x' :
printf ( "case x \n" ) ;
break ;
default :
printf ( "Case default \n" ) ;
}
}

                The output of this program would be:
                          case x

Note: Indeed here when we utilize 'v', 'a', 'x' they are really 
supplanted by the ASCII values (118, 97, 120) of these character constants.
  • We might need to execute a typical/common set of statements for different cases. How this should be possible is appeared in the following project. Let’s take a look.
main( )
{
char ch ;
printf ( "Enter any of the alphabet a, b, or c " ) ;
scanf ( "%c", &ch ) ;
switch ( ch )
{
case 'a' :
case 'A' :
printf ( "a as in Amazon" ) ;
break ;
case 'b' :
case 'B' :
printf ( "b as in Brilliant" ) ;
break ;
case 'c' :
case 'C' :
printf ( "c as in Computer" ) ;
break ;
default :
printf ( "wish you knew what are alphabets" ) ;
}
}

This is how this program will function. We are making utilization of the way that once a case has fulfilled the control just falls through the case till it doesn’t encounter a break statement. That is the reason if an alphabet set an is entered the case ‘a’ is fulfilled and since there are no announcements to be executed for this situation the control naturally achieves the following case i.e. case ‘A’ and executes every one of the announcements for this situation.

Regardless of whether there are various statements to be executed for each situation, there is no compelling reason to wall them in inside a couple of braces (not at all like if, and else).

  • Every explanation in a switch must have a place with some case or the other. On the off chance that a statement doesn’t have a place with any case the compiler won’t report an error. Be that as it may, the statement could never get executed. For instance, in the accompanying program, the printf( ) never goes to work.
main( )
{
int i, j ;
printf ( "Enter value of i" ) ;
scanf ( "%d”, &i ) ;
switch ( i )
{
printf ( "Hello Programmers" ) ;
case 1 :
j = 10 ;
break ;
case 2 :
j = 20 ;
break ;
}
}

On the off chance that we have no default case, at that point, the program just falls through the whole switch and proceeds with the following guidance (if any) that following the closing brace of the switch.

  • Is switch a replacement for if? The result will come Yes and No. Yes, in light of the fact that it offers a superior method for composing programs when contrasted with if, and no on the grounds that in specific circumstances we are left with no decision, however, to utilize if. The inconvenience of switch is that one can’t have a case in a switch which resembles: case I <= 20 : All that we can have after the case is an int constant or a char value or an expression that assesses to one of these constants. Even a float isn’t permitted. The upside of switch over if is that it prompts a more organized program and the level of space is manageable, more so if there are different statements inside each instance of a switch.
  • The break statements when utilized in a switch takes the control outside the switch. but however, utilization of proceeding won’t take the control to the start of switch as one is probably going to accept.
  • In rule, a switch may happen inside another, yet by and by it is once in a while done. Such explanations would be called nested switch statements.
  • The switch statement is extremely valuable while composing menu driven projects programming language.

Post Author: Base Read

Leave a Reply

Your email address will not be published. Required fields are marked *