SlideShare a Scribd company logo
1 of 45
Control Statement - Selection
if Statements
if
if - else
if – else - if
if Statement
Syntax:
if (expression)
statement;
or
if (expression) {
statement1;
statement2;
}
Don’t forget the curly
brackets, if there are more
than ONE statements
Don’t forget the
brackets !!
if Statement
Pseudo Code:
if <condition is true> start
step 1
step 2
…
step k
end_if
if Statement:
if (<condition>) {
statement 1
statement 2
…
statement k
}
if Statement
Example:
int num1, num2, min;
printf(“Key-in 2 numbers: “);
scanf(“%d%d”, &num1, &num2);
min = num1;
if (num1 > num2)
min = num2;
printf(“Smallest: %dn”, min);
Key-in 2 numbers: _
num2 ?
num1 ?
min ?
Key-in 2 numbers: 20 15
_
20
15
20
15
Key-in 2 numbers: 20 15
Smallest: 15
_
20 > 15?
if Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 65?
if Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 35?
if - else Statement
Syntax:
if (expression)
statement1;
else
statement2;
or
if (expression) {
statement1;
statement2;
} else
statement3;
if - else Statement
or
if (expression) {
statement1;
statement2;
} else {
statement3;
statement4;
}
if - else Statement
Example:
if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %dn”, min);
num2 15
num1 10
min ?
10 < 15?
_
10
Smallest: 10
_
if - else Statement
Example:
if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %dn”, min);
num2 15
num1 20
min ?
20 < 15?
_
15
Smallest: 15
_
if - else Statement
Example:
if (num1 < num2) {
min = num1;
max = num2;
}
else {
min = num2;
max = num1;
}
printf(“Min = %d, Max = %dn”, min, max);
num2 125
num1 700
min ??
700 < 125?
_
max ??
Min = 125, Max = 700
_
125
700
if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 21?
What will the output
be if the mark is 74?
if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
What will the output
be if the mark is 74?
What will the output
be if the mark is 14?
if – else Statement
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else {
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
}
What will the output
be if the mark is 14?
What will the output
be if the mark is 70?
Take a break … (Learn styles)
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
Take a break … (Learn styles)
Example:
void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Passn”);
else
printf(“Failn”);
printf(“Your mark is %d”, mark);
}
Difficult to read!!!
Don’t you think so??
Syntax:
if (expression)
statement;
if statement
Let’s recap …
Syntax:
if (expression)
statement;
else
statement;
if-else statement
Ok now, let’s look at
if – else –if statement
if – else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
if-else-if statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
if-else-if statement
if – else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else (expression)
statement;
if-else-if statement
Be careful…common
mistake made by
students !!
Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 1 is
true, so …
step m, step …. will be
executed
Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 1 is
false, so …
• step m, step …. will be
skipped, and
• condition 2 will be tested
Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 2 is
true, so …
step n, step …. will be
executed
Let’s recap …
Example:
if <condition_1 is true> start
step m
…
end_if
if <condition_2 is true> start
step n
…
end_if
else start
step x
…
end_else
Multiple Selection
Assume condition 2 is also
false, so …
• step n, step …. will be
skipped, and
• step x will be executed
Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
scanf(“%c”, &letter);
if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower casen”);
else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper casen”);
else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digitn”);
else
printf(“Special charactern”);
}
Is the letter a lower case?
Is the letter an upper case?
Is the letter a digit?
Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
scanf(“%c”, &letter);
if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower casen”);
else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper casen”);
else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digitn”);
else
printf(“Special charactern”);
}
(the letter is a lower case) true
(the letter is a lower case) false
(the letter is an upper case) true
(the letter is a lower case) false
(the letter is an upper case) false
(the letter is a digit) true
(the letter is a lower case) false
(the letter is an upper case) false
(the letter is a digit) false
Exercise
Develop a program for the following
problem.
Given a mark, determine its grade based on
the table below:
75 < mark < 100 grade = A
65 < mark < 74 grade = B
55 < mark < 64 grade = C
40 < mark < 54 grade = D
0 < mark < 39 grade = F
others error message
Pseudo Code
Start
Input student’s mark
If student’s mark is greater than or equal to 75
Print “Grade = A”
else If student’s mark is greater than or equal to 65 and lower than
75
Print “Grade = B”
else If student’s mark is greater than or equal to 55 and lower than
65 Print “Grade = C”
else If student’s mark is greater than or equal to 40 and lower than
55 Print “Grade = D”
else If student’s mark is greater than or equal to 0 and lower than
40 Print “Grade = F”
else
Print “Input error”
End
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 errorn”);
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 errorn”);
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 errorn”);
else
printf(“Your grade is %c”, grade);
Nested ifs
if statement that contains
other if / if - else
statements
Nested ifs
Example:
if (age > 18) {
if (age > 55)
price = 2.50; /* Price for senior citizens */
else
price = 5.00; /* Price for adults */
}
else {
if (age < 1)
price = 0.0; /* Price for infants */
else
price = 1.50; /* for children & teenagers*/
}
This price is valid for
people: age > 55
This price is valid for
people: 18 < age < 55
This price is valid for
people: age < 1
This price is valid for
people: 1 < age < 18
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?
Nested ifs - Problem
Try to understand the consequences….
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
if (age > 18) {
if (age > 55)
price = 2.50;
}
else
price = 5.00;
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;
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
switch and break
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Don’t forget the
brackets !!
Don’t forget the
curly brackets !!
Don’t forget the
colons !!
switch and break
Important Rule !!
Syntax:
switch (expression) {
case expression1:
statement1;
break;
case expression2:
statement2;
break;
…
default:
expressionX;
break;
}
Must be
INTEGER or
CHAR !
switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Assume month = 1,
so …
…this step will be
executed. Later …
…case is terminated
here. Jump to …
January
_
January
End _
switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
…this step will be
executed. Later …
March
_
March
End _
Assume month = 3,
so …
…case is terminated
here. Jump to …
switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
break;
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Now…what will
happen if this break
is taken out from the
program?
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
No more !!
switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
…this step will be
executed. Later …
February
_
February
March
_
Assume month = 2,
so …
…case is terminated
here. Jump to …
February
March
End _
…execution continues. Thus,
this step is executed . So …
switch and break
Example: switch (month) {
case 1:
printf(“Januaryn”);
break;
case 2:
printf(“Februaryn”);
case 3:
printf(“Marchn”);
break;
default:
printf(“Othersn”);
break;
}
printf(“End”);
Now…what will
happen if these
breaks are taken out
from the program?
And …
if month = 1 ?
And …
if month = 34 ?
switch and break
Example:
char jantina;
printf (“Masukkan Jantina Anda: (L/P)n”);
scanf (“%c”,&jantina);
switch (jantina) {
case ‘P’:
printf(“nJantina adalah PEREMPUANn”);
break;
case ‘L’:
printf(“nJantina adalah LELAKI”);
break;
default:
printf(“nTIDAK DAPAT DIPASTIKAN!”);
break;
}
Masukkan Jantina Anda: (L/P)
L
Masukkan Jantina Anda: (L/P)
L
Jantina adalah LELAKI
jantina ?
L
End of Lecture
Review Questions

