Professional Documents
Culture Documents
Section 3
Section 3
Src
Logical Tables 3 variables
AND truth table OR truth table
Logical Tables 3 variables
● Let say I am 30 years old, salary = 7000 and weight = 110 kg
● Mostafa > 25 years and salary < 8000 and weight < 150kg? True
○ T and T and T
● Mostafa > 25 years and salary < 8000 and weight > 70kg? True
○ T and T and T
● Mostafa > 25 years and salary < 8000 and weight > 200kg? False
○ T and T and F
● Mostafa > 35 years or salary < 8000 or weight > 200kg? True
○ F or T or F
● Mostafa > 35 years or salary > 9000 or weight > 200kg? False
○ F or F or F
Mixing Logical Operators
● Let say I am 30 years old, salary = 7000 and weight = 110 kg
● Mostafa > 35 years or salary > 6000 or weight > 200kg? True
● Mostafa > 35 years and salary > 6000 or weight > 200kg? False
○ Reduce every subgroup of ANDS first
○ F and T or F
○ F or F ⇒ F
● Mostafa > 20 years and salary > 10 and salary < 8000 or weight > 200kg?
○ T and T and T or F
○ T or F ⇒ T
● Precedence: Means what to apply first. Here AND before OR
○ What about 3 + 4 * 5: is it 7 * 5 or computed 3 + 20? * before +
C Operators
● && for ANDING
● || for ORING
Remember that we are evaluating a logical
expression so the result is always either a true
(1) or false (0).
Study carefully this example and watch out every if and its else and how they are connected ,
#Rule : every ‘else’ statement belongs to the closest if above it from inside out.
Rewrite the code and run it using different values for salary and age and check the output!
Summary: 3 styles
● If (condition) ● If (condition) ● If (condition)
○ body ○ body ○ body
● Body Either: ● else if (condition) ● else if (condition)
○ 1 line code OR
○ body ○ body
○ {
● else if (condition) ● else if (condition)
Several lines ○ body ○ body
● else if (condition) ● else if (condition)
○ }
○ body ○ body
● Also body can be
● else if (condition) ● else
nested ifs
○ body ○ body
Simple Calculator
● Given two numbers and a sign between them which will
indicate if the user want the addition, subtraction, division
or multiplication of these two numbers, find the value of the
answer.
● Inputs ⇒ outputs
○ 7 + 55 ⇒ 62
○ 7 * 10 ⇒ 70
Give yourself few minutes thinking and sketching how you can implement
this program.
Simple Calculator Code
Exercise #1 (try it yourself)
Input 5 2 4
Output 5 2.
Exercise 2#
● Read 2 integers A, B and print based on following cases:
○ if both are odd print their product A*B
○ if both are even print their division A/B
○ if the first is odd and the second is even then find their sum A+B
○ if the first is even and the second is odd then find their subtraction A-B
● Inputs ⇒ outputs
○ 5 7 => 35
○ 12 2 => 6
○ 5 6 => 11
○ 12 3 => 9
Exercise #2 Solution
Recall that in order to check if a number is even or not we can simply do number % 2 if the remainder of the
division is 0 then the number is even otherwise it is odd.
Exercise 3#
● Write a program that reads number X, then other 5 numbers. Print 2 values:
○ How many numbers <= X
○ How many numbers > X
● Inputs
○ 10 300 1 5 100 200
○ Output: 2 3
○ Explantation
○ 2 numbers (1, 5) are <= 10
○ 3 numbers (100, 200, 300) are > 10
Exercise #3 Solution
If we explore the possibilities of the output we will find out that there are around 30 possibilities , doing all of them in a bunch of if conditions
is not the smartest solution , so what is the best approach?
We can create two variables initially equal 0 , and we can check every number of the five numbers and if it is greater we can increment on of
the two variables and if it is not we can increment the other one , follow the code to understand the process.
Studying Tips
• Do Every example and exercise yourself and try different inputs.
• Don`t Give UP! , Do your best solving the assignments , it is ok to struggle at the beginning or fail in
solving 50% of the problems it needs sometime to fully understand the concepts and the tweaks.