Electrical Engineering - Chapter 5: Decision Making

Type of logical expression Produces result of true or false Compares values of two arithmetic expressions

ppt25 trang | Chia sẻ: thuongdt324 | Lượt xem: 534 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 5: Decision Making, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 5 – Decision MakingSimple if StatementCapable of making decisionsRelational expression evaluatedEvaluated as true or falseLesson 5.1if (relational expression) { statement(s); }Block of statements done if results true, skipped if results false!Relational ExpressionsType of logical expressionProduces result of true or falseCompares values of two arithmetic expressionsLesson 5.1left_operand relational_operator right_operandRelational Operators greater than>= greater than or equal to!= not equalLesson 5.1Simple If/Else StatementProvides block of statements to be executed for either true OR falseBraces optional if only one statement in blockLesson 5.2if (expression) { statement1a; statement1b; }else { statement1a; statement1b; }if TRUEif FALSEif Examplesx = 3;if (x b) { ans = 10;} else { ans = 25;}a > b ? (ans = 10) : (ans = 25) ;conditional expressionif true, execute this statementif false, execute this statementans = (a > b) ? 10 : 25 ;Lesson 5.2Nested if-else StatementsLesson 5.3if (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 OperatorsExclamation mark !NOT (negation)unaryTwo ampersands &&AND (conjunction)binaryTwo vertical pipes ||OR (inclusive disjunction)binaryLesson 5.4Logical NOT OperatorReverses result of relational expressionExample: ! (x == y)Lesson 5.4Evaluate relational expression, does 3 equal 7?3x7y! ( false ) so negates to true77! ( true ) so negates to falseLogical AND OperatorCombines two relational expressionsCombined expression true only if BOTH expressions are trueLesson 5.4expression1 && expression2 true false falsefalse && truefalse && falsetrue && truefalsefalsetrueLogical OR OperatorCombines two relational expressionsCombined expression false only if BOTH expressions are falseLesson 5.4expression1 || expression2 true false truefalse || truefalse || falsetrue || truetruefalsetrueValues of Relational ExpressionsResult of relational expressionFalse, C++ compiler gives zeroTrue, C++ compiler gives oneValue of relational expressionZero, result is falseNonzero, result is trueany number other than zero, including negativesLesson 5.5Precedence of OperatorsLesson 5.5 ( ) Parentheses L to R 1 ++, -- Postincrement L to R 2 ++, -- Preincrement R to L 3 ! Logical NOT L to R 3 +, - Positive, negative L to R 3 *, /, % Multiplication, division L to R 4 +, - Addition, subtraction L to R 5 =, >, b || b > c && a == b)x = ((a > b) || (b > c) && (a == b))x = (TRUE || (FALSE && FALSE))Groupx = ((4 > -2) || (-2 > 0) && (4 == -2))Groupx = (TRUE || FALSE)x = (TRUE)x = 1Lesson 5.5Example of Single Variablelogical valueFalse if value is 0True if value is nonzeroLesson 5.5if (c) statement1;else statement2;Statement2 executed if c has value of zero, statement1 is executed if c has any other value but zero.if-else-ifShifts program control, step by step, through series of statement blocksControl stops at relational expression that tests trueLesson 5.6if-else-if FormLesson 5.6if (relational_expression_1) { statement_block_1 }else if (relational_expression_2) { statement_block_2 } . . .else if (relational_expression_n-1) { statement_block_n-1 }else { statement_block_n } false else false else true This statement block wouldbe executed!switch Control StructureConstructed like if-else-ifUsed to transfer controlCan nest switch control structuresKeyword switch followed by expressionLesson 5.6switch (expression) { statement block }Must use parenthesesMust result in integer type valueswitch Statement Block SyntaxLesson 5.6switch (expression) {case constant1: statement1a statement1b case constant2: statement2a statement2b default: statements } Keyword case only used in switch statementcase used to form labelcase label is constant followed by colonConstant1, constant2, etc must be integer expressionsConstant expressions must be uniquedefault optional, used when no match is foundbreak StatementTerminates execution of switch statementSends control to point of closing braceUsually last statement for each caseIf no break, then statements in next case executedswitch (expression) {case constant1: statement1a break; case constant2: statement2a break; default: statements } bool Data TypeNamed after mathematician George BooleCan contain one of only two values (0 or 1)true or false, fail or pass, off or onDeclare by using keyword boolExample: bool salty, hard, acidic;Assign valueExample: acidic = (pH < 7);Lesson 5.7bool VariablesAssign keywords true or false good_taste = trueAssign bools to int variablesint i; i = good_tastePrint bool variables values using coutPrints 1 for true or 0 for falseboolalpha manipulator gives words true or falseLesson 5.7SummaryCreate simple if and if-else statementsCreate relational expressionsUtilize logical operatorsInterpret the order of precedence for all operators (arithmetic, relational, and logical)Create multiple decision statements (if-else-if or switch statements)Utilize bool data typeLearned how to:
Tài liệu liên quan