HIDE NAV

Simple While Loop

The below Java pseudo-code shows some basic programming control styles used with while loops including counting loops, boolean controlled loops and infinite loops exited with break.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        /** boolean controlled loop */
        boolean keepLooping = true;
        while(keepLooping){
            /** set the control boolean false to exit after the loop run finishes */
            if( /** condition */) keepLooping = false;
        }

        
        /** counting loop */
        int loopRepeatCount = 100;
        int i = 0;
        while (i < loopRepeatCount){
                /** runs exactly loopRepeat times */
            i++;
        }

        
        /** infinite loop with break */
        while(true){
            /** use a condition to call 'break'*/
            if( /** condition */) break;
            /** note 1: any code below 'break' within the loop will be skipped if 'break' is executed */
            /** note 2: in nested loops, 'break' only exits the inner most loop */
        }

    }
}