Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In this program, the loop control variable only increases by 1 every iteration but the output shows otherwise

Tags:

java

In the following code from "Java: A Beginner's guide", the for loop seems to iterate more than once when a single character is typed, even though the loop control variable, i, should only be incremented by one each iteration.

The condition to enter the for loop is based on user input. The program will enter the loop and increment i by one until the character S is typed by the user. Every time the program enters the loop, i is printed out.

class ForTest {
  public static void main(String args[])
    throws java.io.IOException {

  int i;

  System.out.println("Press S to stop.");

  for(i = 0; (char) System.in.read() != 'S'; i++)
    System.out.println("Pass #" + i);
  }
}

So it is expected that when a character other than S is typed, the program prints out Pass #0 and then waits for the user to input the next character. Oddly, it loops thrice, printing Pass #0 Pass #1 and Pass #2 before asking for user to input the next character.

Expected:

a
Pass #0
b
Pass #1
S

Actual:

a
Pass #0
Pass #1
Pass #2
b
Pass #3
Pass #4
Pass #5
S
like image 308
Sandy Avatar asked Jan 12 '19 04:01

Sandy


People also ask

Which variable controls the number of iterations performed by a loop?

Expert-Verified Answer. The loop control variable is a variable that controls the number of iterations performed by a loop.

Which refers to the number by which the control variable should get altered after each execution of the loop?

The variable count is initialized, tested, and changed as the loop executes. It is an ordinary int variable, but it is used in a special role. The role is that of a loop control variable.

What is a loop control variable example?

A common use of loops is the one in which the loop makes use of a variable (called control variable) that at each iteration is changed by a constant value, and whose value determines the end of the loop. Example: Print the squares of the integers between 1 and 10.

What is the condition of a for loop?

If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop. After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement.


1 Answers

If you change a little the program in order to debug it:

char myChar;

for(i = 0; (myChar = (char) System.in.read()) != 'S'; i++)
    System.out.println("Pass #" + i + " the character from the console is: " + (byte)myChar);
}

and then run it, you will see what characters are actually comming from the input stream:

Press S to stop.
Pass #0 the character from the console is: 97
Pass #1 the character from the console is: 10
Pass #2 the character from the console is: 98
Pass #3 the character from the console is: 10

97 - is a
10 - is line feed
98 - is b
10 - is line feed


I hope it is clear now to you - if you press a + Enter, then the console returns a + line feed characters to the program, that is two characters, not one.

like image 99
krokodilko Avatar answered Nov 27 '22 01:11

krokodilko