Loops

I am having problems understanding how to program different loops and nested loops. I have basic skills in programming(very ammature) so you must go though it step by step on how to write them.
It's difficult to give you anything here that will be of a lot of use, because C++ programming isn't a simple programming language. If you want to understand loops I would highly recommend reading the literature, and playing around with languages like python, which will work on very simply written code and you can begin to understand the logic behind loops without having to bother with the complexities of C++. If however, you just want an example of a simple program that loops through 10 values of j, and for each j loops through 10 values of i, you would do this in c++ (I haven't run this so I appologise if I've made a mistake):

1
2
3
4
5
6
7
8
9
#include <iostream> // tell the compiler to include the library that allows us to output text to the screen
int main() { // the function in which everything in C++ is executed
  for (int j = 0; j != 10; j++) { // a loop that iterates integer j from 0 until it is equal to 10
    for (int i = 0; i != 10; i++) { // a loop that iterates integer i from 0 until it is equal to 10
      cout << "j = " << j << ", i = "<< i <<endl; // output a description of what is going on to the screen
    }
  }
return 0; // return a value to indicate that we've succeeded
} // close the main function 
Last edited on
Here's a good place to start:

1
2
3
4
for (int i=0; i<5; i++)
{
     cout << i << endl;
}


This loop will execute 5 times. Trying changing the 5 to a 10, and you see that the loop will execute 10 times.

This is a nested loop:


1
2
3
4
5
6
7
for (int i=0; i<5; i++)
{
     for (int j=0; j<5; j++)
     {
          cout << i << endl;
     }
}


This outside loop will execute 5 times, and the inside loop will execute 5 times every time the outside loop executes. So the cout is executed a total of 25 times! Tinker around with these until you understand what is happening.
Last edited on
RPGillespie doesnt i>5; mean that it has to loop less then 5 times? so 4?

It will loop 5 times because he began counting at i = 0, the < refers to the condition at which it will stop, not the number of times it runs through. So the loop will continue while i is less than 5, so it will iterate through i = 0,1,2,3,4 (5 times)
Last edited on
1
2
3
4
for(counting vairable; condition; incrementor){
//statements to execute go here after each increment until the condition is true

}

So i=0 means that the count starts at 0

while the variable 'i' is less than 5 - This is the condition to be true or false and when true breaks the loop

increment the variable 'i' by 1 while the condition is still false

then execute the statements in the curly brackets


To sum it up, as long as the variable 'i' is less than 5, execute. We increment so we know when to stop, in this case at 5 or while 'i' is less than 5 and when 'i' reaches 5 the loop will break.


To explain the nested you have to understand the above and apply it.

1
2
3
4
5
6
7
for (int i=0; i<5; i++)
{
     for (int j=0; j<5; j++)
     {
          cout << i << endl;
     }
}


The outside will execute first and will execute what is in the brackets while 'i' is less than 5.

Lets look at whats in the brackets.
1
2
3
4
for (int j=0; j<5; j++)
     {
          cout << i << endl;
      }


The first loop executes this. So looking at it the same way we can say that while 'j' is less than 5 the statement here will execute, which in this case just prints an 'i'.

Since neither statements are true yet it begins at the outside again, the first loop we looked at, and continues.
Last edited on
Topic archived. No new replies allowed.