0% found this document useful (0 votes)
50 views45 pages

Control Statement Selection

The document discusses if, if-else, and if-else-if statements in C programming. It provides the syntax, examples, and explanations of these conditional statements. Key points covered include: - The syntax of if statements with one or multiple statements - Examples of if statements comparing values and printing outputs - The syntax of if-else statements with one or multiple statements in each block - Examples of if-else statements comparing values and printing different outputs - The syntax of if-else-if statements with multiple conditions checked in order - Examples of if-else-if statements going through conditions sequentially and executing the first true block

Uploaded by

Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views45 pages

Control Statement Selection

The document discusses if, if-else, and if-else-if statements in C programming. It provides the syntax, examples, and explanations of these conditional statements. Key points covered include: - The syntax of if statements with one or multiple statements - Examples of if statements comparing values and printing outputs - The syntax of if-else statements with one or multiple statements in each block - Examples of if-else statements comparing values and printing different outputs - The syntax of if-else-if statements with multiple conditions checked in order - Examples of if-else-if statements going through conditions sequentially and executing the first true block

Uploaded by

Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

Control Statement Selection

if Statements

if

if else - if
if - else

if Statement
Syntax:
Syntax:
Dont forget the
ifif (expression)
(expression)
brackets !!
statement;
statement;
or
or
ifif (expression)
Dont forget the curly
(expression) {{
brackets, if there are more
statement1;
statement1;
than ONE statements
statement2;
statement2;
}}

if Statement
Pseudo
PseudoCode:
Code:
ifif<condition
<conditionisistrue>
true>start
start
step
step11
step
step22

step
stepkk
end_if
end_if

ififStatement:
Statement:
ifif(<condition>)
(<condition>){{
statement
statement11
statement
statement22

statement
statementkk
}}

if Statement
Example:
Example:

int
intnum1,
num1,num2,
num2,min;
min;
printf(Key-in
printf(Key-in22numbers:
numbers:);
);20 > 15?
scanf(%d%d,
scanf(%d%d,&num1,
&num1,&num2);
&num2);
min
min=
=num1;
num1;
ifif(num1
(num1>
>num2)
num2)
min
min=
=num2;
num2;
printf(Smallest:
printf(Smallest:%d\n,
%d\n,min);
min);
Key-in 2 numbers: 20
_ 15
_Smallest: 15
_

num1

?
20

num2

15
?

min

15
20
?

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

What will the output


be if the mark is 65?

printf(Mark:
printf(Mark:);
);
scanf(%d,
scanf(%d,&mark);
&mark);
ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
}}

printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);

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

What will the output


be if the mark is 35?

printf(Mark:
printf(Mark:);
);
scanf(%d,
scanf(%d,&mark);
&mark);

}}

ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);

if - else Statement
Syntax:
Syntax:

or
or

ifif(expression)
(expression)
statement1;
statement1;
else
else
statement2;
statement2;
ifif(expression)
(expression){{
statement1;
statement1;
statement2;
statement2;
}}else
else
statement3;
statement3;

if - else Statement
or
or

ifif (expression)
(expression) {{
statement1;
statement1;
statement2;
statement2;
}} else
else {{
statement3;
statement3;
statement4;
statement4;
}}

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

num1

10

num2

15

min

10
?

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

num1

20

num2

15

min

15
?

if - else Statement
Example:
Example:

num1 700

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

if else Statement
Example:
Example:

void
voidmain()
main(){{
int
intmark;
mark;

}}

What will the output


be if the mark is 21?

printf(Mark:
printf(Mark:);
);
What will the output
scanf(%d,
scanf(%d,&mark);
&mark); be if the mark is 74?
ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
else
else
printf(Fail\n);
printf(Fail\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);

if else Statement
Example:
Example:

void
voidmain()
main(){{
int
intmark;
mark;

}}

What will the output


be if the mark is 74?

printf(Mark:
printf(Mark:);
);
What will the output
scanf(%d,
scanf(%d,&mark);
&mark);
be if the mark is 14?
ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
else
else
printf(Fail\n);
printf(Fail\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);

if else Statement
Example:
Example:

What will the output


be if the mark is 14?

void
voidmain()
main(){{
int
intmark;
mark;
printf(Mark:
printf(Mark:);
);
scanf(%d,
scanf(%d,&mark);
&mark); What will the output
ifif(mark
be if the mark is 70?
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
else
else{{
printf(Fail\n);
printf(Fail\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);
}}
}}

Take a break (Learn


styles)
Example:
Example:

void
voidmain()
main(){{
int
intmark;
mark;
printf(Mark:
printf(Mark:);
);
scanf(%d,
scanf(%d,&mark);
&mark);
ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
else
else
printf(Fail\n);
printf(Fail\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);
}}

Take a break (Learn


styles)
Example:
Example:

Difficult
to
read!!!
void
main()
{
void main() {
Dontso??
you think
int
intmark;
mark;
printf(Mark:
printf(Mark:);
);
scanf(%d,
scanf(%d,&mark);
&mark);
ifif(mark
(mark>=
>=50)
50)
printf(Pass\n);
printf(Pass\n);
else
else
printf(Fail\n);
printf(Fail\n);
printf(Your
printf(Yourmark
markisis%d,
%d,mark);
mark);
}}

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

if-else
if statement
statement

Ok now, lets look at if


else if statement

if else - if Statement
Syntax:
Syntax:
ifif (expression)
(expression)
statement;
statement;
else
else ifif (expression)
(expression)
statement;
statement;
else
else ifif (expression)
(expression)
statement;
statement;
else
else
statement;
statement;

if-else-if statement

if else - if Statement
Syntax:
Syntax:
ifif (expression)
(expression)
statement;
statement;
else
else ifif (expression)
(expression)
statement;
statement;
else
else ifif (expression)
(expression)
statement;
statement;
else
else (expression)
(expression)
statement;
statement;

if-else-if statement

Be carefulcommon
mistake made by
students !!

Lets recap
Example:
Example:

Multiple Selection

ifif<condition_1
<condition_1isistrue>
true>start
start
step
stepm
m

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

end_if
end_if
else
elsestart
start
step
stepxx

end_else
end_else

Lets recap
Example:
Example:

Multiple Selection

ifif<condition_1
<condition_1isistrue>
true>start
start
step
stepm
m

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

end_if
end_if
else
elsestart
start
step
stepxx

end_else
end_else

Lets recap
Example:
Example:

Multiple Selection

ifif<condition_1
<condition_1isistrue>
true>start
start
step
stepm
m

end_if
end_if
ifif<condition_2
<condition_2isistrue>
true>start
start
step
stepnn

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

end_else
end_else

Lets recap
Example:
Example:

Multiple Selection

ifif<condition_1
<condition_1isistrue>
true>start
start
step
stepm
m

end_if
end_if
ifif<condition_2
<condition_2isistrue>
true>start
start
step
stepnn

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

end_else
end_else

Multiple Selection in C
Example:
Example:

#include
#include<stdio.h>
<stdio.h>
int
intmain(
main()){{
char
charletter;
letter;

}}

Is the letter a lower case?

Is the letter an upper case?


scanf(%c,
scanf(%c,&letter);
&letter);
ifif(letter
(letter>=
>=a
a &&
&& letter
letter<=
<=z
z))
printf(Lower
printf(Lowercase\n);
case\n);
Is the letter a digit?
else
if
(letter
>=
A
&&
letter
<=
Z)
else if (letter >= A && letter <= Z)
printf(Upper
printf(Uppercase\n);
case\n);
else
elseifif(letter
(letter>=
>=0
0 &&
&& letter
letter<=
<=9)
9)
printf(Digit\n);
printf(Digit\n);
else
else
printf(Special
printf(Specialcharacter\n);
character\n);

Multiple Selection in C
Example:
Example:

#include
#include<stdio.h>
<stdio.h>
int
intmain(
main()){{
char
charletter;
letter;

}}

(the letter is a lower case) true

(the
(theletter
letterisisaalower
lowercase)
case)false
false
scanf(%c,
&letter);
scanf(%c, &letter);
(the
(theletter
letter
isisan
anupper
uppercase)
case)false
true
ifif(letter
(letter>=
>=a
a &&
&& letter
letter<=
<=z
z))
(the letter is a digit) true
printf(Lower
printf(Lowercase\n);
case\n);
(the
letter
is a lower case) false
else
if
(letter
>=
A
&&
letter
<=
Z)
else if (letter >= A && letter <= Z)
(the
letter is an upper case) false
printf(Upper
printf(Uppercase\n);
case\n);
(the<=
letter
else
9)
elseifif(letter
(letter>=
>=0
0 &&
&& letter
letter
<=
9) is a digit) false
printf(Digit\n);
printf(Digit\n);
else
else
printf(Special
printf(Specialcharacter\n);
character\n);

Exercise
Develop a program for the following
problem.
Given a mark, determine its grade
based on the table below:
75
65
55
40
0

< mark <


< mark <
< mark <
< mark <
< mark <
others

100
74
64
54
39

grade = A
grade = B
grade = C
grade = D
grade = F
error message

Pseudo Code
Start
Input students mark
If students mark is greater than or equal to 75
Print Grade = A
else If students mark is greater than or equal to
lower than 75
Print Grade = B
else If students mark is greater than or equal to
lower than 65 Print Grade = C
else If students mark is greater than or equal to
lower than 55 Print Grade = D
else If students mark is greater than or equal to
than 40
Print Grade = F
else
Print Input error
End

65 and

55 and
40 and
0 and lower

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

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

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
printf(Your grade is %c, grade);

Nested ifs
if statement that
contains other if / if else statements

Nested ifs
Example:
Example:

This price is valid for


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

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

Nested ifs - Problem


Example:
Example:

ifif(age
(age>>18)
18)
ifif(age
(age>>55)
55)
price
price==2.50;
2.50;/*
/*Price
Pricefor
forsenior
seniorcitizens
citizens*/
*/
else
else
price
price==5.00;
5.00;/*
/*Price
Pricefor
foradults
adults*/
*/

Which if does this else


belong to?

Nested ifs - Problem


Try to understand the consequences.

ifif (age
(age >> 18)
18) {{
ifif (age
(age >> 55)
55)
price
price == 2.50;
2.50;
}}
else
else
price
price == 5.00;
5.00;

ifif (age
(age >> 18)
18) {{
ifif (age
(age >> 55)
55)
price
price == 2.50;
2.50;
else
else
price
price == 5.00;
5.00;
}}

Nested ifs - Problem


By default, else will be attached to the
nearest if
ifif (age
ifif (age
(age >> 18)
18) {{
(age >> 18)
18)
ifif (age
ifif (age
(age >> 55)
55)
(age >> 55)
55)
price
price
price == 2.50;
2.50;
price == 2.50;
2.50;
else
else
else
else
price
price
price == 5.00;
5.00;
price == 5.00;
5.00;
}}

switch and break


Syntax:
Syntax:
Dont forget the
switch
switch
(expression)
switch(expression)
(expression){{{
brackets !!
case
expression1:
case
case expression1:
expression1:
statement1;
statement1;
statement1;
break;
break;
break;
break;
case
case
expression2:
case
expression2:
caseexpression2:
expression2:
Dont
Dont
forget
forget
the
the
statement2;
statement2;
statement2;
statement2;
curly
colons
brackets
!! !!
break;
break;
break;
break;

default:
default
::
default:
default
expressionX;
expressionX;
expressionX;
expressionX;
break;
break;
break;
break;
}}}}

switch and break


Important Rule !!
Syntax:
Syntax:
Must be
switch
switch(expression)
(expression){{
INTEGER or
case
expression1:
case expression1:
CHAR !
statement1;
statement1;
break;
break;
case
caseexpression2:
expression2:
statement2;
statement2;
break;
break;

default
default::
expressionX;
expressionX;
break;
break;
}}

switch and break


Example
Example:: switch
switch(month)
(month){{
case
case1:
1:
case
case2:
2:
case
case3:
3:

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

this step will be


printf(February\n);
printf(February\n);

case
is
terminated
break;
executed.
Later
break;
here. Jump to

printf(March\n);
printf(March\n);
break;
break;
January
default:
default:
printf(Others\n);
printf(Others\n); End
_
_
break;
break;

}}
printf(End);
printf(End);

switch and break


March

Example
Example:: switch
switch(month)
(month){{
case
case1:
1:
case
case2:
2:
case
case3:
3:

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

_
End
_

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

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

default:
default:
this step will be
printf(Others\n);
printf(Others\n);

case is terminated
break;
executed.
Later
break;

}}
printf(End);
printf(End);

here. Jump to

switch and break


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

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

happen if this break


printf(March\n);
printf(March\n);
is taken out from the
break;
break;
default:
default:
program?
printf(Others\n);
case
case3:
3:

printf(Others\n);
break;
break;

}}
}}
printf(End);
printf(End);
printf(End);
printf(End);

switch and break


Example
Example:: switch
switch(month)
(month){{
case
case1:
1:
case
case2:
2:
case
case3:
3:

Assume
month = 2,
February
March
_ so

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

this
step
will
be
printf(March\n);
printf(March\n);
break;
executed. Later
break;
default:
default:
printf(Others\n);
printf(Others\n);
execution continues.
Thus, case is terminated
break;
break;
this step is}}executed . So
here. Jump to
printf(End);
printf(End);

switch and break


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

And
case3:
3:
if month = 34
1 ??case

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

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

}}
printf(End);
printf(End);

Masukkan
Jantina Anda: (L/P)
switch and
break
L

Example
Example::

Jantina adalah LELAKI

char
charjantina;
jantina;
printf
printf(Masukkan
(MasukkanJantina
JantinaAnda:
Anda:(L/P)\n);
(L/P)\n);
scanf
jantina
scanf(%c,&jantina);
(%c,&jantina);

L
?

switch
switch(jantina)
(jantina){{
case
caseP:
P:
printf(\nJantina
printf(\nJantinaadalah
adalah
PEREMPUAN\n);
PEREMPUAN\n);
break;
break;
case
caseL:
L:
printf(\nJantina
printf(\nJantinaadalah
adalahLELAKI);
LELAKI);
break;
break;
default:
default:
printf(\nTIDAK
printf(\nTIDAKDAPAT
DAPATDIPASTIKAN!);
DIPASTIKAN!);
break;
break;
}

End of Lecture
Review Questions

You might also like