dhust
8/1/2013 - 9:03 PM

Equivalent While and Do-While programs

Equivalent While and Do-While programs

/********** WHILE STATEMENT **********/

public static void main(String[] args) {

  	// Variables
	Scanner input = new Scanner(System.in);
	String userResponse = "";

	// Valid input check
	while ( !( (userResponse.equalsIgnoreCase("a")) || (userResponse.equalsIgnoreCase("b")) ) ) {

		// Ask question, a or b
		System.out.print("Enter a or b: ");

		// Store response
		userResponse = input.next();

	}

	// Print response
	System.out.printf("You entered the letter %s\n", userResponse);

}

/********** END WHILE STATEMENT **********/



/********** DO-WHILE STATEMENT **********/

public static void main(String[] args) {

	// Variables
	Scanner input = new Scanner(System.in);
	String userResponse;

	// Valid input check
	do {
		// Ask question, a or b
		System.out.print("Enter a or b: ");

		// Store response
		userResponse = input.next();

	} while ( !(userResponse.equalsIgnoreCase("a")) && !(userResponse.equalsIgnoreCase("b")) );

	// Print response
	System.out.printf("You enter the letter %s\n", userResponse);
	
}

/********** END DO-WHILE STATEMENT **********/