15
Selection Statements • Make a decision based on conditions • Allows the computer to be intelligent

Selection Statements Make a decision based on conditions Allows the computer to be intelligent

Embed Size (px)

Citation preview

Page 1: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

Selection Statements

• Make a decision based on conditions

• Allows the computer to be intelligent

Page 2: 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

Page 3: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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

Page 4: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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

Page 5: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

= 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

Page 6: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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

Page 7: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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;

Page 8: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

Syntax of Selection Statements

if (<Boolean expression>) // One-way decision <statement>

if (<Boolean expression>) // Two-way decision <statement>else <statement>

Page 9: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

With Compound Statements

if (<Boolean expression>){ <statement 1> . <statement n>}

if (<Boolean expression>) <statement>else{ <statement 1> . <statement n>}

Page 10: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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>}

Page 11: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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++

Page 12: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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

Page 13: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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

Page 14: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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”;

Page 15: Selection Statements Make a decision based on conditions Allows the computer to be intelligent

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”;}