Loops in C++

By Herbert J. Bernstein

© Copyright 1999, 2002 Herbert J. Bernstein

 

There are three major loop control structures in C++

.

The same loop control structures are available in Java, but the break and continue accept an optional statement label and no goto statement is available. In addition, in Java, the try ... catch construct provides additional control over loop exits in the case of exceptions.

The behavior of these control structures is modified by the following statements

The break statement causes an immediate termination of the enclosing while, do or for statement (or of an enclosing switch statement). Control passes out only one level, so that if the break statement is within a nested statement for which it is also effective, further action may be needed to return control to the desired level.

The continue statement causes an immediate termination of the current pass through the enclosing while, do or for statement . Control passes to the logic that determines if another pass should be made, and is onloy effective for the immediately enclosing level, so that if the continue statement is within a nested statement for which it is also effective, further action may be needed to return control to the desired level.

Execution of a loop within a function may also be terminated by a return statement.

The while statement evaluates the expression. If the result is non-zero, the associated statement is executed and then the process is repeated. Exection continues to loop until the expression evalutes to zero.

The do … while statement is similar in effect, except the associated statement is executed at least once.

The for statement is typically used to control iteration with some variable that is being stepped through a range of values, e.g. for ( i = 0; i < limit; i++) statement will be executed for values of I equal to 0, 1, … limit-1.

We may convert one of the programs we used to illustrate conditionals to use a for instead of a while loop:

#include
<iostream.h>

char instream[1024];

double myatod(const char xstring[]){
  int ipos /* = 0*/;   /* position within string */
  int isign = 0;       /* flag for sign:
                          0 not found, -1 negative, 1 positive */
  int digfound = 0;    /* flag for digits found  */
  int idig;            /* current numeric digit  */
  int decpos = 0;      /* position after the decimal point */
  double xvalue = 0.;  /* tentative return value */
  double xpow10 = 1.;  /* next (negative) power of ten */
  register char c;     /* next character from string */

  /* while (c = xstring[ipos++]) { */
  for (ipos = 0; c = xstring[ipos]; ipos++) {
    switch (c) {
      case ('0'): idig = 0; digfound = 1; break;
      case ('1'): idig = 1; digfound = 1; break;
      case ('2'): idig = 2; digfound = 1; break;
      case ('3'): idig = 3; digfound = 1; break;
      case ('4'): idig = 4; digfound = 1; break;
      case ('5'): idig = 5; digfound = 1; break;
      case ('6'): idig = 6; digfound = 1; break;
      case ('7'): idig = 7; digfound = 1; break;
      case ('8'): idig = 8; digfound = 1; break;
      case ('9'): idig = 9; digfound = 1; break;
      case ('.'):
        if (!decpos) {
          idig = 0;
          digfound = -1;
          decpos++;
          xpow10 /= 10.;
        } else {
          return (isign?isign:1)*xvalue;
        }
        break;
      case ('-'):
      case ('+'):
        if (!isign && !digfound) {
          isign = (c == '-')?(-1):1;
        } else {
          return (isign?isign:1)*xvalue;
        }
        break;
      default: idig = -1;
    }
    if (idig == -1) return (isign?isign:1)*xvalue;
    if (digfound > 0 ) {
      if (!decpos) { 
      xvalue = xvalue*10. + (double)idig;
      } else {
        xvalue = xvalue + ((double)idig)*xpow10;
        decpos++;
        xpow10 /= 10.;
      }
    }
  }
  return (isign?isign:1)*xvalue;
}

int main( void ) {
  double num = 1.;
  while (num != 0. ) {
    cout << "Type a number: ";
    cin  >> instream;
    cout << (num = myatod(instream)) << endl;
  }
}