Académique Documents
Professionnel Documents
Culture Documents
IF … THEN Statement:
The decision logic structure uses the If/Then/Else instruction. It tells the computer that If a
condition is true, Then execute a set of instructions, or Else execute another set of
instructions. The Else part is optional, as there is not always a set of instructions if the
conditions are false. When there are no instructions for true, a Continue statement must be
used. The general form of (IF…THEN) instruction must be written as:
if (condition) then
action
14
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
if (condition) then
action 1
action 2
...
action n
Example1: Write an algorithm that computes the division of two integer numbers.
Algorithm Division( )
Begin
Input (Num1, Num2)
if (Num2 < > 0) then
output (Num1 / Num2)
End
15
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
IF…THEN…ELSE Statement:
Decisions in which you have multiple conditions that lead to one action or set of actions for
True and another action or set of actions for False are slightly more complicated than those with
single conditions. In these decisions you will use logical operators to connect the conditions. As
with decisions based on a single condition, the resultant will determine whether the True or the
False actions are taken. The decision structure becomes more complicated as the number of
conditions and/or the number of actions for a True or False resultant increases.
The general structure of IF…THEN…ELSE instruction is:
if (condition) then
true_action(s)
else
false_action(s)
Example2: Write an algorithm that inputs an integer number, and determines whether the
number is even or odd.
Algorithm Even_Odd ( )
Begin
input (Num)
if (Num mod 2) = 0 then
output (“The Number is Even”)
else
output (“The Number is Odd”)
End
16
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
Algorithm Equation ( )
Begin
input (x)
if (x > 10) then
y←x+5
else
y ← 2*x
output (y)
End
Nested IF Statement:
In algorithms containing multiple decisions, you may have to write nested If/Then/Else
instructions. Decisions using positive and negative logic use nested If/Then/Else instructions.
Decisions using straight-through logic do not. Nested If/Then/Else instructions are sets of
instructions in which each level of a decision is embedded in a level before it; like nesting
baskets, one goes inside the next. You use the nested If/Then/Else structure only when one of
several possible actions or sets of actions are to be processed.
The general form of Nested IF (Straight-through) instruction is:
if (condition 1) then
if (condition 2) then
..
if (condition n) then
action(s)
17
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
Algorithm Equation ( )
Begin
input (x)
if (x >= 10) then
if (x <= 20) then
y ← 2*x+3
else
y←0
output (y)
End
On the other hand, the general form of Nested IF (positive or negative) instruction can be written
as:
if (condition 1) then
action1
else
if (condition 2) then
action2
else
..
if (condition n) then
action a
else
action b
18
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
Example5: Write an algorithm that reads an average mark and prints the equivalent grade.
Algorithm Grade ( )
Begin
input (average)
if (average >= 90) then
output (“Your grade is excellent”)
else
if (average >= 80) then
output (“Your grade is very-good”)
else
if (average >= 70) then
output (“Your grade is good”)
else
if (average >=60) then
output (“Your grade is medium”)
else
if (average >= 50) then
output (“Your grade is pass”)
else
output (“Your grade is fail”)
End
Example6: Write an algorithm that performs the arithmetic operations (+, - , * , /) determined
by a user input for two numbers.
Algorithm Arithmetic ( )
Begin
input (Num1, Num2, Op)
if (Op = “+”) then
output (Num1 + Num2)
else
if (Op = “-“) then
output (Num1 – Num2)
else
if (Op = “*”) then
output (Num1 * Num2)
else
if (Op = “/”) then
19
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
20
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
Home Work:
1. What are the values of a, b and c at the end of the following algorithm?
Algorithm ABC ( )
Begin
a←1
b←2
c←3
if (a <= b) then
if (c > 2) then
c←2
if (c < 3) then
a←0
else
b←0
End
21
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal
Algorithm Select ( )
Begin
alpha ← 5
switch (alpha)
case 1 :
case 2 : alpha ← alpha + 2 break
case 4 : alpha ← alpha + 1
case 5 : alpha ← 2 * alpha
case 6 : alpha ← alpha + 5 break
default: alpha ← alpha - 1
End
4. Write an algorithm that reads a number and determines whether the number is positive,
negative, or zero.
5. Write an algorithm that computes the following equation:
6. Write a program that reads a character and prints out whether it is a vowel or a consonant.
22