DiGrande.it

Blind, Braille and Embossing Technologies

This site uses cookies to personalize content and ads, provide social media features and analyze links. By closing this banner or continuing to browse, you consent to their use.
Read the DiGrande.it Cookie Policy

The While cycle

The While cycle is useful for repeating the execution of a block of code for a variable number of times. A Boolean condition is used to determine whether to execute the code block, just like an If. The construct of the While cycle is as follows:

- While [Boolean Condition].

- ...

- EndWhile

In the While cycle mentioned above the code block is executed if the Boolean condition is True. After the code block is executed, the While cycle reassesses the condition and, if it is still True, reruns the block, and this repetition continues until the condition becomes False. When the condition is False, the execution flow continues with the instruction immediately following its EndWhile instruction.

For example:

- a = 0

- While a <= 10

- a = a + 1

- EndWhile

In the example, the cycle is executed until the variable "a" exceeds the value 10. The initial condition returns to True if "a" is less than or equal to 10.

It is the programmer's responsibility to ensure that the Boolean condition sooner or later becomes False. If this does not happen, the body of the cycle will tend to be executed an infinite number of times. To prevent this danger, a limit of approximately 250,000 iterations was imposed, after which the While cycle triggers an error. Since infinite cycles are one of the most common programming errors, it is always advisable to be very careful when building an While cycle.

For example:

- a = 0

- While a <= 10

- a = a - 1

- EndWhile

In the example, the condition will always be True, since the variable "a" being decreased will never exceed the value 10. After about 250,000 iterations the execution will end with an error.

We saw the While classic cycle, in which Boolean condition is expressed and performed at the beginning. The Boolean initial condition allows you to avoid executing the code lock if you don't need to do it. To execute the code block at least once, a construct While is available in which the Boolean condition is placed at the end.

- While

- ...

- EndWhile [Boolean Condition]

Unlike the start condition, if the Boolean condition is True at the end, the cycle ends. The EndWhile keyword is always used to mark the end of an While cycle. Then the end of the While (End of While) will occur if the condition is True.

For example:

- a = 0

- While

- a = a + 1

- EndWhile a > 10

In this case, the While cycle is blocked at least once. The cycle iteration ends when the variable 'a' is greater than 10.

The Boolean condition can be placed at the beginning of the While cycle, then the code block is executed if the condition is True. Or the condition can be placed at the end of the cycle, and in this case the cycle ends if the condition is True. A particular feature is that the condition can be placed simultaneously at the beginning and at the end, obtaining two conditional controls at the beginning and at the end of the cycle. This special situation makes sense if the two conditions control different variables.

For example:

- a = 0

- b = 0

- While a <= 10

- a = a + 1

- b = Random(10)

- EndWhile b >= 5

The above example illustrates the use of dual condition. The While block is executed when the variable "a" is less than or equal to 10, or it ends when the variable "b" is greater than or equal to 5. In variable "b" each iteration is assigned a random value that can range from 0 to 9.

The body of a While cycle can contain another cycle: in this case we speak of nested cycles.

For example:

- a = 0

- While a <= 10

- b = Random(10)

- While b < 5

- // Do something

- EndWhile

- a = a + 1

- EndWhile

Secondary cycle is only performed if variable 'b' is less than 5. In variable 'b', each iteration of the primary cycle is assigned a random value from 0 to 9.

Iteration of an While cycle can be interrupted using the Break instruction. When you meet the Break instruction the execution goes on to execute the instruction immediately after the body of the cycle.

For example:

- a = 0

- While a <= 10

- b = Random(10)

- If b = 5

- Break

- EndIf

- a = a + 1

- EndWhile

In the above example if variable "b" will be equal to 5 the cycle will end, otherwise it will continue with all iterations. The Break instruction should usually not be used, as it has more suitable tools for writing a cycle. However, if necessary, its use can be tolerated.

The "Continue" instruction has a similar effect to the Break on cycle execution. It is interrupted but, unlike Break, the execution returns to the beginning or the end (depending on where the Boolean condition is) and if necessary the body of the cycle is repeated.

For example:

- a = 0

- While a <= 10

- b = Random(10)

- If b = 5

- Continue

- EndIf

- a = a + 1

- EndWhile

In the previous example, if variable "b" is equal to 5, the cycle is executed again, therefore variable "a" is not increased. Instead, in the next example, the control condition is set to the end, so the Continue instruction skips execution at the end of the cycle.

- a = 0

- While

- b = Random(10)

- If b = 5

- Continue

- EndIf

- a = a + 1

- EndWhile a > 10

If the While cycle has a double control condition, the Continue instruction will skip execution always at the start condition. As with the Break instruction, Continue can be replaced by more powerful instructions that make the code more readable.