If Statement
If Statement
Objectives
At the end of the lesson students should be able to:
• State the purpose of IF statements in problem solving
• Identify IF constructs (IF-THEN, IF-THEN-ELSE, IF-THEN-ELSE IF)
• Identify relational operators (>, <, >=, <=, =, < >)
• Use relational operators to set Boolean expressions/conditions
• Identify AND, OR, NOT as logical operators
• Use logical operators to combine Boolean expressions/conditions
• Solve problems using IF constructs
Key Points
You would have observed in the previous lessons that :
1) the languages for algorithms include natural language,
pseudocode and textual programming languages.
2)Different algorithms can be developed to solve the same
problem
3)Natural language and pseudocode describe algorithms so that
human can understand them.
4) Algorithms described in programming languages can be
executed on a computer
Background
• Now so far we have been using the SEQUENTIAL control structure to
express algorithms. Sequential comes from the word Sequence which
means do execute the statements in the order they appear in the
algorithm. The algorithms we have created thus far are simply created
in order starting with what to do first all the way to what is done last.
• Algorithms can be more useful as they can be used to make
decisions. This type of control structure is called SELECTION. This
means that the algorithm will carry out a task or series of tasks
depending on the condition.
Categories of Selection
• There are THREE basic levels of Selection:
• IF THEN: This is used when there is only one thing that may be done
on a condition.
IF THEN ELSE: This is used when One of TWO things may be done
depending on the truthfulness or falsity of a condition
IF THEN ELSE IF: This is used if TWO or MORE things can be done
depending on the Truthfulness or falsity of an algorithm
Relational Operators
• Selection control structures requires us to use relational operators.
Relational implies comparing two or more things:
Operator meaning
AND Both conditions must be true
OR One of the two conditions must be true
For example
• In order to qualify for St. Hugh’s High School Cheerleading Team, a
student must be a student of St. Hugh’s High and is currently earning
at least a B in all courses of study.
• In order to graduate from St. Hugh’s High School a student must earn
the required number of credits or present reasonable evidence why
he or she was unable to acquire the desired credits.
IF THEN
• We will now look at the format for writing IF THEN statements in our
programs. The rest we will explore later.
• Answer:
• CONDITION: IF student get A THEN
• ACTION: I will give her AUDI
Defining Diagram (Answer)
I P O
INPUT
Info_TechGrade
IF
Info_Tech
Grade=“A”
PRINT”You got
an Audi”
STOP
Computer related Problem
• Selection can be used to solve computer related problems also.
• Question: Write an algorithm that a number and “Yes” if that number
is 5.
INPUT
num
IF num=5
Print
“YES”
STOP
Activity
1) Write an algorithm that accepts two numbers. If the first number is
greater than the second number find the sum of the two numbers.