Conditional statements

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

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.

declarações-condicionais-conditional-statements-01

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 line1 is true, the command is executed on line 2.

The example of age

First, let’s see the use of the 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.

Exercise 1

To make sure that you understand the basic operation of the if statement, do the exercise proposed below:

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.

Exercise 2

Finally, to make sure you really understand how the else command works, do the exercise proposed below:

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!

David V. Santiago

David V. Santiago

MSc in Systems and Computing, Professor of Computer Programming, Algorithms and Data Structures.

Was this article helpful to you?

So support us and share it with others who are interested in this subject!

Share on whatsapp
WhatsApp
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Follow us
on our social media.

This website uses cookies to ensure you get the best experience on our website.