# Ends a pause in nested loops

### C course - loops - infinite breaks and continuities

For all three types of loops, for loop, while loop and do-while loop there are still two important instructions, namely break and continue.

The break Statement is somewhere in the body of the loop, mostly in connection with an if query. If the program runs into it, it cancels the loop. If you come across a break Statement, the loop is left without further ado and the program is then advanced. The purpose of the break statement is therefore to break off the loop when a certain state occurs without having to incorporate an orgy of if ... else ... queries. Here's an example of catching a possible error, the square root of a negative number, and then exiting the loop:

#include // for printf
#include // for sqrt

int i, x = 55;
double x;

for (i = 100; i> -x; i--)
{
if (i + 10 <0) // if i + 10 less than zero then ...
{
printf ("I have no idea about complex numbers ... loop will be canceled \ n");
break;
}

x = sqrt (i + 10); // sqrt - square root - the square root of i + 10
}

With nested loops, of course, break only applies to the current loop. If you also want to break off the higher-level loops, you have to think of something like this:

int i, j, k, done = 0;

for (i = 0; i <100; i ++)
{
if (done)
break;

for (j = 0; j> -10; j--)
{
if (done)
break;
for (k = -10; k <10; k ++)
{
if (i * j * k <0)
{
done = 1;
printf ("break in i:% d j:% d k:% d \ n", i, j, k);
break;
}
printf ("still looping i:% d j:% d k:% d \ n", i, j, k);
}
}
}

The last 5 lines of the output are:

still looping i: 1 j: -1 k: -3
still looping i: 1 j: -1 k: -2
still looping i: 1 j: -1 k: -1
still looping i: 1 j: -1 k: 0
break in i: 1 j: -1 k: 1

The continue Statement jumps to the beginning of the loop, i.e. skips the statements between continue and the end of the loop. The purpose of the continue The instruction is to interrupt the further execution of the loop body in a simple way until the next pass. In contrast to break, will not exit the loop. Here's an example:

int i = 0, x = 0;

while (i <100)
{
i ++;

if ((i> 9) && (i <21)) // i is between 10 and 20
continue;
if ((i> 59) && (i <71)) // i is between 60 and 70
continue;
if ((i> 79) && (i <91)) // i is between 80 and 90
continue;

// summation; is only executed if i Not between 10-20, 60-70 and 80-90
x = x + i;
}

Either break as well as continue may appear several times in different places in the loop, this is where the advantage lies, as you can see in the example.

Endless loops:

For one, there is unwanted endless loops, you program a logical error into the loop, start the program and wonder for a few minutes that nothing more happens. The program then unfortunately has to be shot down. Then you start it again, either in the debugger, or with printf instructions in different places and then find something like this, for example:

int i;

for (i = 0; i <100; i ++)
{
...
if (i == 42)
i = 0; // oops
...
}

or something like this:

int i = 0;

while (i <100)
{
...
if (i == 42)
continue;
...
i ++; // until i turned 42 for the first time, it worked fine, after that i ++ is never executed again
}

Then there are intended infinite loops, The programmer couldn't think of any termination condition (or too many). These must then be ended with one or more break statements:

int x = 0; y = 0, z;

for (;;) // endless for loop
{
z = z + (x * y);
x ++;
y--;
z + = 10;

if (z> 1000)
break;
if (z <-2000)
break;
if (i> 100)
break;
if (y <-200)
break;
}

or:

char c;

while (1) // endless while loop; 1 is always true
{
if (c = getchar () == 'q') // did the user type q?
break;
}

You will use all three types of loops, for while and do-while, as well as break, continue and infinite loops, so make yourself familiar with them.

The following mistake sometimes occurs when programming loops, I've made it myself often enough, so here's the hint:

int i;

for (i = 0; i <666; i ++);
{
...
}

Do you see it? Behind for (i = 0; i <666; i ++) there is still a semicolon;! That means rather compiler, let i run in a loop from zero to 666, then execute this strange single block in curly brackets (once). But this is correct C code, not a syntax error. It's probably not what you wanted to do, though, so hopefully you'll get a warning from the compiler unless you've set the compiler's warning level very low.

If you absolutely want to (or have to) program Windows then this is the Petzold definitely the right one: