Subscribe to C++ FunSchool        RSS Feed
-----

[C++] Effective looping

Icon Leave Comment
Many of my friends ask me help in writing loops. Some also feel that looping is the worst thing in C++. So I have an easy method to counter that difficulty.

First: See how many things are changing.
Depending upon the circumstances, you can have one loop for all things, multiple loops or at the most, the number of loops will be equal to the number of things changing their values.

Second: Try to wind up things in the minimum number of loops possible.
It will not only save memory, but also help you control things effectively.

Third: Try to use the values of the counters somewhere.
If you can use the counters which you initialized for your loop, baam! use them. Many times, the examiner will expect you to do that! And this is where I learnt a lesson to use them.

for eg.
If I have to show table to 3 with alternating sign, then I can use the counter most effectively.

#include<math.h> //for pow function
#include<iostream.h>

int main()
{
  int n=3;
  for (int i=1; i<=10; i++)
  {
    cout << n*pow(-1,i) << "\t";
    n=n+3;
  }

  return 0;
}



Now this was one story. For nested loops, or we can say, loops inside loops, we have to take care of two things. One, the order of execution of main loop and the nested loops and second, whether the loops are dependent or not.

for eg.

loop1
{
loop2
{
loop3
{
:
:
:
}
}
}

Here, it may be possible that the counter of loop1 is being used in loop2 and the counter of loop2 is being used in loop3. Then the change in the counter of loop2 can affect the way loop3 executes.

Now we take a simple example. If we want to print stars in the following pattern:
(if the user input is 5 then)

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

What we do is, we write 2 loops. One loop will control the position of line and one will control what happens in that line. In 1st line, there is 1 star; in 2nd, there are 2 stars and so on.

The syntax is very simple:

#include<iostream.h>

int main()
{
   for(int i=1; i<=5; i++) //To control the line in which the control is
   {
      for(int j=1; j<=i; j++) //To control what happens in the line selected by the outer loop
      {
         cout << "*";
      }
   cout << endl; // This will take the control to the next line
   }

   return 0;
}



In the above loop, we write cout << endl; to go to the next line. For those who don't know the use of endl, endl is a keyword which means end line. It takes the control to the next line in the output window.

This is a simple way in which nested loops can be used.

Another important tip!
Do not forget to initialize properly. If you write, for(int i; i<=7; i++), it might lead into an infinite loop. This is because when we write int i, the system gives a garbage value to i and it can have any positive or negative integer value, generally a 4 digit or 5 digit number. So, this can make the condition true. This is fatal for slower computers. If you compare fast computers with slow computers, you will notice that the fast ones easily execute the loop upto 32767 if it is integer type (so it doesn't become an infinite loop!).

A common mistake which many of us do is that we write the for statement like this:
for (....;....;...);

This becomes a syntax error! What happens is that, as soon as the semicolon comes after the line in which the for statement is written, the compiler ends the body of for loop there itself. So it does nothing. It just initializes, checks for condition and does the updation again and again.

For eg.

#include <iostream.h>
int main()
{
   for (int i=1; i<=10; i++);
   {
      cout << "EXAMPLE LINE" << endl;
   }

   return 0;
}



We want to print "EXAMPLE LINE" 10 times on the screen. But due to the semicolon the cout statement will no longer be a part of the for loop, and it will be shown only once on the screen. Go and try it out for yourself.

So this is all for today's effective programming tutorial. If there are any questions or queries, feel free to Email Me.

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2014

S M T W T F S
 123456
78910111213
141516171819 20
21222324252627
28293031   

Tags

    Recent Entries

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories