You are on page 1of 47

Control Structure:

Selection
(Part 2)
Knowledge:
Understand various concepts of selection control structure

Skill:
Be able to develop a program containing selection control structure

Computer Science Department

FTSM

if Statements

if else - if

if
if - else
TK1913-C Programming

if Statement
The structure is similar to single selection (flowchart)
Syntax:
Dont forget the
if (expression)
brackets !!
statement;
or
if (expression) {
Dont forget the
statement1;
curly brackets !!
statement2;
}
TK1913-C Programming

if Statement
The similarity between single selection structure and
if statement:
Single Selection:
if <condition is true> start
step 1
step 2

step k
end_if
TK1913-C Programming

if Statement:
if (<condition>) {
statement 1
statement 2

statement k
}
4

if Statement
Example:
int num1, num2, min;
printf(Key-in 2 numbers: );
20
scanf(%d%d, &num1, &num2);
min = num1;
if (num1 > num2)
min = num2;
printf(Smallest: %d\n, min);

> 15?

num1

?
20

num2

15
?

min

15
20
?

Key-in 2 numbers: 20
_ 15
_Smallest: 15
_
TK1913-C Programming

if Statement
Example:
int mark;
92 > 80?
printf(Mark: );
scanf(%d, &mark);
if (mark > 80) {
printf(Category: Excellent\n);
printf(Congratulations!);
}

mark

?
92

Mark: 92
_
_
Category:
Excellent
Congratulations!
TK1913-C Programming

