O'Reilly logo

Java Programming for Beginners by Mark Lassoff

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

While loops

Often, we allow small programs, such as the ones we're writing here, to end when there's simply no more code for them to execute. However, while we're working with loops, we're probably going to make the mistake of accidentally creating an infinite while loop and running a program that has no end:

package introtoloops; 
 
public class IntroToLoops { 
    public static void main(String[] args) { 
        int i=5; 
         
        while (i>0) 
        { 
            System.out.println("Hello world"); 
        }  
    } 
} 

When this happens, we'll need to turn our program off manually. In NetBeans, there's a handy little feature called Stop at the left-hand side of the output window:

If we're running ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required