You are on page 1of 32

C Programming

Control – flow (if statements)


True or False?
● Is 3 greater than 5? False ● 3>5
● Is 3 less than 5? True ● 3<5
● Is 3 equal to 5? False ● 3 == 5
● Is 3 greater than or equal to 5? False ● 3 >= 5
● Is 3 greater than or equal to 3? True ● 3 >= 3
● Is 3 equal to 3? True ● 3 == 3
● Is 3 greater than 1? True ● 3>1
● Is 3 not equal to 4? True ● 3 != 4
● Is 3 not equal to 3? False ● 3 != 3
● Remember, we use bool for True and False conditions
Let’s code them
We can also use variables!

We needed to include <stdbool.h> library to use datatype “bool”


AND Logical Operators
● Let say I am 30 years old, salary = 7000 and weight = 110 kg
● Mostafa > 25 years and salary < 8000? True
● Mostafa > 27 years and salary > 9000? False
● Mostafa > 35 years and salary < 8500? False
● Mostafa > 35 years and salary > 9000? False
● Summary
○ Only a case is True: when both conditions are true
OR Logical Operators
● Let say I am 30 years old, salary = 7000 and weight = 110 kg
● Mostafa > 25 years or salary < 8000? True
● Mostafa > 27 years or salary > 9000? True
● Mostafa > 35 years or salary < 8500? True
● Mostafa > 35 years or salary > 9000? False
● Summary
○ Only a case is False: when both conditions are false
Truth Tables 2 variables

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 the following example!


So far
● All conditions are ANDed
○ True IF all ANDed conditions are true
● All conditions are ORed
○ True if any condition is true. False if all are false
● Mixed ORs and ANDs
○ Find each subgroup of ANDs and evaluate first. Do normal ORing for the remaining
● Advanced: What if I want to force specific priority? Use ()
○ Every group of () is computed first. Find the simplest and reduce first
○ Rarely used in practice
Let’s try simplifying
● Let’s simplify this expression T && T && (F || (T && T)) || T
● T && T && (F || (T && T)) || T ⇒ (T && T) is the simplest (). Its value is T
● T && T && (F || T) || T ⇒ (F || T) is the simplest (). Its value is T
● T && T && T || T ⇒ No more (). Next is group ands
● T && T && T || T ⇒ T && T && T is group of ands. Evaluate to T
● T || T. Now final expression is set of conditions ORed ⇒ T
Coding mistakes
● Writing < = NOT <= (extra spaces)
● Writing & NOT &&
● Writing & & NOT && (extra space)
● Writing | not ||
○ & and | are called bits operators that do the normal ANDing and Oring
on bits/
○ Writing = not ==
○ = is assignment. == is for comparing
● Writing ! = instead of != (extra space)
Summary
● Comparisons creates for us conditions (each is true or false)
○ < <= > >= == != [over numbers or characters)
● And Table: True IF all ANDed conditions are true
● Or Table: True if any ORed condition is true. False if all are false
● We can mix ands and ors. Priority rules:
○ First: Conditions inside () are evaluated. Find the simplest and replace it first
○ Second: each groups of ands [&&] are evaluated first
○ Remaining is either one result or ORed conditions
● In practice we usually have 1 or 2 simple conditions
○ If you did not understand () rules, it is ok for now.
If conditions
We can use the logical and comparison expressions in our
programs to perform different actions for different situations.
Sometimes you want a block of code to be executed based on
different conditions or when a condition is true or fulfilled , How
can we carry out such things?
The answer is -> if conditions.
● If (condition)
○ {
○ Body
○ }
Where condition is a logic expression that return a 0 or 1 , Body is any
sequence of instructions.
Example : Write a program that reads an integer salary
● Then If salary < 1000,
○ print you are poor
● Otherwise do nothing
Study the difference between the two examples
• the first example produced an output “you are poor”
because the input value ‘salary’ is less than 1000.

the second example the program produced nothing


because salary >= 1000 , so it skips the if statement.
What if I need more conditions?
● Write a program that reads an integer salary then:
● If salary < 1000,
○ print you are poor
● Else If salary >= 1000 and < 20000,
○ print good salary
● Else If salary >= 20000,
○ print you are rich
● Now, how to command computer to do these if else?
● Good software engineer tests well his code. What are good test cases?
○ 0, 500, 1000, 10000, 200000, 100000000
The if-else Chain
● If we have more than one possibility
we can use else-if statements.
● If (cond)
○ Go to body ONLY if cond true
● If previous condition is false,
move to next
● Else if (cond)
○ Again true, get it in
● If previous false move to next
● Else
○ Get in if nothing previous worked

Note that once a condition is true and a block


of code is executed all the else if and else
statements after it are skipped.
How many digits?
● Read and integer and print if it has
1, 2, 3, 4 or 5+ digits
● For example if input is 556
○ Then print: 3 digits
Nested if conditions
● Inside the body scope, we can do whatever
● Even another if (nested if)
● Or if { if {if () } }
● So whatever code body
Example : Write a program that reads an
integer salary
● Then If salary < 1000,
○ Read another integer Age,
○ If Age < 22 , print “you are still young”
○ Else print “oh it`s a bad salary for your
age!“
○ Else
○ Print “you are rich! It`s a good
salary”

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)

Given 3 numbers A,B,C print the largest and smallest


value of them.

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.

You might also like