You are on page 1of 47

Computer Fundamentals

and
Programming
MODULE 6
SELECTION STRUCTURES
• Identify the selection structures in C.
• Discuss if, if-else, if-else-if statements.
• Use if, if-else, if-else-if statements in decision
making.
• Discuss the nested if statements.
• Discuss switch statement.
• Explain the syntax of switch statement.
• Differentiate switch and if-else statement.
• Discuss the use of default and break keywords in
switch statement.
MODULE 6 – SUBTOPIC 1
CONDITIONAL STATEMENTS
PART 1
• Selection structures are used to perform ‘decision making‘
and then branch the program flow based on the outcome of
decision making.

• Selection structures are implemented in C with if, if else and


switch statements.

• if and if else statements are 2 way branching statements


where as switch is a multi branching statement.
• An if statement can be followed by an optional else statement,
which executes when the Boolean expression is false.

• If the Boolean expression evaluates to true, then the if


block will be executed, otherwise, the else block will be
executed.

• C programming language assumes any non-zero and non-


null values as true, and if it is either zero or null, then it is
assumed as false value.
• An if statement can be followed by an optional else
if...else statement, which is very useful to test various
conditions using single if...else if statement.

• When using if...else if..else statements, there are few points to


keep in mind :
• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the
else.
• Once an else if succeeds, none of the remaining else if's or else's will
be tested.
if if – else - if
if - else
The structure is similar to single selection (flowchart)

Syntax: Don’t forget the brackets


if (expression)
statement;
or
if (expression) { Don’t forget the curly brackets
statement1;
statement2;
}
The similarity between single selection structure
and if statement:
Single Selection: if Statement:
if <condition is true> start if (<condition>) {
step 1 statement 1
step 2 statement 2
… …
step k statement k
end_if }
Example:
int num1, num2, min;
printf(“Key-in 2 numbers: “); 20 > 15? num1 ?
20
scanf(“%d%d”, &num1, &num2);
min = num1; num2 15
?
if (num1 > num2)
min = num2; min 15
20
?
printf(“Smallest: %d\n”, min);

Key-in 2 numbers: 20
_ 15
_Smallest: 15
_
Example:
int grd;
92 > 80?
printf(“Grade: “); grd ?
92
scanf(“%d”, &grd);
if (grd > 80) {
printf(“Category: Excellent\n”);
printf(“Congratulations!”);
}

Grade:92
Mark: _92
_
Category: Excellent
Congratulations!
Example: What will the output be if the mark is
void main() { 65?

int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);
printf(“Your mark is %d”, mark);
}
Example: What will the output be if the mark is
void main() { 35?

int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);
printf(“Your mark is %d”, mark);
}
The structure is similar to double selection (flowchart)
Syntax:
if (expression)
statement;
else
statement;
or
if (expression) {
statement1;
statement2;
} else
statement3;
or
if (expression) {
statement1;
statement2;
} else {
statement3;
statement4;
}
The similarity between double selection structure
and if - else statement:
Double Selection: if Statement:
if <condition is true> start if <condition> {
step 1 statement 1
… …
step k statement k
end_if }
else start else {
step 1 statement 1
… …
step n statement n
end_else }
Example:
if (num1 < num2)
10 < 15?
num1 10
min = num1;
else num2 15
min = num2;
min 10
?
printf(“Smallest: %d\n”, min);

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

_Smallest: 15
_
Example: num1 700
if (num1 < num2) { 700 < 125?
min = num1; num2 125
max = num2;
}
else { min 125
??
min = num2;
max = num1; max 700
??
}
printf(“Min = %d, Max = %d\n”, min, max);

_ = 125, Max = 700


Min
_
Example:
What will the output be if the mark is
void main() { 21?
int mark;
printf(“Mark: “);
What will the output be if the mark is
scanf(“%d”, &mark); 74?
if (mark >= 50)
printf(“Pass\n”);
else
printf(“Fail\n”);
printf(“Your mark is %d”, mark);
}
Example:
What will the output be if the mark is
void main() { 14?
int mark;
printf(“Mark: “);
scanf(“%d”, &mark); What will the output be if the mark is
70?
if (mark >= 50)
printf(“Pass\n”);
else {
printf(“Fail\n”);
printf(“Your mark is %d”, mark);
}
}
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);
}
Example:
void main() { Difficult to read!!!
int mark; Don’t you think so??
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);
else
printf(“Fail\n”);
printf(“Your mark is %d”, mark);
}
if-else-if statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
if-else-if statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression) Be careful…common mistake made by
statement; students
else (expression)
statement;
Multiple Selection in C
Example:
#include <stdio.h>
int main( ) { (the letter is a lower case) true
char letter;
scanf(“%c”, &letter); (the
(theletter
letterisisaalower
lowercase)
case)false
false
if (letter >= ‘a’ && letter <=(the
(the letter
‘z’ letter
) isisananupper
uppercase)
case)false
true
(the letter is a digit) true
printf(“Lower case\n”);
else if (letter >= ‘A’ && letter (the<= letter
‘Z’)is a lower case) false
(the letter is an upper case) false
printf(“Upper case\n”);
(the letter is a digit) false
else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digit\n”);
else
printf(“Special character\n”);
}
if statement that contains
other if / if - else statements
Example: This price is valid for people: 18
age<>age
55
< 55
if (age > 18) {
if (age > 55)
price = 2.50; /* Price for senior citizens */
This price is valid for people: age < 1
else
price = 5.00; /* Price for adults */
} This price is valid for people: 1 < age <
18
else {
if (age < 1)
price = 0.0; /* Price for infants */
else
price = 1.50; /* for children & teenagers*/
}
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?


