Operators Break and Continue


for example, forex

Early exit from the loop with the help of the break operator

Rather often it is required to exit the loop early – for this we use the break operator.

Early exit operator break

The break operator is used for an early exit from the nearest nested external operator of the while or for loop. Also the break operator is used for exit from the switch operator.

An example of usage of the break operator:

   int i;
   for ( i = 0; i<10; i++ )
      {
        ...
        if ( i == 5 ) break;
      }

In our example the loop will be executed not ten but only six times, as at the moment when the variable i equals 5 in the if-else operator the condition i=5 will become true and the operator break will be executed, as an early exit from the loop will take place.

Early termination of an iteration with the help of the continue operator

Sometimes it it required not to exit from the loop but to terminate early one of its iteration. For this the continue operator is used.

Operator continue breaks the execution of the current iteration and passes the control to the beginning of the nearest external operator of the for or while loop, launching the execution of the next iteration.

An example of the continue operator usage :

   int i;
   for ( i = 0; i < 10; i++)
      {
        ...
        continue;
        Print ("Study the continue operator");  //this operator will never be executed
      }


Next article: "Switch Operator"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?