Vous êtes sur la page 1sur 17

Algorithm & Programming

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:

Algorithm Pascal Meaning


= = Equal to
< < Less than
> > Greater than
<= <= Less than or equal to
>= >= Greater than or equal to
<> <> Not equal to
= = Not equal to
Boolean Operators
• We use the following Boolean operators in programming.

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.

• Here is the format in algorithm:


• IF (condition) THEN
• Do Something
• ENDIF
Let’s take a closer look at an IF THEN Selection
• A mother says to her child if you get an A in Information Technology I
will give you a brand new AUDI.
• There is only one thing that will happen BUT it depends on if the
condition is met. Therefore it is IF THEN

• Answer:
• CONDITION: IF student get A THEN
• ACTION: I will give her AUDI
Defining Diagram (Answer)
I P O

Info_TechGrade 1)PRINT” Please enter grade for Audi


Info_TechGrade ”
2) INPUT Info_TechGrade
3) IF Info_TechGrade= “A” THEN
4) PRINT “You an Audi”
5) ENDIF
Pseudocode (answer)
START
Declare Info_TechGrade as REAL
PRINT ‘‘ Please enter grade for Info_TechGrade”
INPUT Info_TechGrade
IF Info_TechGrade = “A” THEN
PRINT “You an Audi”
ENDIF
STOP
Flowchart (answer)
START

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.

• Problem: Print yes only if the number is 5


• Input: number
• Output : “Yes” if it is 5
Defining Diagram
I P O
num PRINT “Enter a number” “Yes”
INPUT num
IF num = 5 Then
PRINT “yes”
Flow Chart
START

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.

Vous aimerez peut-être aussi