THE WHILE CYCLE

java-logo

Having examined conditional tests we will now go on to look at the flow control structures that belong to iterations. The image shows the general form of the while loop.

while

The while loop repeats a series of lines of code until the condition specified in parentheses is true. The condition is evaluated immediately, that is, before the cycle starts. This has an important implication because if the first time the Boolean expression is evaluated it is false, the entire loop is not executed. The post increment instruction x++ is the one that allows us to reach ten and thus exit the loop. We get a sequence of numbers from 0 to 9. We see the example.

Esempio ciclo while

THE DO/WHILE LOOP

It is a variation of the while loop. Its structure is as follows.

do while

This time the Boolean condition is placed at the end of the loop so at least once iteration is executed. Let’s look at an example.

Esempio ciclo do-while

THE CYCLE FOR

Let’s look at the general structure of a for loop.

for

Initialization is usually represented by assigning a value to a variable. This variable represents the control variable of the entire loop. The condition is a normal Boolean expression that often uses the control variable; the loop continues until this condition is true. Iteration is an expression that is executed as the last instruction of the for loop. It is normally represented by the increase or decrease in the control variable. Let’s look at an example.

Esempio ciclo for

THE STATEMENT BREAK

In this section we will discuss the third category of flow control by clarifying the unconditional jump. We restart from the switch instruction where we had encountered the first unconditional jump instruction, namely break.

The keyword break as we have seen causes immediate exit from the switch. In general, we can say that the statement break causes unconditional exit from a loop.

Break and while

The break causes an unconditional jump, and the instruction immediately following the while loop code block is executed. In some programming languages such as C there is a special instruction: goto. It is not recommended to use this instruction because it leads to writing inelegant and unreadable code.

Break and while

UNCONDITIONAL JUMP EXAMPLE NOT WIDELY USED IN JAVA

Label and break

You write a label followed by the colon and a block of code delimited by curly brackets. If within the even nested block we cause the keyword break to appear followed by the label with which we marked the block, it causes immediate exit from the marked block. Let’s look at an example where In one fell swoop we get out of two loops.

Istruzione Break

THE CONTINUE STATEMENT

We can consider continue as complementary to the break instruction, such an instruction within a loop moves the code flow to the head of the loop, so an iteration is skipped. Let’s look at an example where we print all the odd numbers from 0 to 10 first without using continue then with continue.

Esempio ciclo for

The output will be 1-3-5-7-9 that is, only the odd numbers. This time we skip all even numbers (remainder of the form 0) and return to the top of the loop with continue. If the number is odd it is printed and the result is the same. continue performs an unconditional jump as a break, only instead of exiting the loop it skips an iteration and returns to the head of the for loop.

Istruzione Continue

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

         java -jar –enable-preview CourseJava.jar

  • Or run the main found in the file CorsoJava.java.