Multi-way Selection
7
Introduction to Programming
Objectives After completing this lesson, you should be able to do the following: • Understand how a multi-way selection control structure works • Learn how to use the if-else if-else and switch selection control structure
Introduction to Programming
Selection Control Structures • Two-way selection if-else • Multi-way selection a) if – else if – else b) switch statement
Introduction to Programming
If-else if Statements Syntax: if(expression1) statement1; else if(expression2) statement2; … else if(expression) statementN-1; else
• The conditional conditions are tested from top downwards. • As soon as a true condition is found, the statement associated with it is executed and the rest are bypassed. • If all other conditional tests fail then the last else statement is performed.
statementN Introduction to Programming
Example: if-else if Statement if ( x > 0) printf(“x else if ( x printf(“x else printf(“x
is positive”); < 0) is negative”); is zero”);
Introduction to Programming
Switch Statement Syntax: • expr – an switch (expr){ expression which case const1: evaluates to an int statement(s); or char case const2: • Case list must statement(s); consist of constant …. values whose type case constn: matches that of the statement(s); switch expr default: • Cannot include statement(s); variables or expr } Introduction to Programming
Switch Statement • If a case matches the expr value, execution starts at that case • Default is optional • If the value of the expr does not match any label, the control transfers outside of the switch statement • If default is included, the statements under default is executed
Introduction to Programming
Example: Switch Statement int a = 20, b = 15, c = 0, x; scanf(“%d”, &x); switch (x) { case 5: a = a + 10; case 4: b = b + 25; case 3: a = a + b; case 2: b = b * 2; default: c = a + b; } Introduction to Programming
Switch Statement • There are situations which we need to execute statements exclusively • Exclusive execution can be achieved by placing break statements in between cases • BREAK statement causes program control to immediately exit from the switch statement
Introduction to Programming
Example: Switch Statement char color; scanf(“%c”, &color); switch (color){ case ‘R’: printf(“Red”); break; case ‘G’: printf(“Green”); break; case ‘B’: printf(“Blue”); break; default: printf(“No color selected.”); } Introduction to Programming
Example: Switch Statement Problem: Write a program that accepts a number between 1 to 8 and print if it is even or add. … switch (num) { case 2: case 4: case 6: case 8: printf(“number is even”); break; Introduction to Programming
Example: Switch Statement case case case case
1: 3: 5: 7: printf(“number is odd”); break; default: printf(“number is not in case list”);
}
Introduction to Programming
Summary
Introduction to Programming
Laboratory Exercise •
Write a C program that acts as a simple calculator. Display the following menu:
Select Operation (1) Addition (2) Subtraction (3) Multiplication (4) Division Enter choice: 1 Enter 2 integers: 3 4 Sum is 7 Introduction to Programming
Laboratory Exercise • Write a C program that computes the area, perimeter and volume Display the following menu: <1> Area of a rectangle <2> Perimeter of a rectangle <3> Volume of a cube Enter choice: 1 Enter length and width: 3 5 Area is 15 Introduction to Programming