The document discusses the case control structure in C programming using switch-case-default. It provides the general syntax for switch statements and explains that break statements are needed to exit each case. Tips are given such as cases can be in any order and common code can be shared between cases. Advantages are that switch structures the program, but disadvantages are cases must be integer or char constants. The document also briefly discusses the goto keyword.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views7 pages
The Case Control Structure
The document discusses the case control structure in C programming using switch-case-default. It provides the general syntax for switch statements and explains that break statements are needed to exit each case. Tips are given such as cases can be in any order and common code can be shared between cases. Advantages are that switch structures the program, but disadvantages are cases must be integer or char constants. The document also briefly discusses the goto keyword.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7
The case control structure
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. • General syntax switch(integer expression) { case constant 1: do this; case constant 2: do this; case constant 3: do this; default: do this; } Decisions Using switch… • Use of break statement. void main() { int x=2; switch(x) { case 1: printf(“I am in case 1 \n”); break; case 2: printf(“I am in case 2 \n”); break; case 3: printf(“I am in case 3 \n”); break; default: printf(“I am in default \n”); } } The tips and traps • We can put the cases in any order. • You are also allowed to use char values in case and switch. • When we use char they are actually replaced by the ASCII values. • We can execute a common set of statements for multiple cases. • Multiple statement can be executed in each case. There is not 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. The tips and traps • Is switch a replacement for if ? Yes and no. • Following switch statements are legal switch(i*j*k) switch(23+45%4*k) switch(a<4 && b>7) • case 3+4 is correct . • case a+b is incorrect. • switch may occur within another. Advantage Disadvantage Advantages • switch is more structured program. • The level of indentation is manageable. Disadvantages • One can not have a case in a switch which looks like: case i<20: • Even a float is not allowed. Only int constant and char constant or an expression that evaluates to one of these constants are allowed. The goto keyword • General syntax ------ ----- main() { --- goto sos; --- --- sos : --- --- } • Avoid goto keyword ! • The exit() function is a standard library function which terminates the execution of the program.