Upload
wilmarc
View
521
Download
4
Embed Size (px)
Citation preview
Introduction to Programming
ObjectivesObjectives
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 StructuresSelection Control Structures
• Two-way selection
if-else
• Multi-way selection
a) if – else if – else
b) switch statement
Introduction to Programming
If-else if StatementsIf-else if Statements
• 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.
Syntax: if(expression1) statement1; else if(expression2)
statement2;
… else if(expression)
statementN-1; else
statementN
Introduction to Programming
Example: if-else if StatementExample: 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 StatementSwitch Statement
Syntax: switch (expr){case const1:
statement(s); case const2:
statement(s);….case constn:
statement(s);default:
statement(s);}
• expr – an expression which evaluates to an int or char
• Case list must consist of constant values whose type matches that of the switch expr
• Cannot include variables or expr
Introduction to Programming
Switch StatementSwitch 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 StatementExample: 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 StatementSwitch 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 StatementExample: 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 StatementExample: Switch Statement
…
switch (num)
{
case 2:
case 4:
case 6:
case 8: printf(“number is even”);
break;
Problem: Write a program that accepts a number between 1 to 8 and print if it is even or add.
Introduction to Programming
Example: Switch StatementExample: 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
Laboratory ExerciseLaboratory Exercise• Write a C program that acts as a simple calculator.
Display the following menu:
Select Operation(1) Addition(2) Subtraction(3) Multiplication(4) DivisionEnter choice: 1
Enter 2 integers: 3 4Sum is 7