C Loops
C - Loops
Section titled “C - Loops”Loops are used to execute a block of code repeatedly as long as a certain condition remains true. This avoids writing the same code multiple times.
Programming languages provide control structures for iteration (looping). Without loops, executing a task 100 times would require writing the code 100 times.
The general structure of a loop involves:
- Initialization: Setting up loop control variables (optional, often done before the loop).
- Condition Check: Evaluating a condition before (or sometimes after) each iteration.
- Loop Body: The block of code to be executed repeatedly.
- Update: Modifying the loop control variable after each iteration (to eventually make the condition false).
If the condition never becomes false, the loop runs forever (an infinite loop).
C provides the following loop types:
| Loop Type | Description |
|---|---|
while loop | Repeats the loop body as long as the condition is true. The condition is tested before each iteration. |
for loop | A compact loop structure often used when the number of iterations is known or easily determined. Includes initialization, condition testing, and update expressions in its header. |
do...while loop | Similar to while, but the condition is tested after the loop body executes. Guarantees the loop body runs at least once. |
| Nested loops | Placing one loop inside the body of another loop. |
Loop Control Statements
Section titled “Loop Control Statements”These statements alter the normal flow of execution within loops:
| Control Statement | Description |
|---|---|
break statement | Immediately terminates the innermost loop (for, while, do...while) or switch statement containing it. Execution continues with the statement following the terminated loop/switch. |
continue statement | Skips the rest of the current loop iteration and proceeds to the next iteration’s condition check (and update in for loops). |
goto statement | Transfers control unconditionally to a labeled statement within the same function. Use of goto is strongly discouraged in modern programming as it makes code hard to read, debug, and maintain. Use break, continue, or function calls instead. |
The Infinite Loop
Section titled “The Infinite Loop”A loop becomes infinite if its condition always evaluates to true. The for loop provides a common idiom for this:
#include <stdio.h>
int main(void) { // An infinite loop using the for construct for (;;) { // No initialization, no condition, no update printf("This loop will run forever... (Press Ctrl+C to stop)\n"); // Usually, some condition inside the loop triggers a 'break' // Or the program performs a continuous task (like a server) }
// This part is never reached return 0;}An empty condition in a for loop is implicitly treated as true. While while(1) also creates an infinite loop, for (;;) is a conventional and often preferred way to signify an intentional infinite loop in C.
You typically terminate an infinite loop externally (e.g., pressing Ctrl+C in the terminal) or by using a break statement inside the loop based on some internal condition.