Loops in C Programming
Loops are used to repeatedly execute a block of code as long as a certain
condition is true. C programming provides three types of loops: for, while,
and do-while.
For Loop
The for loop is used to iterate over a sequence of values. It has the
following syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Initialization: Initializes the loop variable(s)
Condition: Checks if the loop should continue
Increment/Decrement: Updates the loop variable(s) after each iteration
Example:
for (int i = 0; i < 5; i++) {
printf("i: %d\n", i);
}
While Loop
The while loop continuously executes a block of code as long as a
condition is true. It has the following syntax:
while (condition) {
// code to be executed
}
Example:
int i = 0;
Loops are used to repeatedly execute a block of code as long as a certain
condition is true. C programming provides three types of loops: for, while,
and do-while.
For Loop
The for loop is used to iterate over a sequence of values. It has the
following syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Initialization: Initializes the loop variable(s)
Condition: Checks if the loop should continue
Increment/Decrement: Updates the loop variable(s) after each iteration
Example:
for (int i = 0; i < 5; i++) {
printf("i: %d\n", i);
}
While Loop
The while loop continuously executes a block of code as long as a
condition is true. It has the following syntax:
while (condition) {
// code to be executed
}
Example:
int i = 0;