Lecture 3 - Selection Statements
Lecture 3 - Selection Statements
This is a condition
2
Conditions
● Verbally, we denote the outcome of conditions as:
True False
Yes No
On Off
1 0
We must test if a condition is true or not and based on that we have to take
branching decisions
3
Branching decisions
4
How do we translate this into
programming?
5
Basic thermostat
● It has to keep the temperature fixed in a
certain range
● Assume that the range is between 20
degrees celsius and 25 degrees celsius
If temperature > 25 → Decrease temperature
If temperature < 20 → Increase temperature
Else → Do nothing
6
Relational operators
7
Relational operators
Compares between two operands
● < less than 10 < 13
1 True
0 False
Others True
9
Relational operators
● Produces 0 (False) or 1 (True) when used in an
expression
● Can compare variables
● Precedence lower than arithmetic operators
i + j < k - 1 → (i + j) < (k - 1)
● Left associative
i < j < k → (i < j) < k
10
Pop Quiz
11
Relational operators
● 3 * 5 > 16
● 25 / 5 * 4 >= 10 + 20 - 5 * 2
● 7 % 2 > 12 <= 14
● (1 >= 1.5) * (15 + 3.12)
int i = 35;
float j = 21.3f;
i + j >= i - j;
12
Relational operators
● 3 * 5 > 16 0 (False)
● 25 / 5 * 4 >= 10 + 20 - 5 * 2 1 (True)
● 7 % 2 > 12 <= 14 1 (True)
● (1 >= 1.5) * (15 + 3.12) 0
int i = 35;
float j = 21.3f;
i + j >= i - j; 1 (True)
13
Equality operators
14
Equality operators
Used to check whether two operands are equal or not
● == Equal to
● != Not equal
Produces either 0 (false) or 1 (true) as their result
● Lower precedence than the relational operators
i < j == j < k → (i < j) == (j < k)
● Left associative
10 != 12.5 == 12 → (10 != 12.5) == 12
15
Equality operators
It is very common to mix up equality operator (==) with assignment
operator (=) due to habit with mathematics
They can make the program produce unexpected outcome without
throwing errors
int i = 5;
i == 6; // Will give us 0 (False)
i = 6; // Will store 6 in variable
16
Pop Quiz
17
Equality operators
What is the output of the
expression:
(i >= j) + (i == j)
If,
i = 5, j = 8
CSE
i = 8, j = 5 4373
i = j = 5
18
Equality operators
What is the output of the
expression:
(i >= j) + (i == j)
If,
i = 5, j = 8 → 0
CSE
i = 8, j = 5 → 1 4373
i = j = 5 → 2
19
Logical operators
20
Logical operators
Logical operators are used to produce complicated logical
expressions by connecting simpler ones
● ! Logical negation
● && Logical and
● || Logical or
21
Logical negation
● Is denoted by !
● !(10 > 20) → 1 (True)
● Has the same precedence as unary +/-
● Right associative
!!(a == b) → !(!(a == b))
Simply flips between 1 or 0
!!10 → 1 (True)
22
Logical and
23
Logical or
24
Logical and / or
● Evaluate from left to right
(i >= 0) || (j != 13)
(i != 0) && (i > 0)
26
if statement
Choose between alternatives by testing a condition
if(expression) {statement}
If the expression gives Non-zero values, only then the statement will be executed
27
if statement
if(10 < 20) if(20 < 10)
{ {
printf(“10 is lower\n”); printf(“20 is lower\n”);
} }
printf(“We are done”); printf(“We are done”);
● The expressions inside the curly braces ({}) consist a code block
● The if statement selectively executes certain code blocks based on some
conditions
● The norm is to indent (4 spaces/ 1 tab) the expressions inside a code block
28
if statement
int age;
printf(“Enter your age: ”);
scanf(“%d”, &age);
if((age > 18) && (age < 85))
{
printf(“You can run for president.”);
}
Join simpler conditions together using && and create complex conditions
29
else clause
30
else clause
The if statement may have an else clause, which
provides an alternative when the condition fails
if(expression) {statement}
else {statement}
The else clause does not have a condition. It will only
execute the expressions if the condition after the if
statement is false (0)
31
else clause
if(10 < 20) if(20 < 10)
{ {
printf(“10 is lower\n”); printf(“20 is lower\n”);
} }
else else
{ {
printf(“10 is higher\n”); printf(“20 is higher\n”);
} }
printf(“We are done”); printf(“We are done”);
34
Cascaded if statement
if(expression) ● Best way to test a series of
{statement} conditions
● Another if statement will be
else if(expression)
nested inside first if
{statement} statement’s else clause
...
else if(expression)
If-Ception
{statement}
else
{statement}
35
Pop Quiz
36
Cascaded if statement
if (y != 0)
if (x != 0)
if else if
result = x / y;
else
printf("Error: y is 0\n");
else
37
Dangling else problem
if (y != 0)
if (x != 0)
if else if
result = x / y;
else
printf("Error: y is 0\n");
else
else belongs to the nearest if that has not been paired with an else .
38
Basic thermostat
39
Basic thermostat
● Range is between 20 degrees celsius and 25 degrees celsius
int temp;
scanf(“%d”, &temp);
40
switch statement
41
switch statement
Replaces cascading if statements
43
switch statement
● Use switch case instead of chaining if statements
● Easier to read
● Faster execution
switch(controlling-expression)
{
case constant-expression : statements
…
case constant-expression : statements
default : statements
}
44
switch statement
int id;
scanf(“%d”, &id);
switch(id)
{
case 1: printf(“VIP\n”);
break;
…
case 4: printf(“Economy\n”);
break;
default: printf(“Invalid ID\n”);
break;
} 45
Why use break
● Causes program to “break”
out of the switch
statement
● switch is basically
controlled jump to a
certain label
● Anything after that label
gets executed if there’s no
break
46
Reading Tasks
● C Programming - A Modern Approach | Chapter 5
47
Thank You
48