Flow of Control • The order in which program statements are executed • Flow is sequential unless a “control structure” is used to change that • Control structures used to alter the normally sequential flow of control • Two general types of control structures: – Selection (also called branching) – Repetition (also called looping)
Did you break anything?
Yes
Blame it on someone else
Lay low and keep hidden
Yes
No Are you lying?
Yes
Have your parents asked about it?
No Are you passing your classes? Yes
No
No
Ask for $$ 4
3
Sequential Flow Example Algorithm for calculating the area of a circle:
Selection Structure Example Algorithm to find the pass/fail grade of a student:
1. input radius 2. calculate area 3. print area
If total ≥ 60 grade = “pass” Otherwise grade = “fail” 5
6
1
Repetition Structure Example
Conditions • In order to make control decisions, we must be able to express conditions. • Sometimes called logical expressions
Algorithm to display numbers from 1 to 100:
For i = 1 to 100 print the value
– Evaluates to either true or false – In C++, zero (0) represents false and any non-zero represents true
of i
• Complex conditions are constructed using – Relational operators – Logical operators 7
8
bool Data Type
The Boolean
• C++ standard defines a type bool for variables representing true or false. • Example bool happy; happy = true;
• A bool is a something that resolves to true (1) or false (0) • You can get booleans several different ways – Simple – Complex
• These booleans will be used (later) to make decisions to change your program’s behavior
• The relational and logic operators all return values of type bool.
10
9
Relational Operators
Relational Operator Examples
(for primitive data types) • Literal Example
• Relational Operators
5>3 6 != 6 (4 greater than < less than == equals ! Not != not equal >= greater than or equal num1; //Note: result is now true 12
2
Relational Operators
&& and || Operators • These operators check for multiple conditions • && (AND) needs both the left and the right to be true in order to return true • || (OR) needs either one (or both) to be true to return true • Can shortcut if necessary • Examples: ( (6 > 5) && ( ‘c’ == ‘b’) ) ( (6 > 5) && ( 7 < 9) ) ( (6 > 5) || ( ‘c’ == ‘b’) ) ( (6 > 6) || ( ‘c’ == ‘b’) )
• They are used in expressions of form: expression1 operator expression2
• Examples x > y + 5 a
C++ Example
Meaning
x > y
x is greater than y
=
x >= y
x is greater than or equal to y
== && || =
Unary+ / % < >= !=
Unary–
= 7 + 3
&&
Example of Logical Expression
7 % 3 == 1
10
6 > 2
||
25 / 5 == 4
1
FALSE
5 TRUE
TRUE
FALSE
FALSE TRUE
19
20
Example of Logical Expression 25 > 3
&&
Exercise • Write an expression for each:
!(6 == 4)
– taxRate is over 25% and income is less than $20000 – temperature is less than or equal to 75 or humidity is less than 70% – age is over 21 and age is less than 60 – age is 21 or 22
FALSE TRUE
TRUE TRUE 21
22
The if Structure
C++ Selection Structures • The if structure • The if/else structure • The switch structure
Statement(s) Entry Point
Flow chart:
TRUE
Conditio n Statement(s) FALSE Exit Point Statement(s)
23
24
4
The if Structure •
• •
Example
Simplest selection structure if (condition) statement; The statement is executed if condition is true We can use a code block in place of the single statement to allow multiple statements if (condition) { statement1; statement2; . . . }
Outputs:
#include using namespace std; int main() { int score; cout > score; cout = 60) cout