We explain the different structures by giving examples both in flowchart and in pseudocode.
Conditional structures compare a variable against another value(s), so that based on the result of this comparison, a course of action is followed within the program. It is worth mentioning that the comparison can be made against another variable or against a constant, as needed. There are three basic types, simple, double and multiple.
Simple:
Simple conditional structures are known as “decision making”. These decision making have the following form:
Pseudocode: Flowchart: Doubles:
Double conditional structures allow you to choose between two possible options or alternatives depending on whether or not a certain condition is fulfilled. It is represented as follows:
Pseudocode: Flowchart: Where:
Yes: Indicates the comparison command
Condition : Indicates the condition to evaluate
Then : Precedes the actions to be performed when the condition is met
Instruction(s): These are the actions to be performed when the condition is fulfilled or not
if not : Precedes the actions to perform when the condition is not met
Depending on whether the comparison is true or false, one or more actions can be performed.
multiple:
Multiple comparison structures are specialized decision making that allow comparing a variable against different possible results, executing a series of specific instructions for each case. The common form is as follows:
Pseudocode: Flowchart: Multiple (In case of):
The multiple comparison structures, is a specialized decision making that allows to evaluate a variable with different possible results, executing for each case a series of specific instructions. The form is as follows:
Pseudocode: Flowchart: Let’s see some examples where all of the above apply:
Carry out an algorithm where the user’s age is requested; If he is of legal age, a message indicating this should appear. Express it in Pseudocode and Flowchart.
Pseudocode: Flowchart: It is requested to read three notes from the student, calculate their final score in a range of 0-5 and send a message stating whether the student passed or failed the course. Express the algorithm using pseudocode and flowchart.
Pseudocode:
BEGINNING
Not1, Not2, Not3 :REAL
Def: REAL
READ Note1, Note2, Note3
Def ß (Not1 + Not2 + Not3) /3
Yes Def Write “Failed the course”
Otherwise
Write “Passed the course”
End yes
FINISH
Flowchart:
You want to write an algorithm that asks for the height of a person, if the height is less than or equal to 150 cm, send the message: “Person of short height”; if the height is between 151 and 170 write the message: “Person of medium height” and if the height is greater than 171 write the message: “Tall person”. Express the algorithm using pseudocode and flowchart.
Pseudocode:
BEGINNING
Height: WHOLE
WRITE “What is your height? ”
READ Height
If Height, WRITE “person of short height”
Otherwise
If Height, WRITE “person of average height”
Otherwise
IF Height>170 THEN
WRITE “tall person”
End yes
End yes
End yes
FINISH
It is important to be neat in the code you write!
Flowchart:
Given a number between 1 and 7, write its corresponding day of the week like this:
1- Monday 2- Tuesday 3- Wednesday 4- Thursday 5- Friday 6- Saturday 7- Sunday
Express the algorithm using pseudocode and flowchart.
Pseudocode: Pseudocode:
BEGINNING
All day
WRITE “Say a number to write your day”
LEA Day
In-case-of-Dia do
Case 1: WRITE “Monday”
Case 2: WRITE “Tuesday”
Case 3: WRITE “Wednesday”
Case 4: WRITE “Thursday”
Case 5: WRITE “Friday”
Case 6: WRITE “Saturday”
Case 7: WRITE “Sunday”
ELSE: WRITE “You wrote a number outside the range 1-7”
End-Case
FINISH
Flowchart: