Menu
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY CYBERSECURITY DATA SCIENCE
     ❯   

C For Loop Examples


Real-Life Examples

To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:

Example

for (i = 0; i <= 100; i += 10) {
  printf("%d\n", i);
}
Try it Yourself »

In this example, we create a program that only print even numbers between 0 and 10 (inclusive):

Example

for (i = 0; i <= 10; i = i + 2) {
  printf("%d\n", i);
}
Try it Yourself »

Here we only print odd numbers:

Example

for (i = 1; i < 10; i = i + 2) {
  printf("%d\n", i);
}
Try it Yourself »

In this example we print the powers of 2 up to 512:

Example

for (i = 2; i <= 512; i *= 2) {
  printf("%d\n", i);
}
Try it Yourself »

And in this example, we create a program that prints the multiplication table for a specified number:

Example

int number = 2;
int i;

// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
  printf("%d x %d = %d\n", number, i, number * i);
}

return 0;
Try it Yourself »