Please enable JavaScript to use CodeHS

CodeHS Glossary


Loop-and-a-half Java

A loop, most often set with `while(true)`, that has a `break` statement in the loop body. The loop-and-a-half is preferred when continuously reading input from a user 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: ``` int SENTINEL = -1; while(true){ var num = readInt("Number? "); if(num == SENTINEL) { break; } System.out.println(num); } ```