More Related Content

Similar to Control Statement - Selection

Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015淳佑 楊
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
C programming
C programmingC programming
C programmingXad Kuain
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & ElseAbdullah Bhojani
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Chapter 4.4
Chapter 4.4Chapter 4.4
Chapter 4.4sotlsoc
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 

Similar to Control Statement - Selection (20)

Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Presentation1
Presentation1Presentation1
Presentation1
 
Chapter 1 Nested Control Structures
Chapter 1 Nested Control StructuresChapter 1 Nested Control Structures
Chapter 1 Nested Control Structures
 
C tutorial
C tutorialC tutorial
C tutorial
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
C programming
C programmingC programming
C programming
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Vcs5
Vcs5Vcs5
Vcs5
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Control flow in c
Control flow in cControl flow in c
Control flow in c
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Chapter 4.4
Chapter 4.4Chapter 4.4
Chapter 4.4
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Test2
Test2Test2
Test2
 

Recently uploaded

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...akbard9823
 
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一3sw2qly1
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
Gram Darshan PPT cyber rural in villages of india
Gram Darshan PPT cyber rural  in villages of indiaGram Darshan PPT cyber rural  in villages of india
Gram Darshan PPT cyber rural in villages of indiaimessage0108
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012rehmti665
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一Fs
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...aditipandeya
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Dana Luther
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Lucknow
 

