0% found this document useful (0 votes)
16 views29 pages

Chapter 3.0

The document discusses selection statements in programming. It defines selection statements as control statements that allow non-sequential execution of code based on conditional logic. The document covers the different types of selection statements in C programming - IF statements, IF-ELSE statements, and SWITCH statements. It provides examples of each type of statement and compares IF-ELSE and SWITCH statements. The document also discusses nested IF-ELSE statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views29 pages

Chapter 3.0

The document discusses selection statements in programming. It defines selection statements as control statements that allow non-sequential execution of code based on conditional logic. The document covers the different types of selection statements in C programming - IF statements, IF-ELSE statements, and SWITCH statements. It provides examples of each type of statement and compares IF-ELSE and SWITCH statements. The document also discusses nested IF-ELSE statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CHAPTER 3:

SELECTION
STATEMENTS
DEC20012
CHAPTER 3
3.1 Remember selection statements
3.1.1 Define control statements
3.1.2 List types of control statements
3.1.3 Define IF, IF-ELSE and SWITCH statements
3.1.4 Describe selection statements
3.1.5 Describe structure of simple IF, IF-ELSE, Nested IF-ELSE and
SWITCH statements

3.2 Understand selection statements


3.2.1 Differentiate simple IF, IF-ELSE, Nested IF-ELSE and SWITCH
statements
CHAPTER 3
3.3 Apply selection statements
3.3.1 Construct programs that use simple F, IF-ELSE, Nested IF-
ELSE and SWITCH statements

3.4 Use selection statement to demonstrate the I/O operation

3.5 Use Variant of selection statement to demonstrate the hardware and the
software operation (example: LED Chase Effect)
3.5.1 Initialization step
3.5.2 Input step
3.5.3 Process step
3.5.4 Output step
3.5.5 Termination step
Control Structure
• Refer to the order of execution of
instruction in a program.
• C provides several programs for control
statement and lets to execute the
instructions in a non-sequential tasks
(skipping a block of instructions or execute
a block of instructions repetitively.
Control structure
• 4 basic control structures:-
A. sequence structure
B. selection structure
C. repetition structure (iteration/ looping)
D. Jumps statements

5
A. Sequence Structure
• the simplest of all the structures.
• The program instructions are executed one by one, starting from
the first instruction and ending in the last instruction as in the
program segment.
 
Example :
x = 5; (S1)
y = 10; (S2)
Total = x * y; (S3)
printf(“ Total =%d”, Total); (S4)

Entry S1 S2 S3 S4
Exit

6
B. Selection Structure
• The selection structure allows to be
executed non-sequentially.
• It allows the comparison of two
expressions, and based on the
comparison, to select a certain course of
action.

7
B. Selection Structure(2)
• There are three types of selection
statements:
– if statement
– if-else statement
– switch statement.

8
Selection Structure
C provides three (3) types of selection structures in the form of
statements.
• if selection statement : either performs (selects) an action if a
condition is true or skips the action if the condition is false.
• if…else selection statement : performs an action if a condition is
true and performs a different action if the condition is false.
• switch selection statement : performs one of many different
actions depending on the value of an expression
Selection Structure (if)
This is used to decide whether to do something at a special point, or to
decide between two courses of action.
if selection statement : “For example, suppose the passing grade on an
exam is 60.”
– Pseudo-code:

– C syntax:

– Flowchart:
Selection Structure (if)
An example of if statement positive and negative number
Selection Structure (if)
Another example of if statement: compare 2 numbers
Selection Structure (if)
Another example of if statement: check ur gender
Selection Structure (if…else)
• If-else selection statement : “For example, suppose the passing
grade on an exam is 60.”
– Pseudo-code:

– C syntax:

– Flowchart:
Selection Structure (if…else)
An example of if…else statement positive and negative number
Selection Structure (switch…case)
• switch selection statement :
– Flowchart:
Selection Structure (switch…case)
• switch selection statement:
Selection Structure (switch…case)
An example of switch: [to switch a color]
Inform the
user

store number in
“color”

Choose
between
number 1,2,3
only!!
Selection Structure (switch…case)
An example of switch: to check a grade!
Inform the
user

store character in
“grade”

Choose
between
character
A,B,C,D,F
only!!
Exercise
Create a program if you;
enter number 1, it will display Sunday
enter number 2, it will display Monday
enter number 3, it will display Tuesday
enter number 4, it will display Wednesday
enter number 5, it will display Thursday
enter number 6, it will display Friday
enter number 7, it will display Saturday
#include <stdio.h>
#include <stdlib.h>

int main()
{
int day;
printf("============================== \n");
printf("Sila pilih no hari: \n");
printf("\t1. Ahad\n");
printf("\t2. Isnin\n");
printf("\t3. Selasa\n");
printf("\t4. Rabu\n");
printf("\t5. Khamis\n");
printf("\t6. Jumaat\n");
printf("\t7. Sabtu\n");
printf("============================== \n");

scanf("%d",&day);

switch (day)
{
case 1:
printf("Hari Ahad hari yang indah\n");
break;

case 2:
printf("Hari Isnin mula berkerja\n");
break;

case 3:
printf("Hari Selasa teruskan berusaha\n");
break;

case 4:
printf("Hari Rabu, makan nasi kerabu\n");
break;
Nested if…else statements
• Nested if…else statements test for multiple cases by placing if…
else statements inside if…else statements.
• For example, the following pseudocode statement will print A for
exam grades greater than or equal to 90, B for grades greater than
or equal to 80, C for grades greater than or equal to 70, D for
grades greater than or equal to 60, and F for all other grades.

This pseudocode
may be written in C
as
Nested if…else statements
Nested if…else statements

if (condition)

else if (condition)
Depend on how
else if (condition) Many condition
You want to use
else if (condition)

else
Nested if…else
Syntax:
if(condition1)
Condition True {
Statement1
1 statement1
False
}

Condition True
Statement2 else if(condition2)
2
False {
statement2
True
Condition
Statement3 }
3
False
else if(condition3)
{
Statement4 statement3
}

else
statement4 25
if
comparison Nested if else
comparison
Nested if else switch case
Comparison if..else & switch..case
BASIS FOR
IF-ELSE SWITCH
COMPARISON
Basic Which statement will be executed Which statement will be executed is
depend upon the output of the decided by user.
expression inside if statement.
Expression if-else statement uses multiple switch statement uses single
statement for multiple choices. expression for multiple choices.
Testing if-else statement test for equality as switch statement test only for equality.
well as for logical expression.
Evaluation if statement evaluates integer, switch statement evaluates only
character, pointer or floating-point character or integer value.
type or boolean type.
Sequence of Either if statement will be executed or switch statement execute one case
Execution else statement is executed. after another till a break statement is
appeared or the end of switch
statement is reached.
Default Execution If the condition inside if statements is If the condition inside switch
false, then by default the else statements does not match with any
statement is executed if created. of cases, for that instance the default
statements is executed if created.
Editing It is difficult to edit the if-else It is easy to edit switch cases as, they
statement, if the nested if-else are recognized easily.
statement is used.
3.5 Use Variant of selection statement to demonstrate the hardware and the
software operation (example: LED Chase Effect)
3.5.1 Initialization step
a t
3.5.2 Input step
3.5.3 Process step
l a i n k s
3.5.4 Output step

e xp e e )
3.5.5 Termination step

i l l f w 1 4
W nd o e k
e w e
e
th fore
(b e

You might also like