if Statement
Example:
void main() {

What will the output


be if the mark is 65?

int mark;
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

if Statement
Example:
void main() {
int mark;

What will the output


be if the mark is 35?

printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

if - else Statement
The structure is similar to double selection (flowchart)
Syntax:

or

if (expression)
statement;
else
statement;
if (expression) {
statement1;
statement2;
} else
statement3;

TK1913-C Programming

if - else Statement
or
if (expression) {
statement1;
statement2;
} else {
statement3;
statement4;
}
TK1913-C Programming

10

if else Statement
The similarity between double selection structure
and if - else statement:
Double Selection:
if <condition is true> start
step 1

step k
end_if
else start
step 1

step n
end_else
TK1913-C Programming

if Statement:
if <condition> {
statement 1

statement k
}
else {
statement 1

statement n
}
11

if - else Statement
Example:
if (num1 < num2)
10 < 15?
min = num1;
else
min = num2;
printf(Smallest: %d\n, min);

num1

10

num2

15

min

10
?

_Smallest: 10
_

TK1913-C Programming

12

if - else Statement
Example:
if (num1 < num2)
20 < 15?
min = num1;
else
min = num2;
printf(Smallest: %d\n, min);

num1

20

num2

15

min

15
?

_Smallest: 15
_

TK1913-C Programming

13

if - else Statement
Example:

num1 700

if (num1 < num2) {


700 < 125?
min = num1;
num2 125
max = num2;
}
min 125
??
else {
min = num2;
max = num1;
max 700
??
}
printf(Min = %d, Max = %d\n, min, max);
_
Min
= 125, Max = 700
_
TK1913-C Programming

14

if else Statement
Example:
void main() {
int mark;

What will the output


be if the mark is 21?

printf(Mark: );
scanf(%d, &mark); What will the output
be if the mark is 74?
if (mark >= 50)
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

15

if else Statement
Example:
void main() {
int mark;

What will the output


be if the mark is 74?

printf(Mark: );
scanf(%d, &mark); What will the output
if (mark >= 50)
be if the mark is 14?
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

16

if else Statement
Example:
What will the output
void main() {
be if the mark is 14?
int mark;
printf(Mark: );
scanf(%d, &mark); What will the output
if (mark >= 50)
be if the mark is 70?
printf(Pass\n);
else {
printf(Fail\n);
printf(Your mark is %d, mark);
}
}
TK1913-C Programming

17

Take a break .
Lets have a minute break.
While youre having a break, let
me introduce you some program
styles..
Why we need program
styles?.To ensure your
program is readable.
Lets look some
examples..

TK1913-C Programming

18

Take a break (Learn styles)


Example:
void main() {
int mark;
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

19

Take a break (Learn styles)


Example:

Difficult to read!!!
void main() {
int mark;
Dont you think so??
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming

20

Lets recap
Syntax:
if (expression)
statement;
else
statement;

if-else
if statement
statement

Ok now, lets look at


if else if statement
TK1913-C Programming

21

if else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
TK1913-C Programming

if-else-if statement

22

if else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else (expression)
statement;
TK1913-C Programming

if-else-if statement

Be carefulcommon
mistake made by
students !!
23

Lets recap
Example:

Multiple Selection

if <condition_1 is true> start


step m

step
Assume
m, step
condition
. will1 be
is
end_if
true,
executed
so
if <condition_2 is true> start
step n

end_if
else start
step x

end_else
TK1913-C Programming

24

Lets recap
Example:

Multiple Selection

if <condition_1 is true> start


step m

step
Assume
m, step
condition
. will1be
is
end_if
skipped,
false,
andso
if <condition_2 is true> start
step n
condition 2 will be tested

end_if
else start
step x

end_else
TK1913-C Programming

25

Lets recap
Example:

Multiple Selection

if <condition_1 is true> start


step m

end_if
if <condition_2 is true> start
step n

step
Assume
n, step
condition
. will2be
is
end_if
true,
executed
so
else start
step x

end_else
TK1913-C Programming

26

Lets recap
Example:

Multiple Selection

if <condition_1 is true> start


step m

end_if
if <condition_2 is true> start
step n

Assume
step n, condition
step . will
2 isbealso
end_if
skipped,
andso
false,
else start
step x
step x will be executed

end_else
TK1913-C Programming

27

Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;

Is the letter a lower case?

scanf(%c, &letter);
Is the letter an upper case?
if (letter >= a && letter <= z )
printf(Lower case\n);
else if (letter >= A && letter <= Z)
Is the letter a digit?
printf(Upper case\n);
else if (letter >= 0 && letter <= 9)
printf(Digit\n);
else
printf(Special character\n);
}
TK1913-C Programming

28

Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;

(the letter is a lower case) true

scanf(%c, &letter); (the


(theletter
letterisisaalower
lowercase)
case)false
false
if (letter >= a && letter
z ) isisan
(the
(the<=
letter
letter
anupper
uppercase)
case)false
true
printf(Lower case\n);
(the letter is a digit) true
else if (letter >= A &&(the
letter
<= Z)
letter
is a lower case) false
printf(Upper(the
case\n);
letter is an upper case) false
else if (letter >= 0 && letter
9) is a digit) false
(the<=letter
printf(Digit\n);
else
printf(Special character\n);
}
TK1913-C Programming

29

Exercise
Develop a program for the following problem.
Given a mark, determine its grade based on the
table below:
74 < mark < 100
64 < mark < 75
54 < mark < 65
39 < mark < 55
0 < mark < 40
others
TK1913-C Programming

grade = A
grade = B
grade = C
grade = D
grade = E
error message
30

A
n
s
w
e
r
1

int mark;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
printf("Grade = A);
else if ((mark >= 65) && (mark <= 74))
printf(" Grade = B);
else if ((mark >= 55) && (mark <= 64))
printf(" Grade = C);
else if ((mark >= 40) && (mark <= 54))
printf(" Grade = D);
else if ((mark >= 0) && (mark <= 39))
printf(" Grade = E);
else
printf(Input error\n);
TK1913-C Programming

31

A
n
s
w
e
r
2

int mark;
char grade ;
printf(Key-in the mark : );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
grade =A;
else if ((mark >= 65) && (mark <= 74))
grade =B;
else if ((mark >= 55) && (mark <= 64))
grade =C;
else if ((mark >= 40) && (mark <= 54))
grade =D;
else if ((mark >= 0) && (mark <= 39))
grade =E;
else
printf(Input error\n);
printf(Your grade is %c, grade );
TK1913-C Programming

32

A
n
s
w
e
r
3

int mark;
char grade;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
grade=A;
else if ((mark >= 65) && (mark <= 74))
grade=B;
else if ((mark >= 55) && (mark <= 64))
grade=C;
else if ((mark >= 40) && (mark <= 54))
grade=D;
else if ((mark >= 0) && (mark <= 39))
grade=E;
if ((mark > 100) || (mark < 0))
printf(Input error\n);
else
TK1913-C Programming
printf(Your grade is %c, grade);

33

Nested ifs

if statement that contains


other if / if - else statements

TK1913-C Programming

34

Nested ifs
Example:

This price is valid for


people:
> 55
people:
18 age
< age
< 55

if (age > 18) {


if (age > 55)
price = 2.50; /* Price for senior citizens */
This price is valid for
else
people:
price = 5.00; /* Price for
adultsage
*/ < 1
This price is valid for
}
else {
people: 1 < age < 18
if (age < 1)
price = 0.0; /* Price for infants */
else
price = 1.50; /* for children & teenagers*/
}
TK1913-C Programming

35

Nested ifs - Problem


Example:
if (age > 18)
if (age > 55)
price = 2.50; /* Price for senior citizens */
else
price = 5.00; /* Price for adults */

Which if does this else


belong to?

TK1913-C Programming

36

Nested ifs - Problem


Which one?
if (age > 18) {
if (age > 55)
price = 2.50;
}
else
price = 5.00;
TK1913-C Programming

if (age > 18) {


if (age > 55)
price = 2.50;
else
price = 5.00;
}
37

Nested ifs - Problem

By default, else will be attached to the


nearest if
if (age > 18)
if (age > 55)
price = 2.50;
else
price = 5.00;

TK1913-C Programming

if (age > 18) {


if (age > 55)
price = 2.50;
else
price = 5.00;
}
38

switch and break


The structure is similar to multiple selection (flowchart)

Syntax:
switch switch
(expression)
{
(expression)
{
case expression1:
statement1;
break;
case espression2:
statement2;
break;

default:
expressionX;
break;
}
TK1913-C Programming

Dont forget the


brackets !!

Dont forget the


curlycolons
brackets
!! !!

39

switch and break


Important Rule !!

Syntax:
Must be
switch (expression) {
INTEGER or
case expression1:
CHAR !
statement1;
break;
case espression2:
statement2;
break;

default:
expressionX;
break;
}
TK1913-C Programming

40

switch and break


Example: switch (month) {
case 1:

Assume month = 1,
so
printf(January\n);
break;

case 2:
case 3:
default:

this step will be


printf(February\n);
case
is terminated
break;
executed.
Later

here. Jump to

printf(March\n);
break;

printf(Others\n);
break;

January
_
End
_

}
printf(End);
TK1913-C Programming

41

switch and break


Example: switch (month) {
case 1:
case 2:

March

printf(January\n);
break;

_
End
_

printf(February\n);
break;

case 3:

Assume month = 3,
printf(March\n);
so
break;

default:

this step will be


printf(Others\n);
case
is terminated
break;
executed.
Later

}
printf(End);
TK1913-C Programming

here. Jump to

42

switch and break


Example: switch (month) {
case 1:
case 2:

printf(January\n);
break;
printf(February\n);
Nowwhat
No more !!will
break;

case 3:

happen if this break


printf(March\n);
is taken out from the
break;
default:
program?
printf(Others\n);
break;
}
}
printf(End);
printf(End);
TK1913-C Programming

43

switch and break


Example: switch (month) {
case 1:
case 2:
case 3:
default:

Assume
month = 2,
February
_ so
March

printf(January\n);
_ _
break;
End
printf(February\n);

this step will be


printf(March\n);
break;
executed. Later

printf(Others\n);
continues.
break; Thus, case

execution
this step is}printf(End);
executed . So
TK1913-C Programming

is terminated
here. Jump to
44

switch and break


Example: switch (month) {
case 1:
case 2:

printf(January\n);
break;
printf(February\n);

Nowwhat will
happen if these
printf(March\n);
breaks are taken out
break;
default:
from the program?
printf(Others\n);

And
And

case 3:
if month = 34
1 ??

break;
}
printf(End);
TK1913-C Programming

45

Exercise
To practice what youve
learned, try exercises on
page
/* Do exercises from pg
155 -156 of
Pengaturcaraan C

TK1913-C Programming

46

End of Lecture 7 (Part 2)


At last, chap 7 is finished.
Whats next???

Control Structure: Repetition on


the way
TK1913-C Programming

47

You might also like