Loops : Some Info

     Hi, conditional operators, variables are the base of any program apart from it's functionality to communicate with system. 2nd important thing is control that is the flow of statements. Now simple programs like hello world can do fine with those but when we start to make such programs which need to take big decisions and directions, we encounter a problem that is re-using a piece of code or in simple words repetition of a piece of code.
     Loops are very helpful and can be considered basic of programming. Let's have a look at loop system how to use them and get some common error information. A loop is made up of 2 basic parts, one is it's body which contains the code that is going to be looped and 2nd is the condition around which we want to loop the code. both of course are very important. Without either one of them, loop is useless. Simplest loop is while loop. it describes basic functionality of loops:

 #include <iostream>  
 using namespace std;  
 void main(){  
  int iterations = 5;  
  while (iterations > 0){  
  cout << "iteration number " << iterations << endl;  
  iterations--;  
  }  
  system("Pause");  
 }  
     In above code line 5 has first part of loop which would be the condition and rest braces {} hold the piece of code which will ran again and again till the condition get's wrong and loop breaks. Other then while loop, there are 2 other loops in C++ that are

  • for loop
  • do while loop
The basic concept is still the same, condition + piece of code. But there is some difference between these. For loop is mostly used when we need to create a new condition with loop. That is if in above code i don't have a variable iterations, i can just create one specially for the loop using for loop.

 #include <iostream>  
 using namespace std;  
 void main(){  
  for (int iterations = 5; iterations > 0; iterations++){  
  cout << "iteration number " << iterations << endl;  
  }  
  system("Pause");  
 }  

    Here you can see that all code related to loop condition is written in just one line that is

  • a variable to keep track of loop iteration
  • changing the value of that variable in each iteration of loop
  • check the condition of loop.

     Hence we can say that for loop is a little cleaner type of simple loop which is easy
to make and understand.
     So far we have 2 important notes here:
  • in case of previous data containing a loop condition we can use while loop
  • other wise to create new condition or enhance a condition we can use for loop

     Now let's see 2 keywords related to loops and this chapter will be over.
continue;
break;

Continue:

     A keyword to tell a loop that end current iteration right now and go to next iteration.
That is it will skip rest of statements in loop's body and start next iteration. Like:

 #include <iostream>  
 using namespace std;  
 void main(){  
  int iteration = 10;  
  while (iteration > 0){  
  if (iteration == 5)  
   continue;  
  cout << iteration++ << endl;  
  }  
  system("Pause");  
 }  

     It can be useful to save some extra work and conditions which will instead stop the rest of
code to run.

Break:

     Another keyword to stop loop, to break it. that mean stop looping right now. hence when it runs
the loop ends, rest statements will not run.

 #include <iostream>  
 using namespace std;  
 void main(){  
  int iterations = 0;  
  while (true){ //true will make loop run and not stop by condition - making it an infinit loop  
  iterations++;  
  cout << iterations << endl;  
  if (iterations == 10)  
   break;  
  }  
  system("Pause");  
 }  

Nested loops:

     If a loop is part of another loop's body then it's called nested loop, just like
one loop make repetition easy, nested loops make complex operations on repetitions
possible and easy. There are various uses of nested loops. Main thing to notice is
that in this situation, one loop is depending on other loop. And using this dependency,
we can implement many math and other problems easily. Here is a very simple example of it:

 #include <iostream>  
 using namespace std;  
 void main(){  
  for (int i = 0; i < 10; i++)  
  {  
  for (int j = 0; j < 10; j++)  
  {  
   cout << i*j;  
  }  
  cout << endl;  
  }  
  system("Pause");  
 }  

     Let's finish topic by looking at some mistakes that can make program go unresponsive. here
are some points that you should keep in view while using loops:

  • always double check the conditional part of loop. it should be checked properly and the variable controlling loop should be incremented or decremented properly.
  • if you are using continue or break statements then make sure they are used such that in case of any wrong value they will eventually end loop.

     The main point is that loop should end properly.

For any queries please comment.
Thank you.

Comments

Popular posts from this blog

C++ Console : Basic Graphic Techniques

C++ Console : Change Font Size

C++ Console : Color