Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational operators Logical operators and expressions switch statement – multiple choices
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Relational Operators Operators –Less than < –Less and equal than <= –Greater than > –Greater and equal than >= –Equal to == –Not equal to != Program 4.1
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Logical expressions Initializations Logical expressions
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. if Statements Program 4.2
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Nested if Statements if (condition) { if (condition) { if (condition) { statements; } Program 4.3
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Character Handling Character type header – or Functions for converting characters
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Functions for Testing Characters
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Logical Operators Boolean operators Boolean header file –bool.h Return integer –false (0), true (non-zero) Program 4.4
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. if-else statement char letter = 45; if (isalnum(letter)) cout << “It is a letter or a digit.” << endl; else cout << “it is neither a letter nor a digit.” << endl; Program 4.5
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Nested if-else Statements
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Nested if-else statement
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More coffee and donuts
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Clearer version
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Logical (Boolean) operators Logical AND – && Logical OR – || Logical NOT – ! Program 4.6
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Conditional Operator Ternary operator condition ? experssion1: expression2; E.g. –c = a > b ? a: b; –if (a>b) c = a; else c = b; –loan = (2*income<balance/2) ? (2*income): balance/2); –a < b ? ++i : --i; Program 4.7
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. switch Statement Multiple choices Keywords –switch –case –default –break Program 4.8, 4.9
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Alternative Ways
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Decision Statement Blocks and Variable Scope