Conditional statements

Conditional statements allow you to create much smarter algorithms, making them make important decisions during their execution.

11/09/2023

youtube-thumb
algol-youtube-play

Before learning about conditional statements, it is first recommended that you already know how arithmetic operators work.

If you are new to arithmetic operators, you should certainly read this article in which we explain in detail about the subject.

But let’s get down to business!


What are conditional statements?

In computing, a conditional statement is an element that provides the power to put more intelligence into the algorithms, as they give the algorithm the ability to make decisions. That’s right! Make decisions.

Thus, the use of conditional statements allows the algorithm to do one thing or another, based on what we know as a conditional expression.

declaracoes-condicionais

Just to illustrate, imagine the following scenario:

An algorithm has a variable called age, whose value is entered by the user. The algorithm needs to print two different messages depending on the value contained in that variable.

  • Thus, if the value contained in the age variable is less than 18, the algorithm should display the message “You cannot access the system! You are a minor!”.
  • On the other hand, if the value contained in the age variable is greater than or equal to 18, the algorithm should display the message “Welcome to the system!”.

But how can we do this verification? How could the algorithm ask, “Hey, variable! Is the value inside you less than 18?

You will learn from now on!


The IF statement

In fact, in the majority of programming languages, we use the if statement to create conditional statements.

Just to illustrate, see an example below the basic structure of an if statement:

1.if(conditional expression){
2. command executed if the conditional expression is true;
3.}
  • In line 1 (code above), the conditional expression is the question that the algorithm asks. This question returns two possible values: true or false.
  • In this sense, if the response of the conditional expression of line 1 is true, the command is executed on line 2.

The example of age

First, let’s see the use of the if if statement in the age variable example:

1.if(age < 18){
2. println("You cannot access the system! You are a minor!");
3.}
  • In line 1 (code above), the expression age < 18 asks if the content of the age variable is less than the value 18.
  • In line 2 (code above), the message print command is executed only if the conditional expression in line 1 returns true!

It is important to note that the conditional expression of line 1 made use of the < operator (less than).

The < operator is just one of several existing relational operators, which are, in fact, widely used in conditional statements.

In the next article we will study relational operators in detail.


The ELSE statement

The else statement complements the if statement. The else creates what we call an alternate flow, in order to define the action that will be taken if the conditional expression returns a false value.

Just to illustrate, let’s go back to the previous example of age:

1.if(age < 18){
2. println("You cannot access the system! You are a minor!");
3.}
  • Notice in line 2 that the if statement defines a message to be printed if the expression age < 18 in line 1 is true.
  • However, if the conditional expression returns false, nothing will happen. That’s where the else command comes in.

The code below shows how the else command works.

1.if(age < 18){
2. println("You cannot access the system! You are a minor!");
3.} else {
4. println("Welcome to the system!");
5.}
  • If the age < 18 on line 1 returns a false response, the command on line 4 (which is the contents of the else) will be executed.

You have certainly learned the basics of conditional statements. However, there is still much more to learn and practice.

So continue your evolution in programming and read our article on relational operators. Certainly, this knowledge will give you the power to write much more advanced conditional statements!

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Other articles