14
CHAPTER 3 CHAPTER 3 CONTROL STRUCTURES CONTROL STRUCTURES (SELECTION) (SELECTION) INTRODUCTION TO INTRODUCTION TO COMPUTER PROGRAMMING COMPUTER PROGRAMMING (CSC425) (CSC425)

CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Embed Size (px)

Citation preview

Page 1: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

CHAPTER 3CHAPTER 3CONTROL STRUCTURESCONTROL STRUCTURES

(SELECTION)(SELECTION)

INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER

PROGRAMMINGPROGRAMMING(CSC425)(CSC425)

Page 2: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2

CONTENTS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

The switch statement Nested selection

Page 3: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

LEARNING OUTCOME

At the end of : Identify the concept and usage of selection

control structure: able to write a program using

Page 4: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

4

THE switch STATEMENT switch structure: alternate to if..else

break;}

Page 5: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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

Page 6: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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

Page 7: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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

Page 8: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

8

THE switch STATEMENTExample

Page 9: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

9

THE switch STATEMENTExample (cont’d)

Page 10: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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!”;

}

}

Page 11: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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)

Page 12: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

12

THE nested if STATEMENTS

em

este

r Jan

– A

pr 2

01

0

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Consider the following statements :

else

Page 13: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

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.

Page 14: CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Exercise 3

Write the C++ program based from the flowchart shown below: