What is Loop in C
Programming loops repeatedly run a code until a predetermined condition is satisfied. Programmers can run an instruction or set of instructions repeatedly, avoiding duplicating code by using a loop function.
In C programming, there are primarily two kinds of loops:
- Entry-controlled loops: BeforeBefore starting the loop's main body, the test circumstance is examined. Entry-controlled loops are those seen in the While and For loops.
- Exit-Controlled Loops: After the body's conclusion, the test circumstance is assessed in an exit-controlled loop. Regardless of whether the circumstance is true, the loop's body will run no fewer than once. An exit-controlled loop is a do-while loop.
1. For loop:
Initializes, checks for conditions, performs the body, and completes the modification.
The C computer language's for loop is a structure for managing repetition that enables developers to create loops that will run a particular amount of times. Software developers can execute n numbers of steps sequentially on only one line using a for loop.
A loop parameter is utilized to manage the loop when looping for a loop. The loop's loop element is initialized with an amount, and then its test conditions are verified. Should the assertion hold true, the command will shift to the physique, where the for loop's main function will be performed. Repeating procedures will be taken until the criteria for exit are met. It will terminate if its test circumstance is incorrect.
The initialization process Expression: We set a loop clock or loop variables to an integer in this phrase. For instance, int i=1;
Test Expression: Test requirements are carried out in this phrase. The body of the loops will be executed, and this loop parameter will be modified if the given circumstance passes to true.
Example Code:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 15; i++)
{
printf( "Apple is red in colour\n");
}
return 0;
}
Output:
2. While loop:
Sets up, conditions are checked, the body is executed, and updates may occur within the body.
The amount of repetitions has no bearing on the whilst loop. Whereas the while loop's performance is stopped based on the test circumstance, the for loop's total number of repetitions was known beforehand. The while loop will end if the condition being tested turns out to be false; otherwise, the remainder of the code will be performed.
Example:
#include <stdio.h>
int main() {
// Example of a while loop
printf("\n While loop in C:\n");
int j = 1;
while (j <= 5) {
printf("ID NO: %d\n", j);
++j;
}
return 0;
}
Output:
3. Do-while loop:
This code runs the main portion and then does the required verification.
The do-while loop testing circumstance, evaluated at the body's conclusion, is the only distinction between a do-while loop and a while loop. The body within the iteration will run at least once in the do-while loop, regardless of the test's result.
The while loop, or do-while loop, test circumstance, evaluated at the body's conclusion, is the sole difference between the do-while loop and a while loop. The main portion of the cycle will run at least twice in the do-while loop, regardless of the test's circumstance.
Example Code:
#include <stdio.h>
int main()
{
int i = 2;
do
{
printf( "Taj Mahal is in Agra\n");
i++;
} while (i < 1);
return 0;
}
Output:
Statements on Loop Control
In programming C, loop commands are employed to alter the typical performance sequence.
In C programming, loop control statements alter the order in which operations occur.
- Break statement:
To end both switch and loop statements, use the break statement. The statement that comes just after the looping statement or switch receives its execution transition.
- Continue statement :
The continuation clause resets the situation immediately and repeats the previous statement without reading the rest of its body.
- Goto statement:
Control is sent to the designated expression via the goto expression.
Infinite Loop
When the main body inside the loop is continually run, and the test's statement never turns false, this is known as an infinite loop. When the circumstance is always true, an application is caught in a never-ending cycle. Generally, looping process control declarations can be used to fix this kind of issue.
Example Code:
#include <stdio.h>
int main ()
{
int i;
for ( ; ; )
{
printf("This loop is never ending.\n");
}
return 0;
}
Output: