tailieunhanh - Lecture Programming in C++ - Chapter 5: Decision making
On completion of this chapter students will learned how to: Create simple if and if-else statements, create relational expressions, utilize logical operators, interpret the order of precedence for all operators (arithmetic, relational, and logical), create multiple decision statements (if-else-if or switch statements), utilize bool data type. | Chapter 5 – Decision Making Simple if Statement Capable of making decisions Relational expression evaluated Evaluated as true or false Lesson if (relational expression) { statement(s); } Block of statements done if results true, skipped if results false! Relational Expressions Type of logical expression Produces result of true or false Compares values of two arithmetic expressions Lesson left_operand relational_operator right_operand Relational Operators greater than >= greater than or equal to != not equal Lesson Simple If/Else Statement Provides block of statements to be executed for either true OR false Braces optional if only one statement in block Lesson if (expression) { statement1a; statement1b; } else { statement1a; statement1b; } if TRUE if FALSE if Examples x = 3; if (x greater than >= greater than or equal to != not equal Lesson Simple If/Else Statement Provides block of statements to be executed for either true OR false Braces optional if only one statement in block Lesson if (expression) { statement1a; statement1b; } else { statement1a; statement1b; } if TRUE if FALSE if Examples x = 3; if (x b) { ans = 10;} else { ans = 25;} a > b ? (ans = 10) : (ans = 25) ; conditional expression if true, execute this statement if false, execute this statement ans = (a > b) ? 10 : 25 ; Lesson Nested if-else Statements Lesson if (outer) { if (inner) { } else { } } else { } If outer is true, this block executed. If outer is false, this block executed. Executed if inner is true. Executed if inner is false. Three Logical Operators Exclamation mark ! NOT (negation) unary Two ampersands && AND (conjunction) binary Two vertical pipes || OR (inclusive disjunction) binary Lesson Logical NOT Operator
đang nạp các trang xem trước