In programming, we frequently have to repeat an action with little to no difference in the specifics each time it is carried out. This need is satisfied via a loop mechanism.
The computer’s adaptability rests in its capacity to repeatedly carry out the given set of instructions. This entails repeating some programme code, either a predetermined number of times or up until a specific condition is met. In order to efficiently do this repeating task, loop-controlled instructions are utilised. This prevents the repetitions from making the programme appear redundant.
The three categories of loops used in C programming are listed below.
- For loop
- while loop
- Do-while loop
Variety of Loops
Loops that control entry
- Prior to entering the loop body, the test condition is assessed in entrance controlled loops. Entry-controlled loops include the for and while statements.
Shut down controlled loops
- The test condition is evaluated at the conclusion of exit-controlled loops. The loop body will run at least once whether the test condition is true or false. An example of an exit-controlled loop is the do-while loop.
For Loop
- A repetition control structure known as a “for loop” enables us to quickly create a loop that will run a predetermined number of times. This is how the for loop functions:A variable is initialised to a value in the initialization statement, which is executed just once.The test expression is assessed in the next phase. Let’s assume that the test expression is found to be true. In that scenario, the for loop continues to run and re-evaluates the test expression; however, if the test expression is found to be false, the for loop ends.Until the test expression returns false, the loop continues to run. The loop ends when the test expression returns false.
While loop
- Pre-tested loops are also known as while loops. Depending on a test expression that is provided as a boolean condition, the while loop enables code to be executed numerous times. While introducing for loops, it was made clear that the number of iterations was known, whereas while loops are used when the precise number of loop iterations is unknown. Based on the test condition’s evaluation of true or false, the while loop’s execution is stopped.
Do-while loop
- Similar to how a while loop terminates execution based on the test condition, do-while loops also do this. While the other two loops are entry-controlled, the do-while loop tests the condition at the conclusion of the loop body, which is the fundamental distinction between it and the while loop. Regardless of the test circumstance, a do-while loop’s body will run at least once.It may occasionally be essential to exit a loop in the middle of it. We employ the break statement and the continue statement in this.
change statement
- Regardless of the type of loop we are in, if a break statement is reached, the loop is terminated, and the programme moves on to the statement that comes after the loop.
further statement
- When a continue statement appears in a loop, the control immediately moves to the test condition, which is where the loop begins, disregarding any code that comes after the continue statement.