If you want to do the same things many time you can use a loop. In C, there are three different kind of loops. They are the for loop, while loop, and the do while loop. They come in handy in certain conditions. Right now we'll focus on the for loop.

The for loop is good for looping a definite amount of loops. That is you already know how many times to loop. More importantly for loop can count. You can start from any number, end in any number, and count in multiples for example, counting in odd or even numbers.

The syntax is:

for (initialization; condition; incrementation) statement;

or for multiple statements:

for (initialization; condition; incrementation)
{
statement 1
statement 2
}

#include<stdio.h>
#include<conio.h>

int i;

void main() {
clrscr();
printf("Prints my name three times.\n");
for (i=0; i<3; i++) printf("Anthony Perlas\n");
printf("\nCounts from 1 to ten.\n");
for (i=1; i<=10; i++) printf("%d ", i);
printf("\n\nCounts down from 10 to 0.\n");
for (i=10; i>=0; i--) printf("%d ", i);
printf("\n\nCounts the odd numbers from 1 to 21.\n");
for (i=1; i<=21; i+=2) printf("%d ", i);
printf("\n\nMultiple statements.\n");
for (i=0; i<3; i++)
{
printf("%d ", i);
printf("Anthony Perlas\n");
}
getch();
}
Prints my name three times.
Anthony Perlas
Anthony Perlas
Anthony Perlas

Counts from 1 to ten.
1 2 3 4 5 6 7 8 9 10

Counts down from 10 to 0.
10 9 8 7 6 5 4 3 2 1 0

Counts the odd numbers from 1 to 21.
1 3 5 7 9 11 13 15 17 19 21

Multiple statements.
0 Anthony Perlas
1 Anthony Perlas
2 Anthony Perlas

What's i++? It's a short cut. It's much easier to type i++ than i=i+1. It also avoids having to type the same variable twice. Here's a table of shortcuts in C.

The long Wayi=i+1i=i-1i=i+2i=i-2i=i*2i=i/2
The short cuti++i--i+=2i-=2i*=2i/=2

Basically you take out the repeating variable and move the operator to the front of the equal sign.

Nested For Loops:

You can put a loop inside a loop. This is called a nested loop. You can nest as many loop as you can but keep it to a minimum as it could get confusing especially when trying to debug your program.

Here's an example:

#include<stdio.h>
#include<conio.h>
#define max 5

void main() {
clrscr();
printf("Prints the value of i and j as it goes through the loop.\n");
for (int i=0; i<max; i++) for (int j=0; j<max; j++) printf("(%d, %d)\n",i,j);

getch();
}
Prints the value of i and j as it goes through the loop.
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 0)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(4, 0)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
    Now for your assignment:
  1. Create a program that uses a for loop that doubles the value of i starting from 1 to 1024. The output should be 1 2 4 8 16 32 64 ... 1024.
  2. Create a program that allows the user to input a number and find its factorial. Use float instead of int since you will be getting large number.
  3. Create a program that uses a nested for loop to simulate counting in 4-bit binary.
  4. See what I did for A, B or C.

    Click on A, B or C to see the code.

    Hover over A, B or C to see the output.

    Wasn't that easy and fun? Next we'll discuss the other loops.

    <<back<< >>Next>>