While Statement

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

*Syntax: while ( Expression ) Statement
 
*Semantics: The expression must be of type boolean. If it evaluates to false, the given statement is skipped, Otherwise it is executed and afterwards the expression is evaluated again. If it is still true, the statement is executed again. This is continued until the expression evaluates to false.
 
*Following layout conforms to our coding standard:

while ( Expression ) {
    Statement1
    Statement2
    ...
}

 

HiLo.java
      guess = RitIo.getInteger("Your guess: ");
      tries = 1;
      while (guess != secretNumber) {
         if (guess < secretNumber) {
            System.out.println("Too small!");
         } else {
            // guess > secretNumber
            System.out.println("Too large!");
         }
         guess = RitIo.getInteger("Your guess: ");
         ++tries;
      }

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002