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 ...

Get Java Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.