View
225
Download
0
Category
Preview:
Citation preview
8/3/2019 Selection Making Decisions
1/21
Logical Data in C
Data is called logical if it conveys avalue of true or false.
If a data value is zero, it is consideredfalse. If a value is non-zero, it is
considered true.
1
8/3/2019 Selection Making Decisions
2/21
2
Logical operators
The relational operators are the Logical AND, LogicalOR and and the NOT operator.
Value 1 Value2 && | | ! (Value 1)
T T T T F
T F F T F
F T F T TF F F F T
8/3/2019 Selection Making Decisions
3/21
Logical Operator Examples
int a = 7, b=0;
a && b // prints 0
a || b // prints 1
!a && b // prints 0!a || b // prints 0
a && !b // prints 1
!(a || b) // prints 0
!(a && b) // prints 1
3
8/3/2019 Selection Making Decisions
4/21
4
Relational operators
Six relational operators support logical relationships.They are all binary operators that accept twooperands and compare them. The value of an
expression involving a relational operator is eithertrue or false.
The operators are:< less than
greater than the other two
>= greater than or equal
== equal
!= not equal
8/3/2019 Selection Making Decisions
5/21
5
Examples
x = 1;
y = 4;
z = 14;
x 1 0
!( x = 1 && y == 3 || z < 14 0
8/3/2019 Selection Making Decisions
6/21
Simplifying Expressions
6
8/3/2019 Selection Making Decisions
7/21
7
Two-way Selection
The basic decision statement in the computer is thetwo-way selection. The decision is described to thecomputer 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 theanswer is false, then a different action or set ofactions is executed. Regardless of which set of
actions is executed, the program continues with thenext statement, after the selection.
8/3/2019 Selection Making Decisions
8/21
8
ifstatement
if is a mechanism for executing a statement/sconditionally.
If expression
action 1
else
action 2
In this case only action 1 or action 2 is executed,
depending on whether the expression is true or false.
8/3/2019 Selection Making Decisions
9/21
9
ifstatement cont.
Usually the expression in an ifstatement is a relational, equality or
logical expression.if j < k
let min = j
if j < kprint j is smaller than k\n
8/3/2019 Selection Making Decisions
10/21
10
ifstatement cont.
if j < k
then
let min = j
print j is smaller than k
elseprint j is larger than k
8/3/2019 Selection Making Decisions
11/21
11
Nested ifstatements
if expression 1
then
action 1if expression 2
then
action 2
elseaction 3
else
action 4
8/3/2019 Selection Making Decisions
12/21
12
Dangling else problem
Once you start nesting ifelse statements, you canencounter the dangling else problem. This problem
is created when there is no matching else for every if.
In some programming languages:
an else is always paired with the most recent unpaired if
This might result in your program not matching youractual intent.
8/3/2019 Selection Making Decisions
13/21
Dangling else problem
Write the code for the following pseudocode:
if x is greater to 5
if x is less than 10print that the value is between 5 and 10
else
print that the value is less than 5
13
8/3/2019 Selection Making Decisions
14/21
Range Checks
Range CheckCompare a variable to aseries of values between limits
To perform a range check, makecomparisons using either the lowest orhighest value in each range of values
you are using
14
8/3/2019 Selection Making Decisions
15/21
Common Errors with Range
Checks Ask one question too many
Path is dead or unreachable
Asking unnecessary questions
Never ask a question is there is only onepossible answer or outcome
15
8/3/2019 Selection Making Decisions
16/21
16
The case construct
Case structuresimply provides a convenientalternative to using a series of decisions when youmust make choices based on the value stored in a
single variable
switch ( expression )
case case-label-1 :
statement-list
case case-label-2 :
statement-list
...
default :
statement-list
8/3/2019 Selection Making Decisions
17/21
17
The switch statement cont.
Switch works by evaluating expression, passingcontrol to the case labeled with its value (or thedefault) and executing the cases statement-list.
The case-labelshave to have expressions that thecompiler can evaluate to an integer constant, and allthe case-labelshave to be unique.
8/3/2019 Selection Making Decisions
18/21
18
The switch statement cont.switch ( operator )
case '+' :// when the value of operator is +
result = op1 + op 2
end +
case '-' :// when the value of operator is -
result = op1 - op 2
end -case '*' :// when the value of operator is *
result = op1 * op 2
end *
case '/' :// when the value of operator is /
if ( op2 != 0.0 )
result = op1 / op 2;end /
default :// when the value of operator is not any of the above
print Unknown operator
result = 0.0
end default
end switch
8/3/2019 Selection Making Decisions
19/21
19
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 valuesare equal, the first is greater than the second, or the first is lessthan the second
An AND decision occurs when two conditions must be true inorder for a resulting action to take place
In an AND decision, first ask the question that is less likely to betrue
Most programming languages allow you to ask two or morequestions in a single comparison by using a logical AND
operator
8/3/2019 Selection Making Decisions
20/21
Summary
When you must satisfy two or more criteria to initiate an event ina program, you must make sure that the second decision ismade 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
8/3/2019 Selection Making Decisions
21/21
Summary
Common errors that occur when programmers perform rangechecks include asking unnecessary and previously answeredquestions
The case structure provides a convenient alternative to using a
series of decisions when you must make choices based on thevalue stored in a single variable
A decision table is a problem-analysis tool that consists ofconditions, possible combinations of Boolean values for theconditions, possible actions based on the conditions, and the
action that corresponds to each Boolean value of each condition
21
Recommended