C For Loop
Used to iterate a specific number of times.
Syntax:
for (initialization; condition; update) {
// Code to be executed in each iteration
}
Syntax:
for (initialization; condition; update) {
// Code to be executed in each iteration
}
- Initialization - It initializes the variable for the loop to process.
- Condition - Checks the condition before iteration. If false it exits the loop.
- Update - It increments or decrements the variable used to initilize the loop.
Example
#include <stdio.h>
int main() {
// Print numbers from 1 to 5 using a for loop
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
See how the loop works
- int i = 1 initializes the variable i to 1.
- i <= 5 is the condition that checks if i <= 5.
- i++ increments the value of i after each iteration
Using Arrays
Example
#include <stdio.h>
int main() {
// Iterating through an array
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Learn more about C arrays
Quick Recap
Topics Covered
C For Loop
Practice With Examples in Compilers
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compile
Example 1
Example 1
Example 2
Example 3
Example 4
Example 5