In programming, sometimes there is a need to perform some operation more than once or (say) n number of times. Loops come into use when we need to repeatedly execute a block of statements. Show
For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below: Manual Method (Iterative Method)Manually we have to write cout for the C++ statement 10 times. Let’s say you have to write it 20 times (it would surely take more time to write 20 statements) now imagine you have to write it 100 times, it would be really hectic to re-write the same statement again and again. So, here loops have their role. C++
Output Hello World Hello World Hello World Hello World Hello World Using LoopsIn Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below. In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.
There are mainly two types of loops:
for LoopA for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. Syntax: for (initialization expr; test expr; update expr) { // body of the loop // statements we want to execute } Example: for(int i = 0; i < n; i++) { // BODY } In for loop, a loop variable is used to control the loop. First, initialize this loop variable to some value, then check whether this variable is less than or greater than the counter value. If the statement is true, then the loop body is executed and the loop variable gets updated. Steps are repeated till the exit condition comes.
Flow Diagram of for loop:Example: C++
Output Hello World Hello World Hello World Hello World Hello World While LoopWhile studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us. while loops are used in situations where we do not know the exact number of iterations of the loop beforehand. The loop execution is terminated on the basis of the test conditions. We have already stated that a loop mainly consists of three statements – initialization expression, test expression, and update expression. The syntax of the three loops – For, while, and do while mainly differs in the placement of these three statements. Syntax: initialization expression; while (test_expression) { // statements update_expression; } Flow Diagram of while loop:Example: C++
Output Hello World Hello World Hello World Hello World Hello World do-while loopIn do-while loops also the loop execution is terminated on the basis of test conditions. The main difference between a do-while loop and the while loop is in the do-while loop the condition is tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two loops are entry-controlled loops. Note: In a do-while loop, the loop body will execute at least once irrespective of the test condition. Syntax: initialization expression; do { // statements update_expression; } while (test_expression);
Flow Diagram of the do-while loop:Example: C++
In the above program, the test condition (i<1) evaluates to false. But still, as the loop is an exit – controlled the loop body will execute once. What about an Infinite Loop?An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. An infinite loop occurs when a condition is always evaluated to be true. Usually, this is an error. Using For loop:C++
Output: This loop will run forever. This loop will run forever. ................... Using While loop:C++
Output: This loop will run forever. This loop will run forever. ................... Using the Do-While loop:C++
Output: This loop will run forever. This loop will run forever. ................... More Advanced Looping Techniques
Important Points
Related Articles:
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. Is a repetition of a certain set of instruction until a condition is met?In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.
Which control structure is used when a set of statements executed repeatedly?The iteration structure executes a sequence of statements repeatedly as long as a condition holds true.
What structure is used when a set of statements in repeated many times?Answer: A repetition structure is also known as a loop. While and do-while statements are condition-controlled loops because they usea true/false condition to control thenumber of times it repeats. A for loop is a count-controlled loop because it repeats a specificnumber of times.
What are repetition structures?Repetition structures are used to repeat statements or blocks of code. The decision whether to repeat the code is based on the evaluation of a logical expression. If the expression is true, the code is executed. If false, the code is not executed.
|