Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Logical and Relational Expressions and Logical.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Logical and Relational Expressions and Logical."— Presentation transcript:

1 © Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Logical and Relational Expressions and Logical and Relational Operators

2 © Janice Regan, CMPT 128, Control Structures  Control structures are used to manage the order in which statements in computer programs will be executed  Three different approaches  In sequence  Branching  Looping

3 © Janice Regan, CMPT 128, Control Structures  Branch: Altering the flow of program execution by making a selection or choice  Loop: Altering the flow of program execution by repetition of a particular block of statement(s)

4 Branching  One way selection  Two way selection  Multiple way selection © Janice Regan, CMPT 128,

5 4 One-way selection  Simplest form of a branch.  Evaluate a condition that can be True or False  If the condition is true a series of actions are executed  If the condition is false that series of actions are not executed  C and C++ implementation:  if statement

6 © Janice Regan, CMPT 128, Two-way selection  Evaluate a condition that can be True or False  One “Set of things to do” if the condition is true,  A different “Set of things to do” if the condition is false  C and C++ implementation:  if-else statement

7 © Janice Regan, CMPT 128, Multiple-way selection  If condition1 is true the 1 st series of actions is completed  If condition1 is false and condition2 is true the 2 nd series of actions is completed  If condition1 and condition2 are false and condition3 is true the 3 rd series of actions is completed  …  If all conditions are false the final series of actions is completed  Implemented in C++ as an if-elseif-else statement

8 © Janice Regan, CMPT 128, Selection: decision statements Each decision statement contains a condition  The condition is an expression with a logical value (true or false)  The condition is a Boolean expression  A relational expression (a type of logical expression)  Another type of logical expression  A Boolean variable or constant

9 © Janice Regan, CMPT 128, Relational Expressions  A type of logical expression  Combines two numbers, strings, characters to give a value of true or false  A simple relational expression is  Two numerical values combined using a binary relational operator  A more complex relational expression is  A combination of simple relational expressions

10 © Janice Regan, CMPT 128, Binary Relational Operators C, C++  greater than  >= greater than or equal to  == equal to  !=not equal to  Evaluated left to right Binary Equality Operators in C, C++

11 © Janice Regan, CMPT 128, Other logical expressions Other types of logical expression include  Two logical variables / constants combined with a binary logical operator  One logical variable or constant  One logical variable or constant operated on by a unary logical operator  Two relational expressions combined with a binary logical operator  …  Any more complex logical expression

12 © Janice Regan, CMPT 128, Binary Logical Operators C, C++  &&Logical AND  ||Logical OR  !Not  Evaluated left to right  Arguments of logical operators have values of true or false Unary Logical Operators C, C++

13 © Janice Regan, CMPT 128, C and C++ Boolean values  In C++ Boolean variables or constants  Have type bool and value true or false  Have integer values consistent with C  In C Boolean values (true and false) are represented as integers  All non-zero integer values  true  Zero value  false  Newest version of standard C99 has type bool

14 © Janice Regan, CMPT 128, Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 && EXPRESSION2 T T T T F F F T F F F F

15 © Janice Regan, CMPT 128, Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 || EXPRESSION2 T TT T FT F TT F F F

16 © Janice Regan, CMPT 128, Truth Tables ! The ! (Not) operator EXPRESSION1 ! EXPRESSION1 T F F T

17 © Janice Regan, CMPT 128, Precedence of operators C, C++  ( ) []. innermost first  (pre) + - ! ~(unary) (right to left)  * / %  + -  >=  == !=  &&  ||  = += -= *= /= %=(right to left)  ?:

18 © Janice Regan, CMPT 128, Expressions with relational operators  Value of a relational expression (expression including a relational or binary equality operator) is true or false.  Arguments of a relational operator are numerical (or character)  A < CLet A=9, B=5, C=2  A != -C

19 © Janice Regan, CMPT 128, Expressions: relational operators  A < C  Let A=9, B=5, C=2 A C < 9 2 F A C < Value of expression is Boolean: In C++ Boolean (T or F) is represented by a bool type variable.

20 © Janice Regan, CMPT 128, Expressions: relational operators  A * B <= C X <= C  Let A=9, B=5, C=2 * A B X C <= F A B C * <= Value of expression is Boolean:

