C++ Tutorial #10 — Introduction to while / for loops


Topics Discussed: For / While loops, loop structure, using counter controlled loops.

Source code available here.


Loops
In this video I discuss two or the most important constructs in the entire language of java. In the next lesson I talk a bit more about for loops, which seem to be used a little more than while and do_while which we discussed here.


While loops
While loops are statements that will be repeated until either a break statement is reached, or until the condition is no longer true. Here’s a few examples of loops using while:


//syntax while(condition){ //do stuff }
//Examples below

int i=0;
while(i < 9){
     //do stuff
     i++; //increment i-- if you remove this statement, the loop will run infinitely.
}

boolean isTrue=true;
i=0; //reset 0 before the loop

while(isTrue){//while the boolean is true
     if(i > 10){
          isTrue = false;
}//end if
     i++; // increment i
}//End loop

i=0;//reset 0 before the loop
while (i < 10){
     cout << i; //output the value of i each time through the loop
     i++; // increment i each time through the loop
}

In the examples above you’ll notice that we always have a ‘way out’ of the loop, that’s because there is a common error known as an ‘infinite loop’ in which your code executes endlessly because your loop is unable to exit.


Do_while loops
Do_while loops are the same basic idea as while loops, except that they execute the contents of the loop once before checking the conditional. These are very useful in making users enter certain values for their input, as seen below:


int aValue=0;
do{ // do these instructions
cout << "Enter a value 1 or greater for the variable: ";
cin >> aValue;
}while(aValue < 1);//until this condition is no longer true.


For loops
For loops are similar in idea to a while statement, except that it contains a few other parts that need to be talked about after I give an example of the syntax and use of for loops:


for(int i=0; i<10; i++){
cout << i;
}

The above code does 4 things, one of which is apparent, the println() statement, so we won’t talk about that. But the for statement can be a little confusing, but when we break it into parts it will make a lot more sense.

Parts of a for loop
Every loop has three parts, which are as follows:

  1. The initializer: This part of the loop is where we set or declare some variable for use. Typically you’ll see something set to 0 here if you’re counting up to a variable.
  2. The conditional:The conditional is the same as with a while loop. It should be a statement that evaluates to a true, false, or boolean(t/f) statement.
  3. The iterator:The iterator is the part of the for loop in which we increase, decrease, or otherwise change some aspect of the loop, usually to make the loop get closer to it’s conclusion.


Additional considerations of for loops
The syntax and usage of for loops can be a little tricky overall:


for(int i=0; i<10; i++){
     //null
}
cout << i; //This will error

The above is an example of an error that I see quite often among my peers in college. Despite the initializer coming before the scope bracket, it is considered part of the scope of the loop. Therefore we can thing of the scope of a for loop being like this:

for{(int i=0; i<10; i++)
     //do stuff
}
rather than its existing structure.


Incrementing numbers
Any number that is of a type that can hold a whole number value can be incremented or decremented. In MOST cases you will be using “post-incrementing” which is seen as varName++; however, there are other ways of incrementing numbers:


++i; // pre-increment
i++; // post-increment (most-common)
--i; // pre-decrement
i--; // post-decrement (common while decrementing)

Each time you hit the statement i++;, you increase the value of i by the value of 1.


Nested for structure
Nested for’s actually share syntax with nested if statements in terms of scoping and complexity. Granted there aren’t any else statements to take into consideration.

C++ Tutorial #9 — Section 1 review
C++ Tutorial #11 — Common loop errors, break statement, and sentinel loop.