You are on page 1of 15

# Multi-way Selection

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:
• The conditional
if(expression1)
conditions are tested
statement1; from top downwards.
else
if(expression2)
• As soon as a true
condition is found, the
statement2; statement associated
… with it is executed and
else the rest are bypassed.
if(expression) • If all other conditional
statementN-1; tests fail then the last
else else statement is
statementN performed.
Introduction to Programming
Example: if-else if Statement

if ( x > 0)
printf(“x is positive”);
else if ( x < 0)
printf(“x is negative”);
else
printf(“x 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:
statement(s);
• Cannot include
} 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

switch (num)
{
case 2:
case 4:
case 6:
case 8: printf(“number is even”);
break;
Introduction to Programming
Example: Switch Statement

case 1:
case 3:
case 5:
case 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.

Select Operation
(2) Subtraction
(3) Multiplication
(4) Division
Enter choice: 1

Enter 2 integers: 3 4
Sum is 7
Introduction to Programming
Laboratory Exercise

and volume