You are on page 1of 28

SELECTION CONTROL

STRUCTURES
IN C USING FLOWCHART AND
PSEUDOCODE

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 1


SINGLE WAY SELECTION

IF STATEMENT IN C

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya


C++ if Statement
If (test Expression)
{
// statements
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya


• The if statement evaluates the test expression
inside parenthesis.
• If test expression is evaluated to true,
statements inside the body of if is executed.
• If test expression is evaluated to false,
statements inside the body of if is skipped.

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya


• The if selection statement is a single-selection
statement because it selects or ignores a single action
• The if selection statement either performs (selects)
an action if a condition is true or skips the action if
the condition is false.

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 5


Single way selection: If-statement
// Program to display remark well done if the score entered is greater than 50.

FLOWCHART
Pseudo code Start
Start
Input score(70)
If score>= 50 Input score
print Well
done,your score YES
is 70 If
score Output Well done ,
End if
>=50 score = 70
Print this is
selection only NO
statement Output this is
Stop single selection
statement

Stop

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 6


Single way selection: If-statement

// Program to display remark well done if the score entered is greater than 50.

#include <stdio.h>
int main() {
int score; Output for s core of 90

Well done, your sc ore is: 90


printf("Enter an integer: ");
scanf("%d", &score); This is singleway selection using
only if statement
// true if number is less than 0 if
Output for sc ore of 49
(number > =50) {
printf("Well done , your score is: %d.\n", score ); This is singleway selection using
} only if statement

printf("This is single way selection using only if statement");

return 0;
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 7


TWO WAY SELECTION

IF ELSE STATEMENT IN C++

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 8


• The if...else selection statement performs an
action if a condition is true or performs a
different action if the condition is false.

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 9


Two-way selection statements : using If-else
// Program to check score entered and then give grade pass if score equal or greater than 50 and fail is the
score is less than 50
FLOWCHART
Pseudo code Start
Start
Input score(70)
If score>= 50 Input score
print pass
Else YES
Print Fail If
End if score Output pass
Stop >=50
NO

Output Fail

Stop

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 10


Two-way selection statements : using If-else
// Program to check score entered and then give grade pass if score equal or
greater than 50 and fail is the score is less than 50
#include <stdio.h> Output fo r score of 90
int main() {
PASS
int score;
Output fo r score of 49
printf("Enter score: ");
scanf("%d", &score); FAIL

// check score value


if (score>= 50) {

printf("PASS");
}
else {
printf(“FAIL”);
}

return 0;
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 11


• Write a program to compute amount to be paid as VAT
for goods bought in Amani Shop as follows
If goods amount to 25000/= and above the tax is 7.5%
otherwise if amount below 25000/= tax is imposed is 5%
of the amount.

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 12


Two-way selection statements : using If-else
Start

Pseudo code FLOWCHART


Start
Input Amount
Input amount
If amount >= 25000
tax_computed_amount= (25000+(25000*7.5)/100)); Output
YES
print tax_computed_amount If
Compute
tax_comput
ed_amount
tax_computed_amount=
Else amount>=25000
(25000+(25000*7.5)/100));
tax_computed_amount= (25000+(25000*5.0)/100));
NO
print tax_computed_amount Compute
tax_computed_amount=
End if (25000+(25000*5.0)/100));

Stop
Output
tax_computed_amount

