I have the following program:
public class ForTest {
public static void main(String[] args)
throws java.io.IOException {
int i;
System.out.print("Press S to stop.");
for (i = 0; (char) System.in.read() != 'S'; i++)
System.out.println("Pass #" + i);
}
}
What I am expecting to receive upon executing this program (after entering say 'A', then 'B', then 'C') is the following:
Press S to stop.'A'
Pass # 0
'B'
Pass # 1
'C'
Pass # 2
Instead, I would receive something like this:
Press S to stop.'A'
Pass # 0
Pass # 1
Pass # 2
'B'
Pass # 3
Pass # 4
Pass # 5
etc.
I am having trouble understanding why the body of the for loop is being executed multiple times before read is invoked again. Any help here would be much appreciated, even if it's just a link to the relevant javadoc. Thanks in advance!