Control statements in Java

Control statements

Control statements are nothing more than a set of words that tell the interpreter things like what the next statement to execute is, how many times to evaluate a particular expression, and so on

Sentences of selection

They serve to group statements and statements into a compound statement or block, which from that moment can be considered syntactically equivalent to a simple

if-else

The expression if-else lets you choose whether to run the next or following sentences on the basis of a boolean value

It has the following syntax:

First evaluates the boolean expression and in the case that your result is equal to TRUE to run the block of sentences then if

In the case that your result is equal to FALSE to run the block of sentences then the else

else is optional and we may not use it, although if we want to have all the options covered, it is advisable to use it

Within a block of statements can include additional if-else, this practice is known as nesting and it is very useful to continue adding conditions to check the code

The example compared the integer values contained in variables a, b, and c, using nested if-else, showing the user the highest value per screen

else-if

The else-if expression is an improvement to the if-else expression, its operation is similar, however it allows to have a multiple selection without resorting to nesting, although within the block of sentences it will be possible to perform nesting

It has the following syntax:

else is optional and we may not use it, although if we want to have all the options covered, it is advisable to use it

The code in this example is very similar to the previous one used with if-else, they are syntactically equivalent, but it has managed to reduce the number of lines of code a bit

switch

Since multiple selection is a very common construction, it is more advisable to use the switch

But keep in mind that it only works with whole expressions, we can't use any Boolean expression as with if-else or else-if

It has the following syntax:

First evaluates the entire expression and checks whether it equals the value contained in the first case, in case its result is equal to TRUE to run the block of sentences then in that case

The execution will not end until find a break, therefore ending the execution of the switch expression

break it can be omitted and then it would check the next case

In the event that no case has value TRUE, then it would work in a similiar way to the else, executing the block of sentences then the default

The example has checked which day of the week is the integer value by displaying the name of the day and if it is Saturday or Sunday, showing that it is a non-school day and weekend

This example would only work with values from 1 to 5, because if we enter any other value, it would show us by screen the default, that is, that it is weekend

Sentences jump

Sometimes we need to exit unconditionally of any expression and to do this we will use the sentences jump

break

This expression provides the option to unconditionally exit a while, do-while, for, for-each, or switch expression

In case the expression is nested, what it will do is exit the innerdest expression

It has the following syntax:

Although syntactically correct, its use makes the code difficult to read, since it is adding an exit point not controlled by a Boolean expression

continue

This expression is similar to the break expression, however, it is less aggressive

Breaks the execution of the block of sentences but it re-evaluates the boolean expression

It cannot be used on a switch, as it would have no effect

In case the expression is nested, what it will do is exit the innerdest expression

It has the following syntax:

Sentences of iteration

Sometimes we need to run a block of sentences un determinado o indeterminado número de veces, a estas sentencias se las denomina loops

while

This expression repeats a block of statements while is satisfied a boolean expression

It has the following syntax:

Within the block of sentences we must make sure that you modify in any way the value of the boolean expression, because otherwise we can fall into a loop that never ends and is called infinite loop

This situation is not desirable, as our program will be hung and it is likely that the user would not know what to do in that situation, since the programmer has not bothered to control it

The example walks through numbers 1 through 100 and shows the user per screen

The counter started at 0, because it is an agreement that was used to initialize variables in C and is still used in Java

Just like using variable names i, k, or j for counters

do-while

This expression is similar to while, however the statement block is always executed at least once and the condition check is performed after that execution

It has the following syntax:

Within the block of sentences we must make sure that you modify in any way the value of the boolean expression, because otherwise we can fall into a infinite loop

Also supports expressions break and continue

The example walks through numbers 1 through 100 and shows the user per screen

It should be noted that the first time the Boolean expression is not evaluated, but rather because of that situation, it would be totally equivalent to the expression while

for

This expression is similar to while, however it allows you to initialize the variable that will handle the loop

It has the following syntax:

Within the variation of the variable we must make sure that you modify in any way the value of the boolean expression, because otherwise we can fall into a infinite loop

Also supports expressions break and continue although its use is discouraged, as it is considered that for this expression they break the normal flow of the program

The example walks through numbers 1 through 100 and shows the user per screen

It is totally equivalent to the while expression, but saves some lines of code by including its own initialization and in this case, an increase in the variable outside the expression block

for-each

This expression is similar to the for but is specialized in travel objects that support the Iterator class

It was introduced in Java version 5

It has the following syntax:

It is not necessary to make sure that it is changed in any way the value of the boolean expression as in a for, because when based on the Iterator class, the loop will end when there are no more elements of the object, the preventing it from falling into a infinite loop

Also supports expressions break and continue although its use is discouraged, as it is considered that for this expression they break the normal flow of the program

The example walks through an array containing numbers 1 through 5 and shows the user per screen

In this case, a simple array has been traversed, but more specialized objects could be traversed (as long as they support the Iterator class) such as an ArrayList, a Vector, a Map, etc