Upload
zoe-barton
View
214
Download
0
Embed Size (px)
Citation preview
Selection Statements
• Make a decision based on conditions
• Allows the computer to be intelligent
Testing for Conditions
• Compare two numbers for equality, less than, or greater than
<expression> <relational operator> <expression>
x > 0 greater thanx < y less thanx >= 2 greater than or equal tox <= 3 less than or equal tox == 4 equalsx != y not equals
Boolean Values
• Type bool has two possible values, true or false
• In C++, bool is the same as int, and true is really 1 or any positive value whereas false is really 0
• The names bool, true, and false exist just for the programmer’s convenience
Evaluating a Comparison
• In C++, comparisons evaluate to 1 or 0, meaning true or false
• Comparisons are also called Boolean expressions
cout << 5 < 10; // Displays 1
cout << 10 == 10; // Displays 1
cout << 10 != 10; // Displays 0
= and ==
• In C++, == means equals whereas = means assignment
// Assume x has the value 10
cout << x == 10; // Displays 1
cout << x = 10; // Sets the value of// x to 10 and displays// 10
Priority of Operators
Expression Priority( ) 1*, /, % 2+, - 3==, <, >, <=, >=, != 4
x + y < 3 + z // First arithmetic, then comparison
(x + y) < (3 + z) // Add parentheses for clarity
Selection Statements
// absolute value
if (x < 0) x = -x;
// Guard against division by 0
if (x == 0) cout << “Error: attempt to divide by 0;else cout << y / x;
Syntax of Selection Statements
if (<Boolean expression>) // One-way decision <statement>
if (<Boolean expression>) // Two-way decision <statement>else <statement>
With Compound Statements
if (<Boolean expression>){ <statement 1> . <statement n>}
if (<Boolean expression>) <statement>else{ <statement 1> . <statement n>}
Behavior of Selection Statements
?
statement
true
?
statement
false
true
false
statement
if (<Boolean expression>){ <statement 1> . <statement n>}
if (<Boolean expression>) <statement>else{ <statement 1> . <statement n>}
Logical Operators andCompound Boolean Expressions
// see if number is within a range
if (x >= 1 && x <= 6) cout << “Number is between 1 and 6”;
// see if number is not within a range
if (x < 1 || x > 6) cout << “Number is not between 1 and 6”;
&& and|| or! not
All Booleanexpressionsreturn 1 or 0in C++
Truth Tables for Logical Operators
A B A || B A && Btrue true true truetrue false true falsefalse true true falsefalse false false false
A ! Atrue falsefalse true
Priority of the Operators
Expression or Operation Priority( ) Evaluate from inside out! Evaluate from left to right
*, /, % Evaluate from left to right+, - Evaluate from left to right<, <=, >, >=, ==, != Evaluate from left to right&& Evaluate from left to right|| Evaluate from left to right
Multiway Selection
// Run a drawing function based on a character command
if (letter == ‘C’ || letter == ‘c’) drawCube();else if (letter == ‘T’ || letter == ‘t’) drawTriangle();else if (letter == ‘R’ || letter == ‘r’) drawRectangle();else cout << “Unrecognized commmand”;
Switch Statements
// Run a drawing function based on a character command
switch (letter){ case ‘C’: case ‘c’: drawCube(); break; case ‘T’: case ‘t’: drawTriangle(); break; case ‘R’: case ‘r’: drawRectangle(); break; default: cout << “Unrecognized commmand”;}