Amazing C++ If, Else & Switch Statements
Amazing C++ If, Else & Switch Statements
Control Structures
1
2.1 Introduction
2
2.2 Algorithms
Computing problems
Solved by executing a series of actions in a
specific order
Algorithm is a procedure determining
Actions to be executed
Order to be executed
Example: recipe
Program control
Specifies the order in which statements are
executed
3
2.3 Pseudocode
Pseudocode
Artificial, informal language used to develop
algorithms
Similar to everyday English
Not executed on computers
Used to think out program before coding
Easy to convert into C++ program
Only executable statements
No need to declare variables
4
2.4 Control Structures
Sequential execution
Statements executed in order
Transfer of control
Next statement executed not next one in sequence
3 control structures (Bohm and Jacopini)
Sequence structure
Programs executed sequentially by default
Selection structures
if, if/else, switch
Repetition structures
while, do/while, for
5
2.4 Control Structures
C++ keywords
Cannot be used as identifiers or variable names
C++ Keywords
6
2.4 Control Structures
Flowchart
Graphical representation of an algorithm
Special-purpose symbols connected by arrows (flowlines)
Rectangle symbol (action symbol)
Any type of action
Oval symbol
Beginning or end of a program, or a section of code (circles)
Single-entry/single-exit control structures
Connect exit point of one to entry point of the next
Control structure stacking
7
2.5 if Selection Structure
Selection structure
Choose among alternative courses of action
Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
If the condition is true
Print statement executed, program continues to next
statement
If the condition is false
Print statement ignored, program continues
Indenting makes programs easier to read
C++ ignores whitespace characters (tabs, spaces, etc.)
8
2.5 if Selection Structure
if ( grade >= 60 )
cout << "Passed";
9
2.5 if Selection Structure
10
2.6 if/else Selection Structure
if
Performs action if condition true
if/else
Different actions if conditions true or false
Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
11
2.6 if/else Selection Structure
12
2.6 if/else Selection Structure
Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
13
Importance of Curly Braces
14
Exam Grade Flowchart int examGrade, quizGrade;
if (examGrade < 60)
cout << “We have a problem” << endl;
if (quizGrade < 10)
cout << “We have a real problem” << endl;
else
cout << “Ok”;
true
examGrade < 60
false true
quizGrade < 10
15
Writing Cases
16
Putting it all together
examGrade < 60 quizGrade < 10 Action
Case 1 true false “We have a problem”
Case 2 true true “We have a problem” and
“We have a real problem”
Case 3 false true/false “Ok”
int examGrade,
int examGrade,quizGrade;
quizGrade;
if
if (examGrade
(examGrade< <60)
60) {
System.out.println(“We have a problem”);
cout << “We have a problem” << endl;
if (quizGrade < 10)< 10)
if (quizGrade
System.out.printl(“We have a real problem”);
cout << “We have a real problem” << endl;
else
} System.out.println(“Ok”);
else
cout << “Ok”;
17
boolean Operators
18
Expression Combinations
The && (and) operator
operand1 operand2 operand1 && operand2 Let age = 17
true true true
true false false
Let age = 16
false true false
false false false
Let age = 12
19
Expression Combinations
The || (or) operator
operand1 operand2 operand1 || operand2
true true true
true false true
false true true
false false false
20
Playing Cards
21
Prints a Card Name
22
2.16switch Multiple-Selection
Structure
switch
Test variable for multiple values
case value2:
case value3: // taken if variable == value2 or == value3
statements
break;
23
2.16switch Multiple-Selection
Structure
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
24
Converting if/else to a switch
switch (rank)
{
if (rank == JACK) case JACK:
cout << "Jack"; cout << "Jack";
break;
else if (rank == QUEEN) case QUEEN:
cout << "Queen"; cout << "Queen";
break;
else if (rank == KING; case KING:
cout << "King"; cout << "King";
break;
else if (rank == ACE) case ACE:
cout << "Ace"; cout << "Ace";
break;
else default:
cout << rank; cout << rank;
}
25