Lesson 2: Algorithms with conditionals

This lesson explores the use of conditionals to construct algorithms.

You will see some examples on how to use conditionals to solve certain type of problems.

The approximate time to complete this lesson is 15 minutes.

Transcription

Welcome back.

In this lesson we are going to keep studying algorithms. In this case we are going to focus on algorithms that must use conditions.

As we sstudied in the previous lesson, an algorithm is a finite sequence of well-defined steps; when the steps are executed in the given order, they solve a problem. The steps can be executed without knowledge of the problem that is being solved, and they must be executed by a computer.

Remember definitions are of most importance. It is always important to follow the definitions.

In the previous lesson we studied the example below. That algorithm calculates the perimeter of square giving the one side. We can name these type of algorithm as linear algorithms. Because all the steps are executed in the given order one after the other one.

The previous approach is not possible to solve all type of problems. Most of the problems that you are going to solve as a programmer must be solved using more complex algorithms.

Example 1: Algorithm with conditionals

Let’s see this example: given two numbers print the greatest one.

In this case we must find out, from two numbers, which one is the greatest one and then print it.

Let’s see the pseudo-code.

Read number1 and number 2
If number1 > number2, then print number 1 is the greatest one
Else print number 2 is the greatest one

number1 and number 1 are the numbers that we are going to compare.

They are the input of the algorithm.

Then, if number1 is greater than number2, the computer should print number1 is the greatest one, in any other case (else), the computer should print number2 is the greatest one.

This is what is call an algorithm that use conditionals (if-then-else)

If a certain condition holds then we are going to do something, which in this case is to print number1 is the greatest one. If that condition doesn’t hold, then we are going to print number2 is the greatest one.

Let’s see the flow diagram for this algorithm.

flow diagram of an algorithm to print the greatest of two numbers

In this example, we first must write the start symbol, that is how we start every algorithm. Then we read both numbers. Then we have the symbol for the conditional statement, inside of that symbol we are going to write a condition: number1 > number2.

If the condition holds (if the result of the condition is true) we are going to print number1 and we finish.

if the condition is false, then we print number2 and finish.

As you can see, we can express the conditionals in pseudo-code and, in a flow diagram.

But the point here is that if the algorithm that needs conditional, it doesn’t matter what way you prefer to represent it, you will need to use conditionals.

Example 2: algorithms with conditionals

Let’s see another example: giving an integer number print if the number is odd or even

So, let’s first start with the pseudo-code.

read number
If number % 2 == 0, then print even
Else print odd

First, we read the number. Then we use a conditional to ask if the number is even. In this case we use the operator percentage returns reminder of the division.

If the reminder of a division of a number divided by 2 is 0, then the number is even. That’s the definition of even number. So, if that condition holds, then we print the number is even. If it doesn’t hold, then we print the number is odd.

Let’s check now the flow diagram.

flow diagram of an algorithm to print whether a number is odd or even

We use the start symbol. Then we read a number. After that we use the symbol for the conditional.

The condition in this case is number % 2 == 0. This is also read in most of the programming languages number mod 2. So, if this condition is true then we print that the number is even, and we finish. If the condition is false, then we print that the number is odd and finish.

Until now we are talking about conditions, but we didn’t go too deep in what a condition is.

In programming conditions can have only two values: true or false. Any other thing that doesn’t have these two values, cannot be used as a condition in programming.

There is also a similar term that is defined in discrete mathematics, it is called proposition.

So, conditions in programming is the same thing that propositions in discrete mathematics.

Relational and logical operators

We can use any operator that returns only those two values in a conditional statement.

Find below some of the operators, and examples, that we can use to create these conditions.

relational operators, logical operators and examples than can be used to create algorithms with conditionals

 First, we have relational operators. These are operators that are commonly used in mathematics to compare numbers. What number is less than another one, or greater than another one, etc.

Notice that to compare, we use two equal symbols (==).  This happens because we are comparing if two variables have the same value is equal to a certain value.

We use one equal (=) when we want to assign (or to modify) the value of a variable.

So, any time we are comparing using conditionals we need to use two equal symbols (==).

Algorithm with multiple conditionals

There are also logic operators: and, or and not (among others). Using them we can create what is called compound propositions in discrete mathematics.

Here we can call them like conditionals with more than one operator. If we need to use more than one operator then we have to use logic operators to combine the conditions, as you can see in the examples from the figure above.

The condition a>5 && a<8, will be true when the value on the variable a is greater than 5 and less than 8.

That’s how we create conditionals using these operators and combine them using logical operators. We can also have multiple conditionals.

For instance, let’s say we have this problem: given a number print whether the number is positive, negative or zero.

Here we have three possibilities. Therefore, we need to use more than one conditional.

Example

Let’s see how can we solve this problem.

Pseudo-code:

Read number
If number > 0 print number is positive
Else if number < 0 print number is negative
Else print number is equal to cero

As you can see, in this case we have two “if” statements. That’s where the name multiple conditionals come from. The last step of the algorithm is just an “else”, it doesn’t ask for any other condition because if the number is not greater than zero and, if it is not less than zero then it must be zero. There is only one other choice. That’s why in the last one we just write “else”.

Let’s see now how we can express multiple conditionals using a flow diagram.

flow diagram of an algorithm that uses multiple conditionals

First, we read the number. Then, we state the condition. In this case number > 0.

If it’s true, we print that the number is positive and finish. If it is false, then we must check

another condition because we have now two different choices. if it’s false the number can be zero or it can be less than zero.

So, we must use another conditional here and this condition will be number<0. If the condition is true, then we print the number is positive and finish. If the condition is false, then we print zero and we finish.

That’s how we use multiple conditionals to create algorithms.

This type of algorithm that needs multiple conditionals are very very common in programming.

Remember we are studying algorithm at a beginner level, so there are a lot more content related to algorithms. But, at this level you just need to focus here on the contents that I’m showing you: what is an algorithm, what’s the definition, what is an algorithm with a conditional statement and what are multiple conditionals.

There is another type of algorithm that is missing before we can start using or applying those algorithms in a specific programming language. That one we will study it in the next lesson.

Recap

As a summary, in this lesson we studied:

  • what is a conditional
  • how to represent conditionals using flow diagrams
  • how to use relational and logical operators to create conditionals and,
  • how we can use ultiple conditionals to solve certain types of problems

If you like this lesson leave a comment below.