Stop
Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 13
Two-way selection statements : using If-else
#include <stdio.h>
int main ()
{ OUTPUT FOR 25000/=

// local variable declaration: Computed amount plus tax is: 26875


int amount; OUTPUT FOR 24000/=
int tax_computed_amount;
printf("\ Enter the Goods amount “); Computed amount plus tax is: 26250

scanf(“%d”,&amount);
// check the condition
if( amount >= 25000 )
{
// if condition is true then print the following
tax_computed_amount= (25000+(25000*7.5)/100));
printf(“Computed amount plus tax is: %d" ,tax_computed_amount);

else
}
tax_computed_amount= (25000+(25000*5.0)/100));
printf(“Computed amount plus tax is: %d" ,tax_computed_amount);
}
return 0;
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 14


Two-way selection statements : using If-else
Start

Pseudo code FLOWCHART


Start
INPUT age
Input AGE
If AGE>= 18
print You are eligible for voting YES
Else IF OUTPUT
age>= 18 You are eligible for voting
print You are not eligible for voting
Stop NO

OUTPUT
You are not eligible for voting

Stop

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 15


Two-way selection statements : using If-else

#include <stdio.h>
int main()
{
int age;
printf("Enter your age:"); OUTPUT FOR age 17
scanf("%d",&age);
if(age >=18){ You are not eligible for voting
printf("You are eligible for voting"); OUTPUT FOR age 19
}
else You are eligible for voting
{
printf("You are not eligible for voting");
}
return 0;
}

C PROGRAM TO DETERMINE ELIGIBILITY OF A VOTER IN TERMS OF AGE

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 16


MULTIPLE WAY SELECTION

IF THEN ELSE IF

STATEMENTS IN C

• KEY NOTE: if-else-if statement is used when we need to check multiple


conditions.

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 17


• Write a program to ask a student to enter his or her score and the program display the appropriate answer
depending on the following creteria

Score Message

>= 90 Got A
passed
>= 80 Got A
passed
>= 70 Got B
passed
>= 60 Got C
passed
>= 50 Got D
passed
>=40 Got E
Failed
Else Got INVALID GRADE
Confirm with examination office

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 18


Pseudo code Flowchart Start

Begin INPUT SCORE


Enter score
If grade >= 90 Then
You got an A IF OUTPUT
You Passed SCORE>= You got an A
90 You Passed
ElseIf grade >= 80 Then
DISPLAY You got an A
ELSEIF
DISPLAY You Passed SCORE>= OUTPUT
ElseIf grade >= 70 Then 80 You got an A
DISPLAY You got an B You Passed
DISPLAY You Passed
ELSEIF
ElseIf grade >= 60 Then SCORE>= OUTPUT
DISPLAY You got an C 70 You got an B
DISPLAY You Passed You Passed
ElseIf grade >= 50 Then
ELSEIF
DISPLAY You got an D
DISPLAY You Failed
SCORE= OUTPUT
60 You got an C
ElseIf grade >= 40Then You Passed
DISPLAY You got an E
ELSEIF
DISPLAY You Failed SCORE>= OUTPUT
Else 50 You got an D
You Failed
DISPLAY You got an INVALID GRADE
ELSEIF
DISPLAY Confirm with Examination Office SCORE>=
OUTPUT
You got an E
End If 50
You FAILED
end
OUTPUT
INVALID GRADE

Stop
Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 19
#include <stdio.h>
int main(){
int mark;

Printf("Enter Your Score for STRUCTURED PROGRAMMING\n”);


scanf(“%d”,& mark);
if(mark >= 90) OUTPUT FOR score 85
{
printf( "You got an A \n”; You got an A
printf ( "You Passed!" );
} You Passed!"
else if(mark >= 80)
{
printf( "You got an A \n”; OUTPUT FOR score 39
printf ( "You Passed!" );
}
else if(mark >= 70) You got an E
{ You Failed!"
printf( "You got an B \n”;
printf ( "You Passed!" );
} OUTPUT FOR score 120
else if(mark >= 60)
{
printf( "You got an C \n”;
You got an INVALID GRADE
printf ( "You Passed!" ); Confirm with Examination Office
}
else if(mark >= 50)
{
printf( "You got an D" );
printf ( "You Failed!" );
}
else if(mark >= 40)
{
printf( "You got an E" );
printf ( "You Failed!" );
}
else
{
printf( "You got an INVALID GRADE" );
printf ( “Confirm with Examination Office" );
}
return 0;
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 20


MULTIPLE WAY SELECTION

C++ switch...case syntax

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 21


• Write a program to ask a student to enter his or her score and the program using switch case statement it
displays the correct grade in reference to the score given

Score range Grade

80 -100 DISTINCTION

60 - 69 CREDIT

50 - 59 PASS

40 - 49 WEAK PASS

0 - 39 FAIL

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 22


Pseudo code Start Flowchart
Begin
Input a score
INPUT SCORE
Case based on score
Case 80 -100 OUTPUT
Display “DISTINCTION” Case 80 - Break
You got an A
Case 60 -79 100 You Passed
Display “CREDIT”
Case 50 - 59 Case 60 - OUTPUT
Display “PASS” 79 You got an A Break
Case 40 -49 You Passed
Display “WEAK PASS”
Case 0 - 39 Case 50 -
Display “FAIL” 59 OUTPUT
You got an B Break
Default You Passed
Display “INVALID ”
End Case Case 40 -
49 OUTPUT Break
End You got an C
You Passed

Case 0 – OUTPUT
39 You got an E Break
You FAILED

OUTPUT
Default INVALID GRADE Break

Stop
Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 23
C program to illustrate using range in switch case
// C program to illustrate
// using range in switch case
#include <stdio.h> Role of break key word
int main()
{
int score;
Printf("Enter Your Score for STRUCTURED PROGRAMMING\n”);
scanf(“%d”,&score);
switch (score)
{
case 70 ... 100:
printf(“DISTINCTION”);
break;
case 60 ... 69:
printf(“CREDIT”);
break;
case 50 ... 59:
printf(“PASS”);
break;
case 40 ... 49:
printf(“WEAK PASS”);
break; OUTPUT FOR score 85
case 0... 39:
printf(“FAIL”); DISTNCTION
break;
OUTPUT FOR score 39
default:
FAIL
printf(“SCORE NOT IN RANGE”);
break;
}
OUTPUT FOR score 120
return 0;
SCORE NOT IN RANGE
}

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 24


Example of Nested if statement

Nested if statement is an if statement inside


another if body

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 25


Example of Nested if statement

Write a program that will ask the user to enter his or age and then enter the
gender, the program checks the age entered and the gender , if the age is equal
to or above 18yrs and the gender is “famale” , the program display “You are
eligible for voting” otherwise the program will display “You are not
eligible for voting”

C PROGRAM TO DETERMINE ELIGIBILITY OF A VOTER IN TERMS OF AGE

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 26


Two-way selection statements : using
FLOWCHART
Pseudo code Begin
Start
Input AGE INPUT age
INPUT gender
If AGE>= 18
If GENDER = “FAMALE”
print You are eligible for voting NO
IF
Else age>= 18
print You are not eligible for voting
END IF YES
END IF
YES
IF OUTPUT
Stop You are eligible for
gender=
voting
“Female”
NO
OUTPUT
You are not eligible for voting

End
Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 27
NESTED IF CONCEPT IN C
#include <stdio.h>
int main()
{
int age;
string gender;
printf("Enter your age:");
scanf("%d",&age);
OUTPUT FOR age 17, gender
printf("Enter your gender:");
male
scanf("%6s",&gender);
You are not eligible for voting
if(age >=18){
// nested if OUTPUT FOR age 19, gender
if(gender=“famale”) { female
printf("You are eligible for voting"); You are eligible for voting
}
}
else
}
printf("You are not eligible for voting");
}
return 0;
}
C PROGRAM TO DETERMINE ELIGIBILITY OF A VOTER IN TERMS OF AGE

Succeeding with Technology, By Wrickly John Kebaso : Republic of Kenya 28

You might also like