Decision making and branching(Goto, switch, conditional operator)

In our previous post on C we discussed about the decision making and branching in C using various If-else statements and some programs related to it. Here in this post we are going to explain some other ways of decision making and branching which are very much useful in some programs which are menu driven or do not want to have big logic of loops, but still want a simple loop using a branching statement.

As we have already discussed in our previous post that what is decision making and branching in C and what are various IF-else statements provided in C, I will explain some of the more usable options provided in C:
  • The Switch statement
  • The goto Statement
  • The conditional operator
  • These three branching and decision making statements are often used by the programmers. The switch statement provides the user a way to drive the program in a menu driven list, it gets a value from the user and chooses one particular case to be executed on that value. The goto statement works with a label, and whenever the goto statement is written it performs a branch over to the place where that label is being written. The conditional operator is explained in our previous post of Operators and Expression.





    The Switch Statement:

    The switch statement is the replacement of If statement where one of many choices are needed to be selected. In such cases the complexity of a program gets increased if the IF statement is used, but it gets reduced/ nullified by the SWITCH statement.
    for e.g If a program is written for an ice-cream parlor and the user is informed to select one flavor at a time from 5 different flavors then Using IF statement it would look something like this.....

    printf("Enter the flavor number you want to choose");
    printf("\n 1) Chocolate");
    printf("\n 2) Strawberry");
    printf("\n 3) Raspberry");
    printf("\n 4) Mango");
    printf("\n 5) Orange");
    scanf("%d",&choice);
    if(choice==1) printf("You chose chocolate");
    if(choice==2) printf("You chose Strawberry");
    if(choice==3) printf("You chose Raspberry");
    if(choice==4) printf("You chose Mango");
    if(choice==5) printf("You chose Orange");

    As you can see above the programmer may make some mistake in changing one of the case while making a change in the menu, or it is possible that programmer may change, by mistake, the choice of different case.

    Hence to handle such kind of case (Menu driven list programs), Switch statements were introduced.
    Its syntax is as follows:

    switch(choice)
    {
           case value-1: //Statements in this case to be performed OR executed
                                 break;

           case value-2: //Statements in this case to be performed OR executed
                                 break;

           case value-3: //Statements in this case to be performed OR executed
                                 break;
           ...............
            ...................

           default       : //default execution , in case if none of the case value is chosen
                                 break;
    }

    As shown above, the syntax of switch is very much user friendly, from the programmers point of view. The choice inside the brackets in switch indicated the value to be chosen from the given particular choices. When Switch statement is executed those values in each case (for above example they are : value-1=1, value-2 =2,value-3=3, and so on) is compared with the value of choice. Whichever cases' value matches the value given as choice in switch statement, the code written inside its block are executed. If the value of choice doesn't match any of the given case values then the default block is executed, which generally contains a message Or warning to chose the correct value from the given list of choices to the application user.
    Each block contains a break of its own indicating the end of that case block.



    The Go-to statement: 

    The Go-to statement is more of branching statement which can either be executed conditionally by using it with the IF statement or some other decision making statements, Or unconditionally by directly using it in the program. The goto statement basically branches the control of the program unconditionally to the label written with it.
    This is shown below:

    goto label;                                               label:
    /*...............                                               /*...............                                               
    ................                                                   ................
    statements....                                              statements....
     ................                                                  ............
    ............*/                                                     ...........*/
    label:                                                        goto label;
    statements;                                               //statements.......

    Above shown are two different cases of goto statement where, in first case it branches the control of the loop skipping the upcoming statements until the label and executing those statements which are written only after label. In the second case it makes a loop by executing the same statement blocks again and again by branching to the label which is in the previously executed lines. Hence the codes written after the label in that case will be executed and then after some time the goto would be again executed and again those blocks would be executed making a loop, such a goto statement needs a condition that , at some time, wont allow the goto statement to be executed like any decision making statement, so that there wont be any infinite looping.
    Such Go-to statements should not be used with highly structured programs. But they are used carefully when there is a need, and inspected fro each aspect so that in any condition they dont lead to a infinite looping condition, Or skip an important instruction.

    The Conditional Operator:

    This operator is just a symbolic conversion of If-else statement, which is explained on this link: conditional operator.


    In case you missed to read the previous part of Decision making and branching statements post you may visit it here: - Decision making and branching

    Else you may also visit other sections of our page from: HERE

    Comments

    Popular posts from this blog

    Windows phone Wifi connectivity problem.

    Create Excel file using Java: Apache POI - PART - 2: Line Chart