Professional Documents
Culture Documents
Logical operators
The relational operators are the Logical AND, Logical OR and and the NOT operator.
Value 1 T T F F
Value2 T F T F
&& T F F F
|| T T T F
! (Value 1) F F T T
Relational operators
Six relational operators support logical relationships. They are all binary operators that accept two operands and compare them. The value of an expression involving a relational operator is either true or false. The operators are:
< <= > >= == != less than less than or equal greater than greater than or equal equal not equal higher priority than the other two
Examples
x = 1; y = 4; z = 14;
x <= 1 && y == 3 x <= 1 || y == 3 !( x > 1 ) !x > 1 !( x <= 1 || y == 3 ) x >= 1 && y == 3 || z < 14 0 1 1 0 0 0
5
Simplifying Expressions
Two-way Selection
The basic decision statement in the computer is the two-way selection. The decision is described to the computer as a conditional statement that can be answered either true or false. If the answer is true, one or more action statements is executed. If the answer is false, then a different action or set of actions is executed. Regardless of which set of actions is executed, the program continues with the next statement, after the selection.
if statement
if is a mechanism for executing a statement/s conditionally.
if statement cont.
Usually the expression in an if statement is a relational, equality or logical expression.
if j < k let min = j if j < k print j is smaller than k\n
9
if statement cont.
if j < k then let min = j print j is smaller than k else print j is larger than k
10
Nested if statements
if expression 1 then action 1 if expression 2 then action 2 else action 3 else action 4
11
This might result in your program not matching your actual intent.
12
13
Range Checks
Range CheckCompare a variable to a series of values between limits To perform a range check, make comparisons using either the lowest or highest value in each range of values you are using
14
15
16
Switch works by evaluating expression, passing control to the case labeled with its value (or the default) and executing the cases statement-list. The case-labels have to have expressions that the compiler can evaluate to an integer constant, and all the case-labels have to be unique.
17
18
Summary
Every decision you make in a compute program involves evaluating a Boolean expression For any two values that are the same type, you can use the logical comparison operators to decide whether the two values are equal, the first is greater than the second, or the first is less than the second An AND decision occurs when two conditions must be true in order for a resulting action to take place In an AND decision, first ask the question that is less likely to be true Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator
19
Summary
When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision An OR decision occurs when you want to take action when one or the other of two conditions are true In an OR decision, first ask the question that is more likely to be true Most programming languages allow you to ask two or more questions in a single comparison To perform a range check, make comparisons with either the lowest or highest value in each range of values you are using
20
Summary
Common errors that occur when programmers perform range checks include asking unnecessary and previously answered questions The case structure provides a convenient alternative to using a series of decisions when you must make choices based on the value stored in a single variable A decision table is a problem-analysis tool that consists of conditions, possible combinations of Boolean values for the conditions, possible actions based on the conditions, and the action that corresponds to each Boolean value of each condition
21