Recently uploaded (20)

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
 
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
Gram Darshan PPT cyber rural in villages of india
Gram Darshan PPT cyber rural  in villages of indiaGram Darshan PPT cyber rural  in villages of india
Gram Darshan PPT cyber rural in villages of india
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
定制(Management毕业证书)新加坡管理大学毕业证成绩单原版一比一
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
VIP 7001035870 Find & Meet Hyderabad Call Girls Dilsukhnagar high-profile Cal...
 
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
10.pdfMature Call girls in Dubai +971563133746 Dubai Call girls
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
 

Control Statement - Selection

  • 2. if Statements if if - else if – else - if
  • 3. if Statement Syntax: if (expression) statement; or if (expression) { statement1; statement2; } Don’t forget the curly brackets, if there are more than ONE statements Don’t forget the brackets !!
  • 4. if Statement Pseudo Code: if <condition is true> start step 1 step 2 … step k end_if if Statement: if (<condition>) { statement 1 statement 2 … statement k }
  • 5. if Statement Example: int num1, num2, min; printf(“Key-in 2 numbers: “); scanf(“%d%d”, &num1, &num2); min = num1; if (num1 > num2) min = num2; printf(“Smallest: %dn”, min); Key-in 2 numbers: _ num2 ? num1 ? min ? Key-in 2 numbers: 20 15 _ 20 15 20 15 Key-in 2 numbers: 20 15 Smallest: 15 _ 20 > 15?
  • 6. if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 65?
  • 7. if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 35?
  • 8. if - else Statement Syntax: if (expression) statement1; else statement2; or if (expression) { statement1; statement2; } else statement3;
  • 9. if - else Statement or if (expression) { statement1; statement2; } else { statement3; statement4; }
  • 10. if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %dn”, min); num2 15 num1 10 min ? 10 < 15? _ 10 Smallest: 10 _
  • 11. if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %dn”, min); num2 15 num1 20 min ? 20 < 15? _ 15 Smallest: 15 _
  • 12. if - else Statement Example: if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; } printf(“Min = %d, Max = %dn”, min, max); num2 125 num1 700 min ?? 700 < 125? _ max ?? Min = 125, Max = 700 _ 125 700
  • 13. if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 21? What will the output be if the mark is 74?
  • 14. if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 74? What will the output be if the mark is 14?
  • 15. if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else { printf(“Failn”); printf(“Your mark is %d”, mark); } } What will the output be if the mark is 14? What will the output be if the mark is 70?
  • 16. Take a break … (Learn styles) Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); }
  • 17. Take a break … (Learn styles) Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } Difficult to read!!! Don’t you think so??
  • 18. Syntax: if (expression) statement; if statement Let’s recap … Syntax: if (expression) statement; else statement; if-else statement Ok now, let’s look at if – else –if statement
  • 19. if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; if-else-if statement Syntax: if (expression) statement; else if (expression) statement; else if (expression) statement; else statement; if-else-if statement
  • 20. if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; else if (expression) statement; else (expression) statement; if-else-if statement Be careful…common mistake made by students !!
  • 21. Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is true, so … step m, step …. will be executed
  • 22. Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is false, so … • step m, step …. will be skipped, and • condition 2 will be tested
  • 23. Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is true, so … step n, step …. will be executed
  • 24. Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is also false, so … • step n, step …. will be skipped, and • step x will be executed
  • 25. Multiple Selection in C Example: #include <stdio.h> int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower casen”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper casen”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digitn”); else printf(“Special charactern”); } Is the letter a lower case? Is the letter an upper case? Is the letter a digit?
  • 26. Multiple Selection in C Example: #include <stdio.h> int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower casen”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper casen”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digitn”); else printf(“Special charactern”); } (the letter is a lower case) true (the letter is a lower case) false (the letter is an upper case) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) false
  • 27. Exercise Develop a program for the following problem. Given a mark, determine its grade based on the table below: 75 < mark < 100 grade = A 65 < mark < 74 grade = B 55 < mark < 64 grade = C 40 < mark < 54 grade = D 0 < mark < 39 grade = F others error message
  • 28. Pseudo Code Start Input student’s mark If student’s mark is greater than or equal to 75 Print “Grade = A” else If student’s mark is greater than or equal to 65 and lower than 75 Print “Grade = B” else If student’s mark is greater than or equal to 55 and lower than 65 Print “Grade = C” else If student’s mark is greater than or equal to 40 and lower than 55 Print “Grade = D” else If student’s mark is greater than or equal to 0 and lower than 40 Print “Grade = F” else Print “Input error” End
  • 29. 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 errorn”);
  • 30. 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 errorn”); printf(“Your grade is %c”, grade );
  • 31. 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 errorn”); else printf(“Your grade is %c”, grade);
  • 32. Nested ifs if statement that contains other if / if - else statements
  • 33. Nested ifs Example: if (age > 18) { if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ } else { if (age < 1) price = 0.0; /* Price for infants */ else price = 1.50; /* for children & teenagers*/ } This price is valid for people: age > 55 This price is valid for people: 18 < age < 55 This price is valid for people: age < 1 This price is valid for people: 1 < age < 18
  • 34. 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?
  • 35. Nested ifs - Problem Try to understand the consequences…. if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } if (age > 18) { if (age > 55) price = 2.50; } else price = 5.00;
  • 36. 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; if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; }
  • 37. switch and break Syntax: switch (expression) { case expression1: statement1; break; case expression2: statement2; break; … default: expressionX; break; } Syntax: switch (expression) { case expression1: statement1; break; case expression2: statement2; break; … default: expressionX; break; } Don’t forget the brackets !! Don’t forget the curly brackets !! Don’t forget the colons !!
  • 38. switch and break Important Rule !! Syntax: switch (expression) { case expression1: statement1; break; case expression2: statement2; break; … default: expressionX; break; } Must be INTEGER or CHAR !
  • 39. switch and break Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); Assume month = 1, so … …this step will be executed. Later … …case is terminated here. Jump to … January _ January End _
  • 40. switch and break Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); …this step will be executed. Later … March _ March End _ Assume month = 3, so … …case is terminated here. Jump to …
  • 41. switch and break Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); Now…what will happen if this break is taken out from the program? Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); No more !!
  • 42. switch and break Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); …this step will be executed. Later … February _ February March _ Assume month = 2, so … …case is terminated here. Jump to … February March End _ …execution continues. Thus, this step is executed . So …
  • 43. switch and break Example: switch (month) { case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; } printf(“End”); Now…what will happen if these breaks are taken out from the program? And … if month = 1 ? And … if month = 34 ?
  • 44. switch and break Example: char jantina; printf (“Masukkan Jantina Anda: (L/P)n”); scanf (“%c”,&jantina); switch (jantina) { case ‘P’: printf(“nJantina adalah PEREMPUANn”); break; case ‘L’: printf(“nJantina adalah LELAKI”); break; default: printf(“nTIDAK DAPAT DIPASTIKAN!”); break; } Masukkan Jantina Anda: (L/P) L Masukkan Jantina Anda: (L/P) L Jantina adalah LELAKI jantina ? L