Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"— Presentation transcript:

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

2 2 C ONTENTS CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING The switch statement Nested selection

3 L EARNING O UTCOME At the end of : Identify the concept and usage of selection control structure: able to write a program using

4 4 THE switch STATEMENT switch structure: alternate to if..else break; }

5 5 THE switch STATEMENT Semester Jan – Apr 2010 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 6 THE switch STATEMENT Semester Jan – Apr 2010 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 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 8 THE switch STATEMENT Example

9 9 THE switch STATEMENT Example (cont’d)

10 Example 2: For more than one options in every cases (example: capital and small letters), the following example can be done. switch statement #include 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 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 12 THE nested if STATEMENT Semester Jan – Apr 2010 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Consider the following statements : else

13 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.

14 Exercise 3 Write the C++ program based from the flowchart shown below:


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

Similar presentations


Ads by Google