10 Selection - Making Decisions.pptx - Topic 10 Selection Making Decisions 1 Making Decisions Remember that the selection control structure allows you

10 Selection - Making Decisions.pptx - Topic 10 Selection...

This preview shows page 1 - 16 out of 58 pages.

Topic 10 Selection - Making Decisions 1
Image of page 1
Making Decisions Remember that the selection control structure allows you to choose between two or more alternatives. It allows you to make decisions. Decisions are represented by two values: true false For example, a question such as “Is x greater than y?” gives an answer true (yes) or false (no). 2
Image of page 2
Making Decisions The values true and false represent logical data values. C uses data type int to represent logical data values true and false. The logical values are stored in computer memory as 0 for false and 1 for true. Note: Any non-zero value represents true in C. C++ has a data type called bool (for Boolean type) with values true and false . 3
Image of page 3
Logical Expressions C/C++ has 4 relational operators for creating logical expressions: Relationa l Operator Meaning Logical Expression Example < Less than x < y > Greater than x > y <= Less than or equal to x <= y >= Greater than or equal to x >= y 4
Image of page 4
Logical Expressions C/C++ has 2 equality operators for creating logical expressions: Equality Operato r Meaning Logical Expression Example == Equal to x == y != Not equal to x != y 5
Image of page 5
Logical Expressions C/C++ has 3 logical operators for combining logical expressions into more complex logical expressions: The logical operators can be explained using truth tables . Operato r Meanin g Logical Expression Example && And x < y && a > b || Or x < y || a > b ! Not ! (x < y ) 6
Image of page 6
Logical Expressions Truth table for && (And) operator: operan d1 operand 2 operand1 && operand2 true true true true false false false true false false false false 7
Image of page 7
Logical Expressions Truth table for || (Or) operator: operan d1 operand 2 operand1 || operand2 true true true true false true false true true false false false 8
Image of page 8
Logical Expressions Truth table for ! (Not) operator: operan d1 !operand1 true false false true 9
Image of page 9
Logical Expressions Note that logical expressions with the ! operator can be written without it: !(x > y)is the same as (x <= y) !(x < y)is the same as (x >= y) !(x <= y) is the same as (x > y) !(x >= y) is the same as (x < y) !(x == y)is the same as (x != y) !(x != y) is the same as (x == y) 10
Image of page 10
DeMorgan’s Rule 11
Image of page 11
DeMorgan’s Rule DeMorgan’s rule : 1. ! ( expr1 && expr2 ) is written as ( !expr1 || ! expr2 ) 2. ! ( expr1 || expr2 ) is written as ( !expr1 && ! expr 2 ) For the logical expression: ! ( status == ' S ' && age > 25 ) we write it as: ( !(status == ' S ' ) || !(age > 25) ) => ( (status != ' S ' ) || ( age <= 25 ) ) 12
Image of page 12
Operator Precedence Operator Precedenc e ( ) (parenthetical expressions) highest function calls ! + - & (unary operators) * / % + - < <= >= > == != && || = lowest 13
Image of page 13
Operator Precedence Example 1: No . Logical Expression Evaluation 1 . !flag Þ !F Þ T 3.0 x 4.0 y 2.0 z F flag T is true and F is false 14
Image of page 14
Operator Precedence Example 2: No .
Image of page 15
Image of page 16

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture