What is a Loop?
A loop is defined as a block of statements, which are repeatedly executed for a certain number of times.
The loops are two types.
Counter-controlled repetition: This is also called the definite repetition action, because the number of iterations to be performed is defined in advance in the program itself. The steps for performing counter-controlled repetitions are as follows.
Steps in Loop
Loop variable: IT is a variable used in the loop
Initialization: It is the first step in which starting and final values are assigned to the loop variable. Each time the upload value is checked by the loop itself.
Incrimination/discrimination: It is the numerical value added or subtracted to the variable in each round of the loop. The upload value is compared with the final value and if it is found less than the final value the steps in the loop are executed.
The above steps are implemented in numerous programs in this chapter.
Sentinel- controlled repetition: This is also called the indefinite repetition. One cannot estimate how many iterations are to be performed. In this type, loop termination happens on the basis of certain conditions using the decision-making statement.
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. If it hasn't, the next instruction in the sequence is an instruction to return to the first instruction in the sequence and repeat the sequence. If the condition has been reached, the next instruction "falls through" to the next sequential instruction or branches outside the loop. A loop is a fundamental programming idea that is commonly used in writing programs.
infinite loop (endless loop)
An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. 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. If the presence of the specified condition cannot be ascertained, the next instruction in the sequence tells the program to return to the first instruction and repeat the sequence, which typically goes on until the program terminates automatically after a certain duration of time, or the operating system terminates the program with an error.
Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written.
Loop Type & Description
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
It is more like a while statement, except that it tests the condition at the end of the loop body.
You can use one or more loops inside any other while, for, or do..while loop.