Loop Controls
In the C programming language, there are three types of loops:
for
loop:
This loop is used to execute a block of code repeatedly for a specified number of times. It has three components: an initialization, a condition, and an increment/decrement. The loop continues to execute as long as the condition is true.
for (initialization; condition; increment/decrement) {
// block of code to be executed
}
Here’s how the for
loop works:
- The
initialization
the statement is executed once, at the beginning of the loop. This statement typically initializes a loop control variable. - The
condition
is evaluated at the beginning of each iteration of the loop. If the condition is true, the loop continues to execute. If the condition is false, the loop terminates. - The block of code inside the loop is executed as long as the condition is true.
- The
increment/decrement
the statement is executed at the end of each iteration of the loop. This statement typically modifies the loop control variable. - Control then returns to step 2, and the loop continues to execute until the condition is false.
Here’s an example of a for
loop that prints the numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
In this example, the loop control variable i
is initialized to 1, and the loop continues to execute as long as i
is less than or equal to 5. After each iteration of the loop, i is incremented by 1. The block of code inside the loop simply prints the value of i in the console.
while
loop:
This loop is used to execute a block of code repeatedly as long as a certain condition is true. The condition is checked at the beginning of each iteration, and the loop continues to execute as long as the condition is true. It has the following syntax:
while (condition) {
// block of code to be executed
}
Here’s how the while
loop works:
- The
condition
is evaluated at the beginning of each iteration of the loop. If the condition is true, the block of code inside the loop is executed. If the condition is false, the loop terminates. - The block of code inside the loop is executed as long as the condition is true.
- Control then returns to step 1, and the loop continues to execute until the condition is false.
Here’s an example of a while
loop that prints the numbers from 1 to 5:
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
In this example, the loop continues to execute as long as i
is less than or equal to 5. After each iteration of the loop, i
is incremented by 1. The block of code inside the loop simply prints the value of i
to the console.
do-while
loop:
This loop is similar to the while
loop, but the condition is checked at the end of each iteration instead of at the beginning. This means that the loop will always execute at least once, even if the condition is initially false. It has the following syntax:
do {
// block of code to be executed
} while (condition);
Here’s how the do-while
loop works:
- The block of code inside the loop is executed once, regardless of whether the condition is true or false.
- The
condition
is evaluated at the end of each iteration of the loop. If the condition is true, the loop continues to execute. If the condition is false, the loop terminates. - The block of code inside the loop is executed as long as the condition is true.
- Control then returns to step 2, and the loop continues to execute until the condition is false.
Here’s an example of a do-while
loop that prints the numbers from 1 to 5:
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
In this example, the block of code inside the loop is executed once, regardless of the value of i
. After the first iteration of the loop, i
is incremented by 1, and the condition is checked. The loop continues to execute as long as i
is less than or equal to 5. The block of code inside the loop simply prints the value of i
in the console.
Loop Control Statements
There are three loop control statements that can be used to modify the behavior of loops. These statements are:
break
: This statement is used to immediately terminate a loop. When abreak
the statement is encountered within a loop, the program jumps out of the loop and continues with the next statement following the loop.continue
: This statement is used to skip the remaining statements in the current iteration of a loop and start the next iteration. When acontinue
statement is encountered within a loop, the program skips any remaining statements in the loop body and immediately jumps to the next iteration of the loop.goto
: This statement is used to transfer control to a labeled statement within the same function. When agoto
statement is encountered, the program jumps to the labeled statement and continues execution from there.
It’s worth noting that the use of goto
is generally discouraged in modern programming practices, as it can make code difficult to read and understand. In most cases, break
and continue
statements are sufficient for controlling the behavior of loops in C.