Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

The Case Control Structure in C Programming Language

In actuality, we are regularly looked with circumstances where we are required to settle on a decision between various options instead of just a single or two. For instance, which school to join or which lodging to visit or still harder which young lady to wed. Genuine C writing computer programs is the same; the decision we are requested to make is more muddled than simply choosing between two options. C gives a unique control articulation that enables us to deal with such cases viably; as opposed to utilizing a progression of if explanations. This control guidance is, in reality, the theme of this section. Towards the finish of the section, we would likewise consider a called goto, and comprehend why we ought to stay away from its utilization in C programming.

Choices or making Decisions Using the switch

The control statement that enables us to settle on a choice or decision from the number of decisions is known as a switch, or all the more effectively a switchcase – default, since these three keywords go together to put forth up the control expression. They frequently show up as follows:

switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

The integer expression the keyword switch is any C expression that will yield an integer value. It could be an integer value steady like 1, 2 or 3, or an expression that assesses to an integer. The keywords case is trailed by an integer or a character constant. Every constant for each situation must be unique in relation to all the others. The “do this” lines in the above type of switch speak to any legitimate C expression. Let’s check what happens when we run a program containing a switch? To begin with, the number expression following the keyword switch is assessed. The value it gives is then coordinated, one by one, against the constant qualities that follow the case expression.

At the point when a match is discovered, the program executes the statements following that case, and all resulting case and default explanations also. On the off chance that no match is found with any of the case statements, just the statement following the default is executed. The below couple of examples will indicate how this control structure functions.

Think about the following project:

main( ) {
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "This is case 1 \n" ) ;
case 2 :
printf ( "This is case 2 \n" ) ;
case 3 :
printf ( "This is case 3 \n" ) ;
default :
printf ( "This case is by default \n" ) ;
}
}

                 The output of this program would be:
                        This is case 2
                        This is case 3
                        This case is by default

The output is unquestionably not what we anticipated! We didn’t expect the second and third line in the above program output. The above project output is case 2 and 3 and the default case because we said the switch executes the situation where a match is found and all the consequent cases and the default too. In the event that you need that just case 2 ought to get executed, it is upto you to escape the switch at that point and along these lines utilizing a break statement.

The following program shows how this is finished. Note that there is no requirement for a break statement after the default since the control leaves the switch at any rate.

main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "This is case 1 \n" ) ;
break ;
case 2 :
printf ( "This is case 2 \n" ) ;
break ;
case 3 :
printf ( "This is case 3 \n" ) ;
break ;
default :
printf ( "This case is by default \n" ) ;
}
}

                      The output of this program would be:
                             This is case 2

The post The Case Control Structure in C Programming Language appeared first on Base Read.



This post first appeared on Finance And Computer Technology, please read the originial post: here

Share the post

The Case Control Structure in C Programming Language

×

Subscribe to Finance And Computer Technology

Get updates delivered right to your inbox!

Thank you for your subscription

×