By default, else will be attached to the nearest if

if (age > 18) if (age > 18) {


if (age > 55) if (age > 55)
price = 2.50; price = 2.50;
else else
price = 5.00; price = 5.00;
}
MODULE 6 – SUBTOPIC 2
CONDITIONAL STATEMENTS
PART 2
• The syntax of the switch statement is a bit peculiar.

• Its objective is to check several possible constant values for an


expression.

• Something similar to what we did at the beginning of this


section with the concatenation of several if and else
if instructions.
• Its form is the following:
switch (expression)
{
case constant1: group of statements 1;
break;
case constant2: group of statements 2;
break;
.
.
.
default: default group of statements;
}
• It works in the following way: switch evaluates expression and
checks if it is equivalent to constant1, if it is, it executes group
of statements 1 until it finds the break statement. When it finds
this break statement the program jumps to the end of
the switch selective structure.

• If expression was not equal to constant1 it will be checked


against constant2. If it is equal to this, it will execute group of
statements 2 until a break keyword is found, and then will jump
to the end of the switch selective structure.
• Finally, if the value of expression did not match any of the
previously specified constants (you can include as
many case labels as values you want to check), the program
will execute the statements included after the default: label, if it
exists (since it is optional).
• Both of the following code fragments have the same behavior:

Image Source : http://www.cplusplus.com/doc/oldtutorial/control/


The structure is similar to multiple selection (flowchart)
Syntax: Don’t forget the brackets !!
switch switch
(expression) {
(expression) {
case expression1:
statement1;
break;
case espression2: Don’t
Don’t
forget
forget
thethe
curly
colons
brackets
!!
statement2; !!
break;
break;


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

default:
expressionX;
break;
}
Example: switch (month) { Assume month = 1, so …
case 1:
printf(“January\n”);
break;
case 2: …this step will be executed. Later
printf(“February\n”); … here. Jump to
…case is terminated
break; …
case 3:
printf(“March\n”);
break;
default: January
printf(“Others\n”);
break; _ _
End
}
printf(“End”);
Example: switch (month) { March
case 1: _ _
End
printf(“January\n”);
break;
case 2:
printf(“February\n”);
break;
Assume month = 3, so …
case 3:
printf(“March\n”);
break;
default: …this step will be executed. Later
printf(“Others\n”); … here. Jump to
…case is terminated
break; …
}
printf(“End”);
Example: switch (month) {
case 1:
printf(“January\n”);
break;
case 2:
printf(“February\n”);
Now…what will
No happen
more !! if this break
break; is taken out from the program?
case 3:
printf(“March\n”);
break;
default:
printf(“Others\n”);
break;
} }
printf(“End”);
printf(“End”);
Assume month = 2, so …
Example: switch (month) { February
case 1: _
March
printf(“January\n”);
break; _ _
End
case 2:
printf(“February\n”);

case 3: …this step will be executed. Later


printf(“March\n”); …
break;
default:
printf(“Others\n”);
…execution continues. Thus, this step is …case is terminated here. Jump to
executed . So …
break; …
}
printf(“End”);
Example: switch (month) {
case 1:
printf(“January\n”);
break;
case 2:
printf(“February\n”);
Now…what will happen if these
And
And…… if month
if month
= 1=
34
? ? breaks are taken out from the
case 3: program?
printf(“March\n”);
break;
default:
printf(“Others\n”);
break;
}
printf(“End”);
[1] : Webber, Z. (2018). The Ultimate Beginner’s Guide To Learn And Understand C++
Programming Effectively. Createspace Independent Publishing
[2] : Malik, D.S. (2018). C++ Programming Including Data Structures. 8th Ed. Cengage Learning
[3] : Alvin, C. (2018). Computer Programming: Learn Any Programming Language In 2 Hours.
[4] : Gregoire, M. (2018). Professional C++. Wrox
[5] : Bancila, M. (2017). Modern C++ Programming Cookbook: Recipes To Explore Data Structure,
Multithreading, And Networking In C++17. Packt Publishing
[6] : Connors, K. (2017). Computer Programming for Beginners: Learning How To Code Step By
Step. CreateSpace Independent Publishing
[7] : McGrath, M. (2017). C++ Programming in Easy Steps. 5th Ed. Easy Steps Limited
[8] : Zak, D. (2015). An Introduction to Programming with C++. 8th Ed. Cengage Learning
[9] : Perry, G. (2014). C Programming : Absolute Beginner’s Guide. 3rd Ed. Que Publishing
[10] : Deitel, P.J. (2012). C++ How to program. Pearson Prentice Hall|
[11] : Prata, S. (2012). C++ Primer Plus. 6th Ed. Addison-Wesley Professional
ASK ANY QUESTION RELATED TO
OUR TOPIC FOR TODAY.

You might also like