Lecture 05 Program Control[1]
Lecture 05 Program Control[1]
Program Control
January 6, 2025
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
2
Program Control
• Up to this point we have seen that program’s statements are executed from top to
bottom, in the order that they appear inside the source code.
• However, in real programming, certain statements should be executed only if
specific conditions are met.
• This lecture will teach you how to use if and switch selection statements to
control the flow of your program and make it select a particular execution path
from a set of alternatives.
• It also introduces the conditional operator (?:), which can be used to form
conditional expressions.
3
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
4
if Statement
i f ( condition )
{
. . . /∗ b l o c k o f s t a t e m e n t s . ∗/
}
5
if Statement
If the value of the condition is true, the block of statements between the braces will be
executed. For example,
int x = 3;
i f ( x != 0 )
{
p r i n t f ( ” x i s n ’ t z e r o \n” ) ;
}
Since x is not 0, the value of the if condition is true and the program displays x isn’t
zero.
if (x != 0) is equivalent to if (x)
6
if Statement
If the value of the condition is false , the block of statements won’t be executed. For
example, the following code displays nothing:
i n t x = −3;
i f ( x == 0 )
{
p r i n t f ( ” x i s z e r o \n” ) ;
}
if (x == 0) is equivalent to if (!x)
7
if Statement
If the block of statements consists of a single statement, the braces can be omitted. In
other words, the previous code could be written as -
i f ( x == 0 )
p r i n t f ( ” x i s z e r o \n” ) ;
8
if Statement: Common Errors
A common error is to add a semicolon (;) at the end of the if statement, as you
usually do with the most statements. The semicolon is handled as a statement that
does nothing ( null statement) and the compiler terminates the if . For example, in the
following code,
i n t x = −3;
i f ( x > 0) ;
p r i n t f ( ”x i s p o s i t i v e \n” ) ;
the ; terminates the if statement and the program continues with the printf () call.
Therefore, the output is always x is positive regardless of the value of x.
9
if Statement: Common Errors
Another common error is to confuse the == operator with the = operator inside an if
condition. Remember that the == operator checks whether two expressions have the
same value, while the = operator assigns a value to a variable. For example, this code
int x = 0;
i f ( x = 0)
p r i n t f ( ” x e q u a l s z e r o \n” ) ;
outputs x equals −2, although x is −3. If we had written if (x == −2) nothing would
have been displayed.
The following code displays nothing since the assignment of 0 in x makes the condition
false .
i n t x = −3;
i f ( x = −2)
p r i n t f ( ” x e q u a l s −2\n” ) ;
10
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
11
if-else Statement
i f ( condition )
{
. . . /∗ b l o c k o f s t a t e m e n t s A . ∗/
}
else
{
. . . /∗ b l o c k o f s t a t e m e n t s B . ∗/
}
If the condition is true, the first block of statements will be executed; if not, the
second block will run.
12
if-else Statement
For example, the following program displays x is negative or zero because x is less than
or equal to 0.
i n t x = −3;
i f ( x > 0)
{
p r i n t f ( ” x i s p o s i t i v e \n” ) ;
}
else
{
p r i n t f ( ” x i s n e g a t i v e o r z e r o \n” ) ;
}
13
if-else Statement
And, since both blocks consist of a single statement, we could omit the braces:
i n t x = −3;
i f ( x > 0)
p r i n t f ( ” x i s p o s i t i v e \n” ) ;
else
p r i n t f ( ” x i s n e g a t i v e o r z e r o \n” ) ;
14
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
15
Nested if Statements
An if statement can contain any kind of statement, including other if and else
statements. For example, the following program contains two nested if statements:
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t a = 10 , b = 20 , c = 30;
i f ( a > 5)
{
i f ( b == 2 0 ) /∗ n e s t e d i f s t a t e m e n t . ∗/
p r i n t f ( ”1 ” ) ;
i f ( c == 4 0 ) /∗ n e s t e d i f s t a t e m e n t . ∗/
p r i n t f ( ”2 ” ) ;
else
p r i n t f ( ”3 ” ) ;
}
else
p r i n t f ( ” 4\ n” ) ;
16
}
Nested if Statements
In a program with nested if statements, each else statement is associated with the
nearest if statement that does not contain an else . For example, consider the
following program:
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int a = 5;
i f ( a != 5 )
i f ( a−2 > 5 )
p r i n t f ( ”One\n” ) ;
else
p r i n t f ( ”Two\n” ) ;
return 0;
}
The program displays nothing because the first if statement is false and it has no
17
else associated with.
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
18
if-else if-else Statement
To test a series of if statements and stop when a condition is true, use nested if
statements, like this (the comments label the blocks of statements):
i f ( condition A )
{
. . . /∗ b l o c k o f s t a t e m e n t s A . ∗/
}
else i f ( condition B)
{
. . . /∗ b l o c k o f s t a t e m e n t s B . ∗/
}
.
.
.
else
{
. . . /∗ b l o c k o f s t a t e m e n t s N . ∗/
}
19
if-else if-else Statement
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int a;
p r i n t f ( ” E n t e r number : ” ) ;
s c a n f ( ”%d” ,& a ) ;
i f ( a == 1 )
p r i n t f ( ”One\n” ) ;
e l s e i f ( a == 2 )
p r i n t f ( ”Two\n” ) ;
else
p r i n t f ( ” S omething e l s e \n” ) ;
p r i n t f ( ”End\n” ) ;
return 0;
}
20
if-else if-else Statement
Something else
End
The block of statements in the last else is executed only if all previous conditions are
false . Nevertheless, the last else statement is not mandatory. If it is missing and all
conditions are false , the program continues with the execution of the first statement
after the last else if .
In the previous program, if the number 4 is entered and the last else statement was
missing, the program would display -
End
21
Practice Problems
Write a program that reads an integer and displays its absolute value.
Write a program that reads two integers and displays them in ascending order.
Write a program that reads two integers and displays their relationship without using
an else statement.
Write a program that reads two floats (such as a and b) and displays the solution of
the equation a∗x + b = 0.
Write a program that reads the grades of three students and displays them in
ascending order.
22
Practice Problems
23
Practice Problems
24
Practice Problems
25
Practice Problems
26
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
27
Conditional Operator ? :
The conditional operator ? : allows a program to perform one of two actions depending
on the value of an expression. Expressions that contain the ? : operator have the form -
Since the conditional operator takes three operands, it is often referred to as the
ternary operator. A conditional expression is evaluated in steps. The expression exp1 is
evaluated first. If it is true, exp2 is evaluated and its value becomes the value of the
conditional expression. If the value of exp1 is false , exp3 is evaluated and its value
becomes the value of the conditional expression.
Simply put, a conditional expression is a sort of an if-else statement:
i f ( exp1 )
exp2 ;
else
exp3 ;
28
Conditional Operator ? :
The following program reads an integer and if it is greater than 10 the program
displays One, otherwise Two:
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int a;
p r i n t f ( ” E n t e r number : ” ) ;
s c a n f ( ”%d” , &a ) ;
( a > 1 0 ) ? p r i n t f ( ”One\n” ) : p r i n t f ( ”Two\n” ) ;
return 0;
}
29
Conditional Operator ? :
The conditional operator is mostly used to replace simple if −else statements. For
example, the following if −else statement
i f (a > b)
max = a ;
else
max = b ;
30
Conditional Operator ? :
i f ( exp1 )
k = exp2 ;
e l s e i f ( add1 )
k = add2 ;
else
k = add3 ;
31
Practice Problem
32
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
33
switch Statement
The switch statement can be used instead of if −else−if statements when we want to
test the value of an expression against a series of values and handle each case
differently.
34
switch Statement
The general form of the switch statement is -
switch ( expression )
{
case constant 1 :
/∗ b l o c k o f s t a t e m e n t s , w h i c h w i l l be e x e c u t e d i f t h e value of the
e x p r e s s i o n i s e q u a l t o c o n s t a n t 1 . ∗/
break ;
...
case constant n :
/∗ b l o c k o f s t a t e m e n t s , w h i c h w i l l be e x e c u t e d i f t h e value of the
e x p r e s s i o n i s e q u a l t o c o n s t a n t n . ∗/
break ;
default :
/∗ b l o c k o f s t a t e m e n t s , w h i c h w i l l be e x e c u t e d i f t h e value of the
e x p r e s s i o n i s n o t e q u a l t o anyone o f t h e p r e −m e n t i o n e d c o n s t a n t s . ∗/
break ;
}
35
switch Statement
The expression must be an integer variable or expression, and the values of all
constant 1, constant 2, ... , constant n must be integer constant expressions with
different values.
If the break statement is missing from the matching case, the program will continue
with the execution of the next case statements, until a break statement is met.
If there are two or more case clauses with the same block of statements, they can be
merged together like so:
case constant 1 :
case constant 2 :
case constant 3 :
/∗ b l o c k o f s t a t e m e n t s t h a t w i l l be e x e c u t e d i f t h e v a l u e o f t h e
e x p r e s s i o n m at ches any o f c o n s t a n t 1 , c o n s t a n t 2 o r c o n s t a n t 3 . ∗/
break ;
36
switch versus if
• The main disadvantage of using the switch instead of if is that switch can check
only whether the value of an expression and the case constants are equal, whereas
if can check any kind of condition and not only for equality.
• Moreover, when using switch, the values of expression and case constants must be
integers.
• Characters are treated as small integers so they may be used, but floating-point
numbers and strings are not permitted.
• On the other hand, the use of the switch statement in place of cascaded
if −else−if statements may make the program easier to read.
37
Practice Problems
What is the output of the following program?
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int a = 1;
switch (a)
{
case 1:
p r i n t f ( ”One\n” ) ;
return 0;
case 2:
p r i n t f ( ”Two\n” ) ;
break ;
}
p r i n t f ( ”End\n” ) ;
return 0;
}
38
Practice Problems
Write a program that simulates a physical calculator. The program should take as
input the symbol of an arithmetic operator and two integers and display the result of
the arithmetic operation.
Write a program that displays the area of a square or a circle based on the user’s
choice. If the user enters 0, the program should read the side of the square and display
its area. If the user enters 1, the program should read the radius of the circle and
display its area.
39
Practice Problems
Write a program that takes as input a person’s sex and height and displays the
corresponding description for his or her height, according to following table.
40
Outlines
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
41
Home Works
Write a program that reads two double numbers and displays the absolute value of
their sum.
Write a program that reads three integers and checks if they are in successive increase
order (i.e., 5, 6, and 7 or −3, −2, and −1).
1 Program Control
2 if Statement
3 if-else Statement
4 Nested if Statements
6 Conditional Operator ?:
7 switch Statement
8 Programming Problems
9 Summary
43
Summary
• if Statement
• if −else Statement
• Nested if Statement
• if −else if −else Statement
• Conditional Operator
• switch Statement
44
Thank You!