do-while Statement

do-while Statement


Posted in : Core Java Posted on : October 7, 2010 at 5:27 PM Comments : [ 0 ]

In this example, We will discuss about the use and implementation of the do-while statements in the java programming.

do-while Statement

In this example, We will discuss about the use and implementation of the do-while statements in the java programming. do-while loop executes all the statements first at once and then check the condition if the condition is true then all the statements are also executed in second times otherwise second times ignored all the statements.

The Syntax for the do-while loop:

    do
    {
        statements;
    }
    while(condition);

In this example you will see how to use the do-while loop statement. the input is given by the user as.

package com.devmanuals2;

import java.io.*;

public class DoWhileExample {

	public static void main(String[] args) throws IOException {
		int ch = 0;

		do {
			System.out.println("Enter any no from 1 to 6");
			BufferedReader buffread = new BufferedReader(new InputStreamReader(
					System.in));
			String str = buffread.readLine();
			ch = Integer.parseInt(str);
			if (ch < 7) {

				switch (ch) {
				case 1:
					System.out.println("You are a smart person");
					break;
				case 2:
					System.out.println("You are a clever person");
					break;
				case 3:
					System.out.println("You are a follower person");
					break;
				case 4:
					System.out.println("You are a unsatisfied person");
					break;
				case 5:
					System.out.println("You are a lazy person");
					break;
				case 6:
					System.out.println("You are a disappointed person");
					break;
				}
			} else {
				System.out.println("Exit from the program........");
				System.exit(0);
			}
		} while (ch < 7);

	}
}

Output:

Enter any no from 1 to 6

1

You are a smart person

Enter any no from 1 to 6

8

Exit from the program........

Download The Code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics