C++ part 5 – for loop

(UPDATED 11.9.2013)
In this post we are going to go through a statement called for loop. A for loop is a function in C++ (and other programming languages) that does something the number of times that the user specifies it to do. The for loop can go through values/table of values, do counting among other things you would expect a counter to do. In the below picture I have drawn a picture of how it works. Click on the picture for zooming in.

for

– In the red section we tell the for loop to initialize the variable counter with the value 0.
– In the green section we have a statement where we code the for loop to STOP incrementing the value counter if the value counter has a value that is smaller or equal to 10.
– In the blue section we have told the for loop to increment the variable counter for every loop that it takes.

Basically the formula for the above picture is right below that Dennis Ritchie wrote himself in his own book (I take the complete credit of the above masterpiece that I made with paint).

for (expression1; expression2; expression3)
{
statement
}

and basically what the above code means is that if the condition is true, then do the same cycle again and again until the condition is false and break away from the loop and continue the program like I have tried to demonstrate in the below picture.

pic1


Below is the real code to get the program running

#include <iostream>
using namespace std;
int main ()
{
    int counter;
    
    
    for(counter = 0; counter <= 10; counter++)
    {
    cout << counter << endl;            
    }
    system(“PAUSE”);
    return 0;
}

and here is the output for the above code

forloop2

———————————————————————————————————-

but wait, there is more! Below I have written the multiplication table for the number 2.

#include <iostream>
using namespace std;
int main ()
{
    int counter;
    
    
    for(counter = 0; counter <= 10; counter++)
    {
    cout << 2 * counter << endl;            
    }
    system(“PAUSE”);
    return 0;
}

and here is the picture

formulti

———————————————————————————————————-

Now that we have gone few keypoints of the functionality, I think that the next wise thing is to go through some problems that the people have with the loops for example in the below, I have written a code. What is your opinion that will happen in it? What will be the output?

#include <iostream>
using namespace std;
int main ()
{
for(int i = 0; i <= 10; i++)
{
int theoryNumber = 1;  
cout << “The counter is “<< i <<  endl;
theoryNumber = theoryNumber + 100 + 100;
cout << theoryNumber << endl;
}
s
ystem(“PAUSE”);
return 0;
}

Because for loop always starts over again, meaning that it goes cycles, it will do whatever you write inside it everytime it starts a new loop cycle. For example in the above code, everytime when the for loop ends and starts all over again, it will initialize the theoryNumber in every new loop. This means that the theoryNumber will not accumulate any previous arithmetics or values it was given or had as an input if the creation of the variable is inside the for loop. I will go shortly over this. Below is the output of the aboe code.

forloop2.1

The example material is bad but you probably get the point.

———————————————————————————————————-

In the below example I have an input example of the for loop with an input and if clauses.

#include <iostream>
using namespace std;
int main ()
{        
int sum = 0;    
int userInput = 0;    
// Always remember to initialize!    
// because you never know when your    
// system might crash or go looney        
  for(int i = 0; i <= 10; i++)    
  {        
  cout << “Enter a whole number: ” << endl;        
  cin >> userInput;        
  sum = sum +  userInput;    
   }    
cout << “The sum of the values you enter is: “<< sum << endl;
system(“PAUSE”);
return 0;
}

and below is the output

forloop3

———————————————————————————————————-

Below is an example of for loop with if statement

#include <iostream>
using namespace std;
int main()
{
    int a = 0;
   
    for(int i = 0; i < 10; i++)
    {
            if(i != 9){
                  cout << “the variable i still has to increment” << endl;
            }else if(i == 9){
                  cout << “The program will now stop the forloop” << endl;
            }
    }
   system(“PAUSE”);
   return 0;
   
}

the picture is below

forloop

———————————————————————————————————-

I will add more content today later and with great hesitation and doubt I now release this. Please note that this post does contain a lot of problems so check back later to see the whole post in its final form.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s