0% found this document useful (0 votes)
71 views

03 Logical Operators

The document discusses logical operators in programming. It explains that logical AND (&&) requires both conditions to be true, logical OR (||) requires one or both conditions to be true, and logical NOT (!) reverses the meaning of a condition. Examples are given showing how different logical operators evaluate conditions and determine output. Pseudocode and a flowchart are provided to demonstrate logical AND evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

03 Logical Operators

The document discusses logical operators in programming. It explains that logical AND (&&) requires both conditions to be true, logical OR (||) requires one or both conditions to be true, and logical NOT (!) reverses the meaning of a condition. Examples are given showing how different logical operators evaluate conditions and determine output. Pseudocode and a flowchart are provided to demonstrate logical AND evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Algorithm:

Logical Operators
with Sir Beets
What we learned before: Test precisely 1 condition only
– Relational Operators: > >= < <==
– Equality Operators: == !=
Logical
Logical Operators for testing multiple conditions
Operators – Logical AND ( && ) both conditions must be true
– Logical OR ( || ) either or both conditions are true
– Logical Negation ( ! ) reverse the meaning of the condition
1. Input N
2. If ( N > 0 && N%2 == 0 )
2.1 Then display “Positive even number”
2.2 Else display “Negative or an odd number”

N N>0 N % 2 == 0 Display
Example 1 true false Negative or an odd number
0 false true Negative or an odd number
-1 false false Negative or an odd number
-1200 false true Negative or an odd number
3464 true true Positive even number
8 true true Positive even number
17 true false Negative or an odd number
Start Start

N N

Is, false
N>0
Is, false
?
Logical
true
N>0 &&
N%2==0
Operator
Is, false ?
N%2==0
Flowchart
?
true true

Positive Positive
even Negative or Negative or
even
number an odd an odd
number

End End
&& has higher precedence than II
Short-Circuit Both associate from left to right

Evaluation
Stops when truth or falsehood in first condition is known
1. Input N
2. If ( N > 0 && N%2 == 0 )
2.1 Then display “Positive even number”
2.2 Else display “Negative or an odd number”

Example N N>0 N % 2 == 0 Display


Logical AND, both 1 true false Negative or an odd number
conditions must be true
0 false true Negative or an odd number
-1 false false Negative or an odd number
-1200 false true Negative or an odd number
3464 true true Positive even number
8 true true Positive even number
17 true false Negative or an odd number
1. Input N
2. If ( N > 0 || N%2 == 0 )
2.1 Then display “Positive or even number”
2.2 Else display “Negative and odd number”

Example N N>0 N % 2 == 0 Display


Logical OR, either or both 1 true false Positive or even number
conditions are true
0 false true Positive or even number
-1 false false Negative and odd number
-1200 false true Positive or even number
3464 true true Positive or even number
8 true true Positive or even number
17 true false Positive or even number
1. Input N
2. If ( ! (N>0) )
2.1 Then display “Positive!”
2.2 Else display “Not Positive!”
Example
N N>0 !(N>0) Display

Negation 1 true false Positive!


Reverses the meaning of 0 false true Not Positive!
the condition -1 false true Not Positive!
-1200 false true Not Positive!
3464 true false Positive!
8 true false Positive!
17 true false Positive!
Answer on your own first.
Correct answers on the next page.

Tamika was asked by her babushka (бабушка meaning


Problem grandmother) to buy 4 pcs of durian fruit wherein each should
weigh at least 6.27kg. Create a pseudocode and program that
Sample would determine if Tamika was able to buy her babushka’s
request correctly. Display “Lovely” if she bought right, and
display “Return them” if she did not.

Answer on your own first.


Correct answers on the next page.
1. Input DurianCount
2. Input DurianWeight
3. If (DurianCount == 4 && DurianWeight >= 6.27)
3.1 Then display “Lovely”
3.2 Else display “Return them”

#include <stdio.h>
Pseudocode void main() {
int DurianCount;
float DurianWeight;
if ( DurianCount == 4 && DurianWeight >= 6.27 ) {
printf(“Lovely”);
} else {
printf(“Return them”);
}
}

You might also like