Download presentation
Presentation is loading. Please wait.
Published byYulia Oesman Modified over 5 years ago
1
Control Structures Selection or Decision Branching
2
Decision Two types: simple alternative if
Which the statements are executed Two types: simple alternative if compound alternative if...else
3
Single Alternative Decision
An action is taken if the condition is true, otherwise the control goes to the next statement.
4
Single Alternative Decision
Syntax if (expression) statement If expression is true, statement is executed; otherwise statement is skipped. no ; note: 2 = signs Example: if (stomach == empty) eat a Snickers bar; * *
5
Single Alternative Decision
An expression is any combination of variables, constants, or function calls that evaluate to a value. ex. 5 x + y a = 3 + j N n f(12.3, a, “Yvonne”) Syntax if (expression) statement * *
6
Single Alternative Decision
Syntax if (expression) statement If expression is true, statement is executed; otherwise statement is skipped. no ; note: 2 = signs Example: if (stomach == empty) eat a Snickers bar; eat a marshmallow sunday; * *
7
Single Alternative Decision
Example: if (grade >= 90) cout << “Congratulations!\n”; cout << “Your grade is “ << grade << “.\n";
8
The Compound Statement
Example: if (u > v) { a = 1; b = 2; if ( u > z) x =11; y = 12; } Syntax if (expression) { statement; statement; { statement; statement; } The compound statement is itself a statement. * * * * *
9
if Examples Valid: if (y != 0.0) z = x/y; if (a < b && b < c) { d = a + b + c; cout << "All OK\n"; } Not Valid: if b == a area = a * a; if (a < b) && (b < c) if (a < b) ; Valid But... if (a < b) ; * *
10
if Problems Using = in place of ==
What is the difference between these two? if (toss == 7) cout << “You win the bet.”; if (toss = 7) cout << “You win the bet.”;
11
Compounding 2 if Statements
if (j < k) { min = j; cout << “the smaller number is “ << min; } if (j < k) cout << "j is smaller than k\n"; More Efficient: if (j < k) { min = j; cout << “the smaller number is “ << min; cout << "j is smaller than k\n"; } * *
12
Double Alternative Decision
An action (or set of actions) is taken if the condition is true, another action (or set of actions) is taken if the condition is false, then the control goes to the next statement. if ... else is the typical double alternative. * *
13
The if-else Statement Syntax if (expression) statement1 else
If expression is nonzero then statement1 is executed and statement2 is skipped. If expression is zero statement1 is skipped and statement2 is executed.
14
if ... else Examples if (stomach == empty) { eat a pizza;
eat a Snickers bar; } else eat a salad; if ( j < k ) { min = j; k = k * 3; } else { min = k; j = j * 3; * *
15
Interactive Program Finding the minimum of three values
16
Finding the Minimum of Three Values
int x, y, z, min; cout << “Input three integers: “; cin >> x >> y >> z; if (x < y) min = x; else min = y; if (z < min) min = z; cout << “The minimum value is “ << min << ‘\n’; * * *
17
Interactive Program Finding the minimum of three values
Output: Input three integers: The minimum value is -12 _ * *
18
The if-else Statement Syntax if (expression) if (a > b) statement1
max = a; else max = b; expression1 ? expression2 : expression3 max = (a > b) ? a : b; * * *
19
Nested if Statements A nested if statement is an if statement that is included within another if statement. Syntax if (expression1) { if (expression2) statement }
20
Nested if Example if (number == secretnumber) cout << “You guessed it!”; if (number != secretnumber) { cout << “Sorry, that’s not the number.\n”; if (number > secretnumber) cout << “You guessed too high.\n”; else cout << “You guessed too low.\n”; } * *
21
Chained if...else Example
2 statement else if (expression2) statement else if (expressionN) statementN 8 else last statement 10 next statement Syntax if (expression1) *
22
Chained if...else Example
if (total >=90) grade = ‘A’; else if (total >= 80) grade = ‘B’; else if (total >= 70) grade = ‘C’; else if (total >= 60) grade = ‘D’; else grade = ‘E’; next statement * *
23
The Dangling else if (avg >= 60.0) if (avg < 70.0)
cout << “Passing, but marginal”; else cout << “Failing”; if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”; * *
24
The Dangling else if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”;
25
AND vs. OR if( (rel == 'S') && (rel == 'M') && (rel == 'F') )
cout << "\nImmediate family.\n"; if( (rel != 'S') && (rel != 'M') && (rel != 'F') ) { cout << "\nNot immediate family,\n"; cout << " but a close relation.\n"; }
26
Random Numbers #include<stdlib.h> // defines rand() & srand()
#include<time.h> // defines time() in main(): srand(time(NULL)); l l l num1 = 1 + rand() % 3; num1 = 1 + rand() % 6; num1 = 6 + rand() % 5; how many numbers 1, 2, 3 1, 2, 3, 4, 5, 6 6, 7, 8, 9, 10 starting number * * *
27
The switch Statement Similar to if statements
Can list any number of branches Used in place of nested if statements Avoids confusion of deeply nested ifs
28
The switch Statement Syntax switch (expression) no ; use : {
case value1: statement1; break; case value2: statement2; · · · case valuen: statementn; default: statement; } no ; use : * *
29
The switch Statement Syntax switch (expression) no ; use : {
case value1: statement1; break; case value2: statement2; · · · case valuen: statementn; default: statement; } no ; use :
30
The switch Statement switch (let_grd) { case ‘A’:
cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; case ‘C’: cout << “Grade is between 70 & 79”; break; cont.
31
The switch Statement case ‘D’:
cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; default: cout << “You entered an invalid grade.”; } next statement
32
The switch Statement switch (let_grd) { case ‘A’:
cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; case ‘C’: cout << “Grade is between 70 & 79”; case ‘D’: cout << “Grade is between 60 & 69”; case ‘E’: cout << “Grade is between 0 & 59”; default: cout << “You entered an invalid grade.”; }
33
The break Statement switch (let_grd) { case ‘A’:
cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; case ‘C’: cout << “Grade is between 70 & 79”; case ‘D’: cout << “Grade is between 60 & 69”; case ‘E’: cout << “Grade is between 0 & 59”; default: cout << “You entered an invalid grade.”; }
34
The break Statement switch (let_grd) { case ‘A’:
case ‘B’: cout << “Good Work”; break; case ‘C’: cout << “Average Work”; break; case ‘D’: case ‘E’: cout << “Poor Work”; }
35
The break Statement switch (let_grd) { case ‘A’: case ‘a’: case ‘B’:
case ‘b’: cout << “Good Work”; break; case ‘C’: case ‘c’: cout << “Average Work”; break; etc.
36
The switch Statement * * * * Menu * * * * 1. NY Yankees 2. Orioles
3. Dodgers Choose either 1, 2, or 3:
37
The switch Statement switch (choice) { case 1:
cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } What will be the output?
38
The switch Statement What will be the output when the user enters 1
World ChampsGood GuysDa Bums 2 Good GuysDa Bums 3 Da Bums 4 skips the switch * * * * * * * *
39
The switch Statement switch (choice) switch (choice) { { case 1:
cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } switch (choice) { case 1: cout << “World Champs”; break; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; default: cout << “Enter a 1, 2, or 3”; } *
40
Common Errors Using = in place of == Improper braces in nested ifs
Too deeply nested ifs Missing break statements in switch the statement Copyright © by Freedom TLC, Inc.
41
Debugging Syntax errors vs. Logic error
Prevention - plan first! Valuation tables Display values C++ Debugger Copyright © by Freedom TLC, Inc.
42
“I discovered I always have choices, and sometimes it’s only a choice of attitude”
Judith M. Knowlton Copyright © by Freedom TLC, Inc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.