Logo of Computer Classes

Learn Beginner Programming Principles

Double alternatives (IF …… .ELSE….) What happens if the condition is not met? Well, if we don't tell it anything, the program will follow the next order sequentially.

But we can also specify what would happen if the condition is not met.

It is the famous trio "if ... then ... but this other".

Let's see how the structure would be in all languages:

if (condition) {this is done} else {if the condition is not met, this is done};

In the example above it would be much better to do it with this type:

In pseudocode

numerical: number, root

end variable declaration

beginning

Write 'enter a number'

Read number

HomeYES

IF number> = 0 THEN:

root = square_root (number);

Write 'the square root is:' + root;

OTHERWISE Write 'sorry, I can't calculate the square root of a negative number'

End yes

finish

Remember that if the number is less than 0 it would be negative and there are no roots of negative numbers.

If you have noticed, we can write Write "a text" + variable (text and then the value of the variable will appear at that time).

When we write our program, instead of pseudocode, we must put the condition as follows:

if number> = 0 {root = square_root (number); Write 'the square root is:' + root;} else {Write 'sorry, I can't calculate the square root of a negative number "}

Note that we can write all the orders we want inside the brackets always separated by;.

When you come across a real program, the commands within a bracket will see that they are usually put in this way:

if number> = 0 {

root = square_root (number);

Write 'the square root is:' + root;

} else {

Write 'sorry, I can't calculate the square root of a negative number "

}

It is the same as before, but when we have to debug (review) the program visually it will be easier for us.

Multiple alternatives or with multiple conditions

It is very likely that we will have the need to include in our programs alternatives with many possible options.

variableOptions = a value to choose, for example from the keyboard or from a window that the user selects;

if (variableOptions = 0) {whatever corresponds};

if (variableOptions = 1) {whatever corresponds};

if (variableOptions = 2) {whatever corresponds};

We can put as many ifs as we want.

There is also the possibility that two conditions must be met at the same time:

if (condition1 && condition2) {This is true}

Also with else:

if (condition1 && condition2) {This is true} else {this is true}

The symbols && mean "and", that is, if condition1 and condition2 are met at the same time (both).

Another case would be if either of the two conditions is met:

if (condition1 | condition2) {This is true}

As you can see is the symbol | (vertical straight bar on keyboard = AltGr + 1)

Try doing the following exercises:

About sequential structures

1. Write an algorithm or pseudocode that calculates the area of ​​a triangle or a rectangle.

2. Write an algorithm or pseudo-code that calculates the price of an item after applying 16% VAT.

On selective structures

3. Design an options menu outline, for example, a menu to select a book to read from among 3 available.

4. Write an algorithm that reads three numbers and prints the largest of them on the screen.

Date update on 2021-03-31. Date published on 2021-03-31. Category: Computer class Author: Oscar olg Fuente: areatecnologia