Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | bertram-evans |
View: | 226 times |
Download: | 5 times |
CHAPTER 3CHAPTER 3CONTROL STRUCTURESCONTROL STRUCTURES
(SELECTION)(SELECTION)
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
2
CONTENTS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
The switch statement Nested selection
LEARNING OUTCOME
At the end of : Identify the concept and usage of selection
control structure: able to write a program using
4
THE switch STATEMENT switch structure: alternate to if..else
break;}
5
THE switch STATEMENTS
em
este
r Jan
– A
pr 2
01
0
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
switch expression is evaluated first Value of the expression determines which
corresponding action is taken Expression is sometimes called the selector Expression value can be only integral Its value determines which statement is selected
for execution A particular case value should appear only once
6
THE switch STATEMENTS
em
este
r Jan
– A
pr 2
01
0
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
One or more statements may follow a case label
Braces are not needed to turn multiple statements into a single compound statement
The break statement may or may not appear after each statement
switch, case, break, and default are reserved words
7
THE switch STATEMENT
Rules : When value of the expression is matched against a case value, Statements execute until break statement is found
or the end of switch structure is reached If value of the expression does not match any of the
case values Statements following the default label execute If
no default label, and if no match, the entire switch statement is skipped
A break statement causes an immediate exit from the switch structure
8
THE switch STATEMENTExample
9
THE switch STATEMENTExample (cont’d)
Example 2: For more than
one options in every cases (example: capital and small letters), the following example can be done.
switch statement#include<iostream.h>void main(){
char grade;cout<<”Enter your grade:”;cin>>grade;
switch (grade){
case ‘A’:case ‘a’:
cout<<”Excellent”;break;
case ‘B’:case ‘b’:
cout<<”Average”;break;
case ‘C’:case ‘c’:
cout<<”Poor”;break;
case ‘F’:case ‘f’:
cout<<”Try Again”;break;
}cout<<”Hope you are satisfied with your results!”;
}
}
11
THE nested STATEMENT
Multiple selection When one control statement is located within another
(nested) The rule :
- Pairing and else with an if - An else is associated with the most recent if
that has not been paired with an else ( an else is always belongs to the closest if)
12
THE nested if STATEMENTS
em
este
r Jan
– A
pr 2
01
0
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Consider the following statements :
else
13
THE nested if STATEMENT
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Exercise 2 Consider the following case :
Nation’s Air force has asked you to write a program to label supersonic aircraft as military or civilian. Your program is to be given the plane’s observed speed in km/h and its estimated length in meters. For planes traveling in excess of 1100km/h, you will label those longer than 52 meters “civilian” and shorter aircraft as “military”. For planes traveling at slower speeds, you will issue an “aircraft type unknown” message.
Exercise 3
Write the C++ program based from the flowchart shown below: