can someone enlighten me on this?

boolean entry=false;

for(int i=1;(!entry);i++)

{

String s = in.readLine();

int a = Integer.parseInt(s);

if(a!=1 && a!=2)

{

System.out.println("Invaild entry");

System.out.println("\n Enter '1' or '2' ");

}elseif(a==1)

{

//do something

entry=true;

}elseif(a==2)

{

//do something

entry=true;

can someone explain to be wat does the for loop in this program do?

rather confused with the boolean variable used.

does (!entry) refers to a value 'not false' which otherwise known as a 'true' value? if it does, why would the for loop exit if the entry=true?

thanks

[1365 byte] By [geniver82a] at [2007-10-2 2:05:54]
# 1

It reads from the input console as long as the input is not 1 or 2. If the input is 1 or 2, the else branches are processed, the entry flag is set to 'true' and therefore the loop ends (!entry = !true = false).

[url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html]The for statement[/url]

MartinHilperta at 2007-7-15 19:47:20 > top of Java-index,Java Essentials,New To Java...
# 2

That's horrible. It would be a lot more readable as while (true)

{

String s = in.readLine();

int a = Integer.parseInt(s);

if (a == 1)

{

//do something

break;

}

else if (a == 2)

{

//do something

break;

}

System.out.println("Invaild entry");

System.out.println("\n Enter '1' or '2' ");

}

(I'd probably ditch the Integer.parseInt as well, and just do string comparison).

YAT_Archivista at 2007-7-15 19:47:20 > top of Java-index,Java Essentials,New To Java...