Wednesday, July 28, 2010

Over and Over Again

We started our tutorial with the while loop. It allows one to perform an action over and over again until a condition becomes true. Another construct that is similar in result but different in syntax is the for loop.
A lot of times when a loop is used, we are interested in having a initialization, a conditional check and an action that changes the value of that variable. The syntax of the for loop contains precisely these three parts.
for (; ; )
Suppose you were helping a younger friend to learn tables and you wanted to print out the table of any number given to your program as input. Here is how the program would look.
#include”stdio.h”
int main()
{
int i, num;
printf(“Enter a number to see its tables\n”);
scanf(“%d”, &num);
for (i=1; i<11; i++)
{
printf(“%d\n”, i*num);
}
}
Done. As simple as that. The initialization of the variable, the condition check and its increment is all done in one statement. The first time the execution reaches the printf function, the i=1 has been executed and the condition check i<11 has been performed. The increment has not yet been visited. After the printf is executed, the execution pointer returns to the for construct, increments the variable and then checks for the condition. Though most of the times one will use the for loop in a very mechanical manner without giving much thought to the sequence of things happening, one must never forget the path taken by the execution pointer. I will show a cool program that will demonstrate the validity of my previous statement and establish it without doubt.
#include “stdio.h”
int main()
{
int i=0;
for (printf("Initialization\n"); printf("Condition check\n"); printf("Action on variable\n"))
{
printf("Inside loop\n");
i++;
if (i>2)
{break;}
}
}
Output:
Initialization
Condition check
Inside loop
Action on variable
Condition check
Inside loop
Action on variable
Condition check
Inside loop

Hope this program has helped you understand the sequence of steps undertaken by the execution of the for loop. One must understand that the loop body is visited at least once because the condition is evaluated to true. This indicates that the printf function must return something. Yes, it does. It returns the number of characters output to the screen. Instead of “Condition check\n”, if I had written a blank string “” then printf would have returned 0 and the condition check would have failed. The body would have not been visited even once.
We have introduced a new keyword break. Execution of break causes the execution pointer to abruptly pre-empt the loop and come out of it irrespective of what the condition check says. break works with for, while and switch…case but not with if. Also, it only breaks the encompasing loop not any loop beyond that. The execution pointer will be at the closing } of the for loop as a result of the break statement being executed.
Okay, now that you have started feeling that for loops are simple, let us complicate things a bit more. Suppose you want the following output:

    *
   ***
  *****
 *******
*********

How can this be accomplished? By using double for loops. Here is the program:
#include”stdio.h”
int main()
{
 int i,j;
 for (i=0;i<5;i++)
 {
  for (j=5;j>i;j--)
  {
   printf(" ");
  }
  for (j=0;j<2*i+1;j++)
  {
   printf("*");
  }
  printf("\n");
 }
 scanf("%d",&i);
}
Yes, it is a bit complicated, but try to understand the working yourself. Look at the stars carefully and observe the repetition structure. First of all, there are repeated spaces. Then there are repeated stars. Finally this all is repeated in every row, but the number of spaces decreases and number of stars increases. If you find this program a bit too complex, let me give a simpler assignment:
*
***
*****
*******
*********
This program is relatively simpler. It only has two repetitions.
for (i=0;i < 5;i++)
{
 for (j=0;j<2*i+1;j++)
 {
  printf("*");
 }
 printf("\n");
}
This is how you can have a for loop inside another. It has allowed us to grow the number of stars on each consecutive line (next line is reached because of the printf(“\n”) statement). A loop inside another gives a 2d representation. If we increase it to one more loop inside, we will have a 3d structure. Of course it is very difficult to show such a structure on the screen. Beyond that I have not seen many programs.

No comments: