If Statements
Topic: Algorithm
Next
Conditional Branching / Selection Statements
Conditional branching is used when there is a choice between two options. Two types of
conditional branching are IF- THEN and IF-THEN-ELSE statements
IF Then Statement
It tells your program to execute a certain section of code only if a particular test
evaluates to true
Syntax:
IF (logical test) THEN
(Carry out one or more statement)
ENDIF
back Next
Example 1 of how If- Then Statement is used:
Syntax for the if-then-else statement is:
Write an algorithm to accept the user’s age and display the message “The user is an
adult” if the user’s age is 18 or above.
Declare variable
user_age as integer
Start
print “Enter user age”
read user_age
If (user_age>=18) then
print “The user is an adult”
Endif
stop back Next
IF-Then Else Statement
back Next
if-then-else Statement
The if-then-else statement provides a secondary path of
execution when an "if" clause evaluates to false.
Syntax:
If (logical test) then
commands / statements
Else
commands/ statements
Endif
Eg. The algorithm below is checking if water has reached boiling point or not.
Declare Variables
waterTemp as integer
Start
waterTemp ←50;
If (waterTemp ==100) then
print ”water has reached boiling point”
Else
print “water has not yet reached boiling point”
endif
stop.
back Next
Example if-then-else Statement
Declare variable
finalScore as real
Start
print“Enter final score”
read finalScore
If (finalScore>=50) then
print “Pass”
Else
print “Fail”
Endif
stop
If-then-else if Statement
back Next
if-then-else if Statement
Syntax
IF <<condition>> THEN
true action
ELSE IF <<condition>> THEN
true action
ELSE
false action
Example of if-then-else if Statement
If then else if
if you are a girl, stand if you are a boy clap otherwise what are you?
IF gender == “girl” THEN
print “stand”
else IF gender == “boy” THEN
print “clap”
else
print “what are you?”
Nested if Statement
back Next
Example Nested If Statement
A nested if statement is an if statement placed inside another if statement. Nested if
statements are often used when you must test a combination of conditions before
deciding on the proper action.
Syntax for nested if
//if the third condition holds
//check if the first condition holds if (condition 3) then
do something
if (condition 1) then //if the third condition does not hold
else
//if the second condition holds
do something else
if (condition 2) then endif
Endif
do something
//if the second condition does not hold
else
do something else
endif
// if the first condition does not hold
else
Example of how to use Nest if Statement: Grade of
a student based on marks
if Statement
back Next
Declare variables Print "B"
else if (marks >= 70) then
Marks as real
Print "C"
Start
else if (marks >= 60)
print “Enter marks”
Print "D"
Read marks else
if (marks >= 50) then //E grade
if (marks >= 90) then Print "E"
endif
//A grade
else
Print "A“
//the student didn't pass
else if (marks >= 80) then
Print "F"
Endif
stop
THE END
Class Work
1. Write an algorithm that calculates the user’s age given that you’re given their birth year. If the
user is under 13 years old, print the message ‘User is a child’ otherwise ‘User is not a child’.
2. Develop an algorithm for Grace Kennedy Ltd. To determine their profit and loss. If cost price is
greater than selling price then there is a loss otherwise profit.
Formula to calculate profit and loss
Profit = S.P - C.P (Where S.P is Selling Price and C.P is Cost Price)
Loss = C.P - S.P