C Loop
Sometimes, we may need to execute the same piece of code multiple times. In general, statements are executed sequentially: The first statement in a function executes first, then the second statement, and so on.
The programming language provides a variety of control structures for more complex execution paths.
The loop statement allows us to execute a statement or group of statements multiple times, and the following is a flowchart for looping statements in most programming languages:
Loop type
The C language provides the following types of loops. Click on the link to see the details for each type.
Loop Type |
Description |
While loop |
A repeating statement or group of statements when the given condition is true. It tests the condition before executing the loop body. |
For loop |
Executes a sequence of statements multiple times, simplifying the code that manages the loop variable. |
Do...while Cycle |
Except that it tests the condition at the end of the loop body, the other is similar to the while statement. |
Nested loops |
You can have a while, for, or do ... One or more loops are used within the while loop. |
Loop Control statement
The loop control statement changes the order in which your code is executed. Through it you can implement the code jump.
C provides the following loop control statements. Click on the link to see the details of each statement.
Control Statements |
Description |
Break statement |
Terminates a loop or a switch statement, and the program continues to execute the next statement immediately following the loop or switch. |
Continue statement |
Tell a loop body to stop this iteration of the loop immediately, and start the next iteration of the loop again. |
Goto statement |
Transfers control to the marked statement. However, it is not recommended to use the GOTO statement in your program. |
Infinite loop
If the condition is never false, the loop becomes an infinite loop. For loops are used in the traditional sense to implement an infinite loop. Because none of the three expressions that make up a loop is required, you can leave some conditional expression blank to form an infinite loop.
#include <stdio.h>
int main ()
{for
(;;)
{
printf ("This loop would run forever.\n");
}
return 0;
}
When a conditional expression does not exist, it is assumed to be true. You can also set an initial value and an increment expression, but in general, C programmers tend to use a for (;;) structure to represent an infinite loop.
Note: you can press Ctrl + C to terminate an infinite loop.
The above is the C language cycle of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!