This question already has an answer here:
- Unexpected result in simple java program 4 answers
I'm working through a piece of code from the "Java: A Beginner's Guide 6th Ed." The section I'm on is explaining the different ways to implement a For Loop.
// Loop until an S is typed. System.out.println("Press S to stop."); for(int i = 0; (char) System.in.read() != 'S'; i++) System.out.println("Pass #" + i);
My question is this - why does it execute 3 times before asking for a new keyboard input? When I type any character, it outputs:
Press S to stop. T Pass #0 Pass #1 Pass #2 P Pass #3 Pass #4 Pass #5
I would expect it to run once and then wait for the next input.