Java for Complete Beginners (Video), Part 8: Do … While

Do…while loops in Java, plus a look at variable scoping and multi-line comments. In this tutorial I tackle a task that’s often assigned to beginners on Java courses; looping until the user enters some particular input.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.

Code for this tutorial:

import java.util.Scanner;


public class App {

	public static void main(String[] args) {

		
		Scanner scanner = new Scanner(System.in);
		
		/*
		System.out.println("Enter a number: ");
		int value = scanner.nextInt();
		
		while(value != 5) {
			System.out.println("Enter a number: ");
			value = scanner.nextInt();
		}
		*/
		
		int value = 0;
		do {
			System.out.println("Enter a number: ");
			value = scanner.nextInt();
		}
		while(value != 5);
		
		System.out.println("Got 5!");
	}

}

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Tutorial, Java Video Tutorials (Beginners) | Tagged , , |