Loop in programming C
There is a situation where you need to make a code a few times. In general, statements are executed sequentially. The first statement of the function is executed first, then the second sentence and so on.
Similar to other programming languages, C also provides us with many control structures and allows you to perform complex parts.
The loop allows to execute a command and a group of commands multiple times, below is the general form:
C supports the following control commands.
Loop type DescriptionWhile loop
Repeat one or more groups of commands while the given condition is true. It checks the condition before executing the loop body.For loop
Execute a series of commands multiple times and summarize the code that manages loop variables.Do . while loop
Same as the While command, except that it checks the condition at the end of the loop body.Cage the loop
You can use one or more loops in other while, for or do.while loops.Loop control commands
Loop control commands change the execution of commands from its usual range. When the execution of the command leaves a range, all automatic objects that are created within that scope are destroyed.
C supports the following loop control commands.
Control commandDescriptionBreak command
End the loop or switch command and switch to executing the loop or switch command immediately after it.Continue statement
When this command is encountered, the program will ignore the statements below it (in the same loop statement) to execute the new loop.Goto command
Go to the assigned command. However, it is advised not to use the goto command in your program.Infinite loop
A loop is an infinite loop when a condition is never false. A for loop is often used for this purpose. When you leave three conditional expressions in a for loop, you will create an infinite loop.
#include int main () { for ( ; ; ) { printf ( "Vong lap nay se chay mai mai.n" ); } return 0 ; }
When the conditional expression is absent, it is assumed to be true.
Note : You can stop (end) an infinite loop by pressing Ctrl + C.
According to Tutorialspoint
Previous lesson: Flow control in C programming
Next lesson: Function in programming C