for

The for statement in C allows the creation of "for-loops". A for loop is usually associated with "counting".

In a for loop, you generally have a variable that serves as a loop counter. The loop counter lets you keep track of how many times you have gone through the loop (ie what iteration of the loop you are on).

Problem:

Write a program that will allow the user to enter a positive whole number. The program will print that many star (*) characters on a line

What we need to learn

  • how to write a for loop

  • loop counters

For loops

A "for loop" is different than a while loop because it typically involves the use of a loop counter.

These loop counters are intiailized at the start of a loop, and are incremented each time we run through the loop. It is checked against some sort of condition that becomes false after some number of iterations. Thus the idea of a counting loop.

Solution:

The steps for this program are as follows:

  • prompt and read a number

  • print out that number of stars.

However, you do not know what number will be entered by the user. Thus, the number of stars is variable. What you do know is that you can print one star at a time. If you repeat that process number times, you will have accomplished the task.

With a for loop, you start by creating a loop counter variable. This variable's job is really just to remember how many times you have gone through a loop. Traditionally you name these loop counters i,j or k. However, if you wish to use full variable names, that is fine also. If you use single letters though, use i,j and k.

A for statment's basic syntax is:

for ( initialize loop counter here; check here; iterate here){
   //things you do each time between curly brackets.
}

Note that a for-loop is actually a quick way of writing a while loop that counts with a loop counter. You can actually rewrite any for loop as a while loop by doing the following:

initialize loop counter here;
while(check here){
  //things you do each time between curly brackets
  iterate here
}

Now, for our program:

int main(void){
    int numToPrint;      //variable we read from user
    int i;               //loop counter

    //prompt and read
    printf("please enter a how many stars to print: ");
    scanf("%d", &numToPrint);

    //this for loop uses i as its loop counter
    //it is initialized to 0 (i=0)
    //it runs as long as i < numToPrint
    //and we increment i by making it 1 higher each time
    for(i=0 ; i <numToPrint; i++){
        printf("*");
    }
    printf("\n");
}

Why i=0?

So, this for loop starts by counting from 0 which may seem odd. In C (and other C syntax based language) you will eventually come across the idea of an array (later chapter). Arrays are a collection of values of the same type and you access them using an index value. index of arrays start at 0. It is better to learn to count from 0 now.

The good news is that if you want to make any loop run n times, there is a relatively easy pattern to it. You simply write a for loop as follows:

for (i=0;i<n;i++){
...
}

you initialize loop counter to 0. Check that it is less than n, and increment by 1.

This loop always runs n times.

You can substitute n for any other expression that holds the total amount you want run the loop for.

Last updated