Vous êtes sur la page 1sur 9

Algorithm Design and Programming Techniques Lecture 2

1st class Asst. Lecture Omar Nowfal

Decision (condition) structures:


There are four types of decision instructions and they are:
1. if ... then
2. if… then…else
3. Nested if
4. Switch

 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

And the flowchart of this instruction is:

14
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal

If we wish to execute several actions if the condition is true:

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

Example3: Example: Write an algorithm that computes the following equation:

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)

This type of nested if statements can also be written as follows:


if (condition1 AND condition2 AND … AND condition n) then
action

Example4: Write an algorithm that computes the following equation:

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

Note: The two if statements can be replaced by the statement:


if ((x >= 10) AND (x <= 20) then
y ← 2*x+3

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

if (Num2 < > 0) then


output (Num1 / Num2)
else
output (“Error division by zero”)
else
output (“Invalid Operation”)
End

 SWITCH … CASE Statement


Sometimes a solution to a problem requires the computer to select one action from a set of
actions. This kind of solution can be designed through the decision logic structure, but the case
logic structure is more efficient for this purpose. The case logic structure is a special variation
of the decision structure, although it is different enough to present here separately. This structure
is used in many business-related problems, such as those dealing with types of accounts in a
bank, types of insurance available to an employee, or the selection of a bonus. The case logic
structure is made up of several sets of instructions, usually only one of which will be selected
by a condition and then executed by the computer. Through the use of the case logic structure,
a programmer can enable a user to enter the value of a variable from the keyboard, from a file,
or use a calculated value to select one of several options in a list.
The general form of SWITCH … CASE statement can be written as:

switch (variable or expression)


case constant1 : actionList1 break
case constant2 : actionList2 break
..
case constantN : actionListN break
default: actionList

20
Algorithm Design and Programming Techniques Lecture 2
1st class Asst. Lecture Omar Nowfal

Example7: Rewrite the algorithm of the example6 using Switch-Case Statement.


Algorithm Arithmetic ( )
Begin
input (Num1, Num2, Op)
switch (Op)
case “+” : output (Num1 + Num2) break
case “-“ : output (Num1 – Num2) break
case “*” : output (Num1 * Num2) break
case “/” : if (Num2 < > 0) then
output (Num1 / Num2)
else
output (“Error division by zero”)
break
default : output (“Invalid Operation”)
End

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

2. What is the value of alpha after executing the following algorithm?

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

3. What is the value of beta after executing the following algorithm?


Algorithm Try ( )
Begin
beta ← 3
switch (beta)
case 3 : beta ← beta + 3
case 1 : beta ← beta + 1 break
case 5 : beta ← beta + 5
case 4 : beta ← beta + 4
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

Vous aimerez peut-être aussi