Case Control Structure DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP)
Decision control structure In real life we are often faced with situations where we are required to make a choice between a number of alternatives rather than only one or two. For example, Which school to join??? or Which hotel to visit??? or Which girl/boy to marry (you almost always end up making a wrong decisions a different matter altogether!). 2
C provides a special control statement that allows us to handle such cases effectively; rather than using a series of if statements. Switch Switch 3
Decisions Using switch The control statement that allows us to make a decision from the number of choices is called a switch. Or more correctly a switch case-default, since these three keywords go together to make up the control statement. 4
Switch Expression can be an integer constant (1,2,3 or a character constant (x,y,z etc). The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The do this lines in the above form of switch represent any valid C statement. 5
Switch Execution First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. If no match is found with any of the case statements, only the statements following the default are executed. 6
Example The output of this program Would be!!! I am in case 2 I am in case 3 I am in default The output is definitely not what we expected! We don t want the cases except case 2. 7
Switch with Break statement If the concerned case statement executing then we use Break statement. Note that there is no need for a break statement after the default, since the control comes out of the switch anyway. 8
Flow Chart of Switch with Break 9
Switch cases The output of this program Would be!!! I am in case 2 10
11
Example The output of this program Would be!!! I am in case x Note: In fact here when we use v, a, x they are actually replaced by the ASCII values (118, 97, 120) of these character constants. 12
common set of statements for multiple cases. If an alphabet a is entered the case a is satisfied and since there are no statements to be executed in this case the control automatically reaches the next case i.e. case A and executes all the statements in this case. 13
Rules If there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else). Every statement in a switch must belong to some case or the other. If a statement doesn t belong to any case the compiler won t report an error. However, the statement would never get executed. For example, in the following program the printf( )never goes to work. 14
Example Printf() within switch will never be executed!!! 15
Rules If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch. A float switch expression is not allowed. The advantage of switch over if is that it leads to a more structured program and the level of indentation is manageable, 16
Rules We can check the value of any expression in a switch. Thus the following switch statements are legal. switch ( i + j * k ) switch ( 23 + 45 % 4 * k ) switch ( a < 4 && b > 7 ) Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7is correct, however, case a + b is incorrect. In principle, a switch may occur within another, but in practice it is rarely done. 17
Can t do with a switch A float expression cannot be tested using a switch. Cases can never have variable expressions (for example it is wrong to say case a +3 :) Multiple cases cannot use same expressions. Thus the following switch is illegal: 18
Concept revision regarding switch The switch keyword is followed by an integer or an expression that evaluates to an integer. The case keyword is followed by an integer or a character constant. The control falls through all the cases unless the break statement is given. 19
The Goto Statement 20
Exit() The exit( int ) function is a standard library function which terminates the execution of the program. <stdlib.h> should be included into your program while using exit(integer) function; Mostly for exiting the control exit(0); is used. 21
Write a program using, Decision control structure Case control structure Goto statement Class Assignment Time: 5min That should produce the following output??? Enter choice (1 to 3) 1 I am in case 1 Do you want to execute again ( Y or N)???y Enter choice (1 to 3) 2 I am in case 2 Do you want to execute again ( Y or N)??? 22
Program using Switch and Goto Statement //Switch-case-default with Goto statement #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { ss: int n; char ch; printf("\nenter choice (1 to 3) "); scanf("%d", &n); switch(n) { case 1: printf("\ni am in case 1\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; case 2: printf("\ni am in case 2\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; case 3: printf("\ni am in case 3\n"); exit(0); break; default: printf("\n You have entered a wrong value\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; } getche(); } 23
24