Loops are used in almost every program. It is one of the basic structures used in all programming languages. They allow you to perform a specific action cyclically in case you need to display, for example, 10 times something almost the same. We will get to know the loops here, which allow you to repeat an action over and over again – know number of times and unknow number of times.

While loop:

The while loop is mostly used in places where the assumed number of repetitions is undefined, but we know the condition that must be met. Its schematic form is presented below:

When the condition is met, the instruction inside the loop is executed. If, however, the condition is false, it may not take place, not once. Usually, a numeric variable is used, and then, thanks to increment or decrement, it changes until the condition becomes false. Similarly to the if conditional statement, you can omit the curly brackets when there is only one method in the body of the loop, or any statement. It is good to remember it as: “until the condition is met, follow the instructions”. Let’s see its operation on a practical example:

We declared a variable counter and we gave it a 0. We check the condition if our variable is less than than 10 – in fact, we are doing instructions inside of it. We then display the text “This is my loop” and increase the counter by 1. Then again the condition is checked whether 1 < 10, again yes, so we repeat the previous steps. When the counter reaches the value 10 the condition is false, because 10 is not less than 10. The loop is interrupted and the text “End of my loop” is displayed.

Loop: do … while

It differs from the while loop, mainly because what is inside of it takes place at least once, because the condition is checked later on. The schematic construction is presented as follows:

Note the semicolon that is ending the “expression”, this does not occur with the while loop. The instructions are executed at least once, and only then we check the condition. When it is true, the instructions are repeated.
This loop can be memorized as follows: “follow instructions until the condition is true”. Here is an example:

The course of action is almost the same, only for example, the counter was initiated with the number 10, in spite of everything the wording “This is my loop” would appear. In our case, the inscription is displayed 5 times.

For loop

In for loops, we usually know exactly how many times we want to repeat something. Its scheme is:

The initial expression is used to initiate a counter, which is usually declared in the same place as the integer variable – most often it is marked with the letters “i” and above, it is useful with nested loops, where it would be difficult to catch up with long names. The condition checks whether the instruction should be executed from within the loop, whereas the modifier changes the counter – usually it is an increment statement. An earlier example using a for loop would look like this:

As you can see, we did not have to declare a global variable here, and the record is more concise. The operation is exactly the same as in the case of while loops.

LEAVE A REPLY

Please enter your comment!
Please enter your name here