0% found this document useful (0 votes)
11 views48 pages

Lecture 3 - Selection Statements

The document discusses selection statements in programming, focusing on conditions, branching decisions, and the use of relational, equality, and logical operators. It explains how to implement if statements, else clauses, cascaded if statements, and switch statements for decision-making in code. Additionally, it highlights the importance of using break in switch statements to control program flow.

Uploaded by

leaderatelast
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)
11 views48 pages

Lecture 3 - Selection Statements

The document discusses selection statements in programming, focusing on conditions, branching decisions, and the use of relational, equality, and logical operators. It explains how to implement if statements, else clauses, cascaded if statements, and switch statements for decision-making in code. Additionally, it highlights the importance of using break in switch statements to control program flow.

Uploaded by

leaderatelast
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/ 48

Selection Statements

CSE 4373: Computer Programming and Applications


Ajwad Abrar
Junior Lecturer, CSE
A scenario
● You want to attend a concert

● Only those with tickets can enter

If you have a ticket, you can enter.

This is a condition

You either have a ticket or you don’t.

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

● > greater than

● <= less than or equal 1.3 >= 12

● >= greater than or equal


8
Logical data in C
Does not have a dedicated boolean datatype

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

Logical operators produce 1 (true) or 0 (false) as results. Any


non-zero operand is 1 (true) and only zero is 0 (false)

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)

● If left operand is sufficient to determine the evaluation, right


operand is skipped

(i != 0) && (i > 0)

i > 0 is only evaluated if i is not 0

● && and || has lower precedence than relational and equality


operators
25
if statement

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

int number = -12;


if(number < 0)
{
printf(“The number is negative.”);
}

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”);

Condition is True Condition is False

● 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”);

Condition is True Condition is False


32
else clause
int number = 10; Number
if(number < 0)
{
printf(“The number is negative”);
Positive Negative
}
else
{
printf(“The number is positive”);
}
33
Cascaded if statement
Number

Positive Zero Negative

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

To which if statement does the else clause belong to?

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

if(temp < 20) {//Increase temperature}

else if(temp > 25) {//Decrease temperature}

else {//Do nothing}

40
switch statement

41
switch statement
Replaces cascading if statements

Consider the following scenario:


id = 1 → VIP
id = 2 → Premium
id = 3 → Regular
id = 4 → Economy
42
switch statement
int id; ● Tedious for
scanf(“%d”, &id); humans to write
if(id == 1) {printf(“VIP\n”);} ● Difficult to
else if(id == 2) {printf(“Premium\n”);} follow
else if(id == 3) {printf(“Regular\n”);} ● Slow to execute
else if(id == 4) {printf(“Economy\n”);} ● Difficult to find
else {printf(“Invalid ID”);} problems

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

You might also like