Loops syntax

Syntax for Loop

The for loop is a structure used to execute one or more statements repeatedly as long as a condition is true. The for loop is particularly suitable if the required loop passes are already known in advance.

The loop usually uses a so-called counter variable. It usually contains a numeric value that controls the number of loop passes to be repeated by its increment or decrement.

for (initialization; condition; increment/decrement) {
	// code-block to be executed
}

Syntax while Loop

The while loop is also used to execute one or more statements until a condition is no longer met. Unlike the for loop, the while loop does not use a counter variable. This would have to be explicitly incremented or decremented in the body of the loop.
If the condition does not return true on the first query, the while loop is not executed at all.

while (condition) {
	// code-block to be executed
}

Syntax do-while Loop

The do-while loop basically does the same thing as the normal while loop. The only difference is that the do-while loop is executed once in any case, even if the first query returns false.

do {
	// code-block to be executed
}
while (condition)