Please enable JavaScript to use CodeHS

CodeHS Glossary


Loop-and-a-half JavaScript

The loop-and-a-half is preferred because it avoids repeating code outside and inside the loop. By using while(true), the loop-and-a-half structure only reads in the variable inside the loop:

var SENTINEL = -1;

function start(){
    while(true){
        var num = readInt("Number? ");
        if(num == SENTINEL) {
            break;
        }
        println(num);
    }
}