View Full Version : Why does this while loop iterate

03-20-2012, 07:02 AM
If the char variable again is initialized to 'N' then why does this loop iterate the first time

import java.util.Scanner;

public class teest
public static void main (String[] args)
Scanner keyboard = new Scanner(System.in);

int num;
String input;
char again = 'N';

while(again == 'Y' || again == 'y')
System.out.println("enter num: ");
num = keyboard.nextInt();
System.out.println("The number entered was " + num);
System.out.println("Do it agian?");
input = keyboard.nextLine();
again = input.charAt(0);

03-20-2012, 07:12 AM
oh sorry wrong program

03-20-2012, 05:15 PM
It doesn't.
Although your original code posted did since it contained a semi-colon at the end of a control structure.