You are on page 1of 21

Logical Data in C

Data is called logical if it conveys a value of true or false.


If a data value is zero, it is considered false. If a value is non-zero, it is considered true.

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

Logical Operator Examples


int a = 7, b=0; a && b a || b !a && b !a || b a && !b !(a || b) !(a && b)
// prints 0 // prints 1 // prints 0 // prints 0 // prints 1 // prints 0 // prints 1

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 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

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

Dangling else problem


Once you start nesting ifelse statements, you can encounter 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 your actual intent.
12

Dangling else problem


Write the code for the following pseudocode:
if x is greater to 5 if x is less than 10 print that the value is between 5 and 10 else print that the value is less than 5

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

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 one possible answer or outcome

15

The case construct


Case structuresimply provides a convenient alternative to using a series of decisions when you must 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

16

The switch statement cont.

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

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

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

You might also like