21 © Janice Regan, CMPT 128, Expressions: logical operators  Value of a relational expression or logical expression (expression including a logical operator) is true or false.  Operands of the logical operator are true or false  Therefore relational and logical expressions can be operands of logical expressions Let A=9, B=5, C=2 Then  (C < B) && A ^ C  !(A < B) && B

22 © Janice Regan, CMPT 128, Truth Tables ^ The ^ (Bitwise Exclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 ^ EXPRESSION2 T TF T FT F TT F F F

23 Other bitwise operators SymbolOperator &bitwise AND binary |bitwise inclusive OR binary ^bitwise exclusive OR binary <>right shift unary ~ bitwise NOT (one's complement) unary © Janice Regan, CMPT 128, Bitwise operators compare the first bit of each variable to get the first bit of the answer, then the second bit of each variable to get the second bit of the answer, …

24 Why is bitwise different  Historically C used non-zero for true and zero for false, and had no Boolean type (C99 and newer has Boolean)  Anything that could be represented as an integer could be compared  If we bitwise AND two true variables X=1010 and Y=0101 it is possible to get the answer false (X&Y = 0000 )  If we AND two true variables we will always get the answer true (X &&Y = TRUE)  If we allowed only Boolean variables as arguments for logical operators and have 0 for false and 1 for true, bitwise is not different. © Janice Regan, CMPT 128,

25 © Janice Regan, CMPT 128, Expressions: logical operators  Let A=9, B=5, C=2  (C < B) && (A ^ C) X && (A ^ C) X && Y < C B X 2 5 T Y A 9 2 ^ C && T A C < ^ B T Value of expression 1001 ^ 0010 = 1011

26 © Janice Regan, CMPT 128, Expressions: logical operators  Let A=9, B=5, C=2  !(A C !X && A > C Y && A > C Y && Z < A B X 9 5 F Y T && T A B < ! C Value of expression ! > A C Z 9 2 T > &&

27 © Janice Regan, CMPT 128, Precedence Examples  Arithmetic before logical  Short-circuit evaluation  (x >= 0) && (y > 1)  Be careful with increment operators! (x > 1) && x

28 © Janice Regan, CMPT 128, Expressions: logical operators  Let A=9, B=5, C=2  !(A C !X || A > C Y || A > C Short Circuit < A B X 9 5 F Y T || T A B < ! C Value of expression !

29 © Janice Regan, CMPT 128, Expressions: logical operators  Let A=9, B=5, C=2  A < B && B < C++ X && B < C++ Short Circuit (increment not evaluated!) < A B X 9 5 F && F A B < C Value of expression

30 © Janice Regan, CMPT 128, Selection Structure  Use a decision statement when an action is to be taken only if a particular condition holds  The condition which must hold may be logical or relational expression or a Boolean variable. The value of the condition must be true or false  Each possible path through a condition statement will contain a sequence of steps to be executed  The condition and the sequences of steps that are executed for each outcome of the condition statement form a selection structure.  A selection structure is a type of control structure

31 © Janice Regan, CMPT 128, Flowcharts  Flowcharts use some basic symbols  To start or end a function  To contain calculations  To make decisions  To connect different parts of an algorithm

32 © Janice Regan, CMPT 128, Flowchart: one way selection  Write the condition that needs to be satisfied in the decision box (diamond).  Based upon the value of the condition (boolean T or F) choose what to do next  The sequence of statements to be executed if the condition is true is placed in the box condition Statement 1; Statement n; T F ⋮ C and C++ implementation if statement

33 © Janice Regan, CMPT 128, One-way selection  Example if statement in C and C++: setFlagOrderBoxes = 0; if (numberOfBoxes < minimumBoxInventory) setFlagOrderBoxes = 1; setFlagOrderBags = 0; // always executed  setFlagOrderBags is always set to 0, even if the condition in the if statement is false

34 © Janice Regan, CMPT 128, C++ Compound/Block Statement  Only one statement following the if statement is part of the if control structure  What if we need more than one statement done if the condition is true?  Must use a block of statements (also called a compound statement)  C++ uses { }, to contain all the statements in a block of statements

35 © Janice Regan, CMPT 128, One way selection: sample  Do a series of actions only if a given condition holds  If the condition does not hold skip the actions if (myScore > yourScore) { cout << “My score was higher than yours “; difference = myScore – yourScore; cout << “I got “ << difference << “ more points than you did” ; }

36 © Janice Regan, CMPT 128, Course coding standard  When writing an if statement you should always use a block  Always use the { } even if the block contained within the { } includes just one statement

37 © Janice Regan, CMPT 128, Justification  WHY use the { } if there is only one statement in the block?  As your code evolves it is common to add statements (functionality) within a decision statement.  When you add statements and forget to add the {} to indicate the extent of the block unexpected things happen. The resulting problems can be difficult to find

38 © Janice Regan, CMPT 128, Course coding standard  To make your program easier to read, understand, debug, and maintain use the following approach  Place the brackets { } that delimit each block (compound statement) each on their own line  Indent each of the statements inside the block a given number (you choose and use consistently) of spaces farther from the left of the page than the brackets.

39 © Janice Regan, CMPT 128, Two-way selection  Another type of simple branch structure  Consider two different series of actions  If a condition is true the first series of actions is completed  If the same condition is false the second series of actions is completed

40 © Janice Regan, CMPT 128, Flowchart: two way selection  Based upon the value of the condition (boolean T or F) choose what to do next  The sequence of statements to be executed if the condition is true is placed in the box at the right  The sequence of statements to be executed if the condition is false is placed in the box below the condition condition Statement 1; Statement n; T F Statement 1; Statement n; ⋮ ⋮ Implemented in C and C++ as an if-else statement

41 © Janice Regan, CMPT 128, Example of two-way selection  Example if-else statement in C and C++: if (examScore > 50) myCourseGrade = “PASS”; else myCourseGrade = “FAIL”;  NOTE: Only one statement follows the if, and one statement follows the else

42 © Janice Regan, CMPT 128, Common Error in C++  Operator = is the "assignment" operator  Operator == is the logical operator to test equality of two variables  These two operators are VERY different  Avoid the following common error: if (x = 12) Do_thing1; else Do_thing2;

43 © Janice Regan, CMPT 128, What actually happens Common Error in C and C++  Boolean variables have values true or false in C++.  In earlier versions of C there was no Boolean type, true and false were represented by integers 0 is false Non zero is true  C++ is backward compatible with C, so recognizes the integer representation used in C  So if(x=12) evaluates to true (12) and thing1 is done (regardless of the original value of x) The value 12 is assigned to x (the value of x becomes 12) if(12) is the same as if(true) and evaluates to true

44 © Janice Regan, CMPT 128, Compound/Block Statement  Must use a compound statement { } to include more than one statement following the if or more than one statement following the else  Course Coding Standard  Each block (after the if and after the else) should have block statement using { } even if the block contains just one statement

45 © Janice Regan, CMPT 128, Two way selection in C++  Complete one of two possible series of actions  First series of actions is complete if condition is true  Second series of actions is completed if condition is false if (condition) {//Series of actions to be taken when the condition is true action 1; ⋮ action n; } else { // Series of actions to be taken when the condition is false action 1; ⋮ action n; }

46 © Janice Regan, CMPT 128, Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T ⋮ ⋮ ⋮

47 © Janice Regan, CMPT 128, Example of Multiple selection  Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 60) myCourseGrade = “C”; else myCourseGrade = “FAIL”;

48 © Janice Regan, CMPT 128, Multiple-way selection  If condition1 is true the 1 st series of actions is completed  If condition1 is false and condition2 is true the 2 nd series of actions is completed  If condition1 and condition2 are false and condition3 is true the 3 rd series of actions is completed  …  If all conditions are false the final series of actions is completed  Implemented in C++ as an if-elseif-else statement

49 © Janice Regan, CMPT 128, Multiple Selections (else if) if (condition1) {// Series of actions to be taken when condition 1 is true action 1; action n; } else if (condition 2) { // actions to be taken when condition 1 is false and condition2 is true action 1; action n; } else { // Series of actions to be taken when condition 1and condition 2 are false action 1; action n; }

50 © Janice Regan, CMPT 128, Course coding standard for ifs  Each if, elseif, and else should put all statement to be executed for the case in a block statement using { }. A block statement should be used even if the block contains just one statement.  Place the brackets { } that delimit each block (compound statement) each on their own line  Indent each of the statements inside the block a specified number of spaces farther from the left of the page than the brackets.


Download ppt "© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Logical and Relational Expressions and Logical."

Similar presentations


Ads by Google