for Statement (Sample Program)

Let’s take an example to explain for loop. We want to write a program that prints the table of 2 on the screen.
In this program, we declare a variable counter of type int. We use this variable to multiply it by 2 with values 1 to 10. For writing the table of 2, we multiply 2 by 1, 2, 3 .. upto 10 respectively and each time display the result on screen. So we use for loop to perform the repeated multiplication.

Following is the code of the program that prints the table of 2.


This is a simple program. In the for statement, we initialize the variable counter to 1 as we want the multiplication of 2 starting from 1. In the condition clause, we set the condition counter <= 10 as we want to repeat the loop for 10 times. And in the incrementing clause, we increment the variable counter by 1. In the body of the for loop, we write a single statement with cout. This single statement involves different tasks. The portion ‘<< “2 x “’ displays the string “2 x “ on the screen. After this, the next part ‘<< counter’ will print the value of counter. The ‘<< “ = ”’ will display ‘ = ‘ and then the next part ‘<< 2 * counter’ will display the result of 2 multiply by counter and the last <<”\n” ( the new line character) will start a new line. Thus in the first iteration where the value of counter is 1, the cout statement will display the following line


2 x 1 = 2


After the execution of cout statement, the for statement will increment the counter variable by 1. Thus value of counter will be 2. Then condition will be checked which is still true. Thus the body of for loop (here the cout statement) will be executed again having the value of counter 2. So the following line will be printed.

2 x 2 = 4


The same action will be repeated 10 times with values of counter from 1 to 10. When the value of counter is 11, the condition ( counter <= 10 ) will become false and the loop will terminate. The output of the above program is as the following.






Now what will we do, if some one says us to write a table of 3, or 4 or 8 or any other number. Here comes the point of re-usability and that a program should be generic. We write a program in which a variable is used instead of a hard code number. We prompt the user to enter the number for which he wants a table. We store this number in the variable and then use it to write a table. So in our previous example, we now use a variable say number where we were using 2. We also can allow the user to enter the number of multipliers up to which he wants a table. For this, we use a variable maxMultiplier and execute the loop for maxMultiplier times by putting the condition counter <= maxMultiplier. Thus our program becomes generic which can display a table for any number and up to any multiplier. Thus, the code of our program will be as below:






The output of the program is shown as follows:


Here is a guideline for programming style. We should avoid using constant values in our calculations or in long routines. The disadvantage of this is that if we want to change that constant value later, then we have to change every occurrence of that value in the program. Thus we have to do a lot of work and there may be some places in code where we do not change that value. To avoid such situations, we can use a variable at the start and assign that constant value to it and then in the program use that variable. Thus, if we need to change the constant value, we can assign the new value to that variable and the remaining code will remain the same. So in our program where we wrote the table of 2, we can use a variable (say number) and assign it the value 2. And in cout statement we use this variable instead of constant 2. If we want that the program should display a table of 5, then we just change the value of the variable. So for good programming, use variables for constant values instead of explicit constant values.

0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger