Looping is a fundamental programming idea that is commonly used in writing programs. A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. It offers a quick and easy way to do something repeatedly.
- for loop
- for-in loop
- while loop
- do-while loop
- i=1 : initialization
- i < =5 : condition
- i++ : increment
When you run the above program it will display number 1 to 5 in message box.
The do-while loop is executed at least once whereas the while loop may not execute at all. That means, it will execute the code block once, before checking if the condition is true , then it will repeat the loop as long as the condition is true.
When you run the above example, it will execute one time even though the condition is not met (i>5).
Difference between while loop and do while loop
A do-while loop guarantees the execution of the loop at least once because it checks the loop condition AFTER the loop iteration. Whereas a while loop will check the condition first before executing the content.
A loop that repeats indefinitely and never terminates is called an Infinite loop or endless loop.
You can use a while loop as infinite loop:
Executing the break statement exits from the current loop or statement , and begins script execution with the statement immediately following.