Topic : Loops
Author : Naos
Page : 1

Good day/evening!

This simple tutorial is destined to those who have already learned the way loops look like and the way they work!
It deals with the "break","continue" operators used in loops and infinite loops!

<<break>>

The use of "break" in a loop is to stop it!

....for loop....

for (int a=0 ; a<=6 ; a++) {
if (a == 3) break;
}


This loop will stop when "a" reaches the value of "3" because "if" returns "true" and then the break operator is executed!

....while loop....

int a = 1;
while (a) {
cout << "Enter value or 0 to close.\n"
cin >> a;
}


Note: This is a way to enter as many values as you want untill the variable in the "( )" is equal to "0". That's because if the expression in the "( )" is [0==false]
while stops.

So if you want the while loop to stop when "a" is equal to...let's say "6" do this:

int a = 1;
while (a) {
cout << "Enter value or 0 to close.\n"
cin >> a;
if (a == 6) break;
}


Remember the "break" operator breaks only the loop it is in!

This is a problem for you to solve:

....complex problem....

for (int a=0; a<=3 ; a++)  //loop 1
for (int b=0; b<=3 ; b++)  //loop 2
if (a == 2) break;


Question: What will be the value of "a" when this loop stops?

Answer: The value of "a" will be 4. Why? Do this to find out:

for (int a=0; a<=3 ; a++)  //loop 1
for (int b=0; b<=3 ; b++) { //loop 2
if (a == 2) break;
cout<< a <<endl;
}
cout<<"\"a\" after the loop is: "<<a<<endl;


Note: The break operator terminates the second loop. So the first loop finishes on its own.

<<continue>>

The "continue" operator moves you at the beggining of the loop (jumps over the rest of
the loop's body)!

....for loop....

for (int a=0; a<=5; a++) {
cout << "My_mind++ "<<a<<"\n";
}


output:

My_mind++ 0
My_mind++ 1
My_mind++ 2
My_mind++ 3
My_mind++ 4
My_mind++ 5


but if you do this:

for (int a=0; a<=5; a++) {
if (a == 3) continue;
cout << "My_mind++ "<<a<<"\n";
}


output:

My_mind++ 0
My_mind++ 1
My_mind++ 2
My_mind++ 4
My_mind++ 5

That's because when a==3 the "cout" line is not executed(simply missed)! However the expression
in the loop is reevaluated and the check is done again (the loop doesn't stop)!
  
....while loop....

Let's say we have this:


int a=0;
while (a++!=5) {
cout << "My_mind++ "<<a<<"\n";
}


output:

My_mind++ 1
My_mind++ 2
My_mind++ 3
My_mind++ 4
My_mind++ 5

but if this is the case:

int a=0;
while (a++!=5) {
if (a == 3) continue;
cout << "My_mind++ "<<a<<"\n";
}


output:

My_mind++ 1
My_mind++ 2
My_mind++ 4
My_mind++ 5

The idea is the same as in the "for" loop!


<<infinite loops>>

The first thing you should know about infinite loops is "Don't use them very often.".
Basically infinite loops are always used with the "break" operator!!!

....for loop....

for (;;) {
...
}


Whatever you write in this loop it will be executed over and over again!
This was the simplest type of infinite loop!

Here is a use of the "break" operator:

for (int a=0; a>=0 ; a++) {
if (a==4) break;
}

It will stop when a==4!

....while loop...

while (true) {
...
}


This is the simplest infinite while loop.

The use of "break" is the very same as in "for"!

int a=1;
while (a) {
if (a==3) break;
a++;
}





I hope this tutorial made things clearer on this matter.


Homework:

Is this loop infinite?

[1]

for (int a=0; a<=6; a++) {
cout << "Booo!\n";
for (int b=0; b>=0; b++) {
if (b == 6)break;
if (a == 5) continue;
}
}


[2]

for (int b=0;b<=6;b++)
for (int a=-100;a<=1;a=a/a) {
if (a==-60) continue;
if (a/-30==2) break;
cout << "Boooo! "<<a<<" \n";
a++;
}






{F1} [1] no  [2] yes




Note: The codes've been compiled successfully using MS Visual C++ 6.0. All of them run fine.
If you think there're bugs in the code please report.

Page : 1