Lecture 8 Control Structures I Selection cont Introduction

  • Slides: 17
Download presentation
Lecture 8: Control Structures I (Selection) (cont. ) Introduction to Computer Science Spring 2006

Lecture 8: Control Structures I (Selection) (cont. ) Introduction to Computer Science Spring 2006 1

Contents n Selection control structures: if, if. . . else n n n Compound

Contents n Selection control structures: if, if. . . else n n n Compound statement Nested if Selection control structures: switch 2

One-Way (if) Selection n The syntax of one-way selection is: if(expression) statement n n

One-Way (if) Selection n The syntax of one-way selection is: if(expression) statement n n Statement is executed if the value of the expression is true Statement is bypassed if the value is false; program goes to the next statement 3

4

4

Two-Way (if…else) Selection n Two-way selection takes the form: if(expression) statement 1 else statement

Two-Way (if…else) Selection n Two-way selection takes the form: if(expression) statement 1 else statement 2 n n n If expression is true, statement 1 is executed otherwise statement 2 is executed statement 1 and statement 2 are any C++ statements else is a reserved word 5

6

6

Compound (Block of) Statement n Compound statement (block of statements): { statement 1; statement

Compound (Block of) Statement n Compound statement (block of statements): { statement 1; statement 2; . . . statementn; } n A compound statement is a single statement 7

Compound Statement Example if(age > 18) { cout<<" Eligible to vote. "<<endl; cout<<" No

Compound Statement Example if(age > 18) { cout<<" Eligible to vote. "<<endl; cout<<" No longer a minor. "<<endl; } else { cout<<"Not eligible to vote. "<<endl; cout<<"Still a minor. "<<endl; } 8

Nested if n n n Nesting: one control statement in another An else is

Nested if n n n Nesting: one control statement in another An else is associated with the most recent if that has not been paired with an else For example: if(score >= 90) cout<<"The grade is A"<<endl; else if(score >= 80) cout<<"The grade is B"<<endl; else cout<<"The grade is F"<<endl; 9

Input Failure and the if Statement n If input stream enters a fail state

Input Failure and the if Statement n If input stream enters a fail state n n n All subsequent input statements associated with that stream are ignored Program continues to execute May produce erroneous results Can use if statements to check status of input stream If stream enters the fail state, include instructions that stop program execution 10

switch Structures n Switch structure: alternate to if-else n Switch expression is evaluated first

switch Structures n Switch structure: alternate to if-else n Switch expression is evaluated first n n Value of the expression determines which corresponding action is taken Expression is sometimes called the selector 11

switch Structures (continued) n n n Expression value can be only integral Its value

switch Structures (continued) n n n Expression value can be only integral Its value determines which statement is selected for execution A particular case value should appear only once 12

13

13

switch Structures (continued) n n One or more statements may follow a case label

switch Structures (continued) n n 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 14

switch Statement Rules n When value of the expression is matched against a case

switch Statement Rules n When value of the expression is matched against a case value, n n If value of the expression does not match any of the case values n n Statements execute until break statement is found or the end of switch structure is reached Statements following the default label execute If no default label and no match the entire switch statement is skipped A break statement causes an immediate exit from the switch structure 15

switch Statement Rules (continued) n When value of the expression is matched against a

switch Statement Rules (continued) n When value of the expression is matched against a case value, n n If value of the expression does not match any of the case values n n Statements execute until break statement is found or the end of switch structure is reached Statements following the default label execute If no default label and no match the entire switch statement is skipped A break statement causes an immediate exit from the switch structure 16

End of lecture 8 Thank you! 17

End of lecture 8 Thank you! 17