13)

Program 12 - For loops + comments

Program 12 For loops + comments Source code - prog12.c
This program shows how comments in a program can be used to make the source code more readable. There are two types of comments in C, single line and multi-line, examples of both are shown in this program. The program also includes a for loop, which is similar to the while loop, but is a lot more controllable.

 

Program code- new parts shown in red

/* This is a multi-line
comment. The compiler will
ignore this, but it is
useful for putting notes
in the source code */

#include <stdio.h>
// This is a single line comment
void main(void)
{
int i;
for(i=0;i<10;i++) // Loop from 0 to 9
printf("%d\n",i);
}

 

Description of the program code
Although, looking at the amount of red, there looks to be a lot new in this program, there isn't a great deal. The first five lines of the program are just one comment placed into the source code. There are two types of comments you can place in C programs, a multi-line comment which can span more than one line, or a single line comment which is constrained to one line.

The top of the program is an example of a multi-line comment. They start with /* and terminate with */ , anything in between is ignored by the compiler so you can put whatever you like in there to make the program more readable.

A single line comment starts with // and is not terminated. Anything after the // on the same line is ignored by the compiler, so again it can be used to place meaningful comments into the source code. There are two single line comments in the program above, the 2nd is included to show that comments can be placed to the right hand side of actual code lines without affecting the program.

It is normal practice with comments to place a comment at the start of a program saying briefly what the program does. Purists say that every single line of code should have a comment explaining it !

Now down to the main new feature of this program the for loop. Firstly notice a variable called i has been defined as an integer, this variable is used in the for loop to hold the current index count. This is probably the reason why so many programmers use a variable called i for this type of thing. You are free though to define a variable of any name and type.

The line for(i=0;i<10;i++) looks complicated, but it isn't. It will make the next line or block execute a number of times. The format of the line consists of three parts, the first part i=0 is the starting value for the variable i. The second part i<10 is the condition that, when true continues repeating, and the third part i++ is the increment or decrement. What the line is really saying is "start the value of i at 0, execute the next code block while the value of i is less than 10, each time the code block is executed add 1 to i "

Below are some more examples of the for loop :

for(i=9;i>=0;i--)

This time the value of i will start at 9 and have 1 deducted from it each loop ( i-- ), it will continue looping while the value of i is greater than or equal to 0.

for(timesthree=0;timesthree<=60;timesthree+=3)

This line will go through the 3 times table, starting at 0 and with each loop adding 3 to the timesthree variable, until timesthree is greater than 60.

 

Summary
This program should have been a welcome rest before moving onto the first game next ! It covered the for loop as well as how to comment a program using both single and multi-line comments. No tasks this time either !

 

Tasks

12.1) Go through all the programs written so far and place comments in them saying what each line does, also to make them neater for future reference, you may want to indent them at the same time.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001