Loops are very useful when you want to perform a task repeatedly. Loop’s body has set of statements, which gets executed on every iteration until a given condition is met.
We have three types of loops in C. The working of these loops are almost similar, however they are being used in different scenarios. You may need to choose the loop based on the requirement.
Below are the tutorial links on each type of loop (for, while, do-while) & loop control statements(break, continue, goto).
- for loop: This is most commonly used loop in C language. The syntax and flow of this loop is simple and easy to learn. However there are few cases when you may prefer any other loop, instead of this.
- while loop: This is used when you need to execute a block of statements repeatedly until a given condition is met. Read this tutorial to understand the flow of this loop.
- do-While loop: It is similar to the while loop, the only difference is that it evaluates the test condition after execution of the statements enclosed in the loop body.
- break statement: It is used with various loops (for, while and do-While) and switch case statements. When a break statement is encountered inside a loop, the control comes out of the loop. When it gets encountered in switch-case, the control comes out of the switch case and continue execution with the statement following switch-case body.
- continue statement: Continue statement is used inside loops. Whenever it is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop’s body for the current iteration.
- goto statement: When goto statement is encountered in a C program, the control jumps to the mentioned label. It is rarely used as it makes the program complex and confusing.