Loops

There are so many different methods of looping which is an essential building block of any game or complicated program. Knowing efficient looping skills will make you a better C++ programmer.

For Loop

The most common form of looping is using the for loop.

#include <iostream>

using namespace std;

int main(){
  for(int i = 0; i < 5; i++){
    cout << i << endl;
  }
  return 0;
}

This will output: 1 2 3 4.
In each line of course because of endl.

A for loop is structured like so:
for(initialization variable; condition to loop; update variable){
execute loop code
}

If you leave the inside empty, it becomes an infinite loop:

for(;;){
  // infinite loop
}

Some other for loop examples may be used with arrays and displaying a sequence of characters (a string):

#include <iostream>

using namespace std;

int main(){
  char string[] = "Just a string";
  int maximum = (int)sizeof(string)/sizeof(char); // sizoeof(char) is 1
  for(int i = 0; i < maximum; i++){
    cout << string[i] << " ";
  }
  return 0;
}

It's good to keep functions outside the loop code, which is why I created the maximum integer so that the sizeof functions are only executed once. We don't want to keep doing sizeof ten or twenty times.

This will display the string as:
J u s t a s t r i n g

While loop

A while loop is also very common, usually used for true or false conditions.

#include <iostream>

using namespace std;

int main(){
  bool btest = true;
  int i = 0;
  while(btest){
    i += 5;
    if(i == 50){
      btest = false; // stop looping at 50
    }
  }
  return 0;
}

Useful for infinite loops where only certain exit inputs can get out of the loop.

Do While Loop

Do While loops are loops that are executed at least once before testing the condition. Useful in situations where the first execution might determine whether we can keep looping.

#include <iostream>

using namespace std;

int main(){
  int memory(0);
  do {
    memory = CheckMemory();
    Sleep(100);
  } while(!memory);
  return 0;
}

It's very useful in situations where you are waiting around until an event happens.

Foreach

There is no foreach loop in C++ or C; unless you install Qt library which has a foreach method.

Break

The break command allows you to exit a loop that you don't need to repeat depending on a condition.

#include <iostream>

using namespace std;

int main(){
  int memory(0);
  do {
    memory = CheckMemory();
    if(memory > 0)break;
    Sleep(10000);
  } while(memory < 1);
  return 0;
}

In the above example, you may not want to go through the whole 10,000 second sleep again before exiting the loop because memory is now greater than 0.

So you break out of the loop.

Continue

Continue is a skipping of the loop so that it can go to the next iteration.

#include <iostream>

using namespace std;

int main(){
  int memory(0);
  for(int i = 0; i < 10; i++){
    memory = Check();
    if(memory < 200 && memory > 100 && i == 2){
      continue;
    }
    GetMoreMemory();
  }
  return 0;
}

You may want to skip this iteration if we already reached 100 memory.
Thus, depending on certain conditions you may want to go to the next loop step rather than continue in the current one.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.