0

how is the flow of execution directed in the code below.(how does 'i' get updated(or incremented)?

while((i=fr.read())!=-1)
{ System.out.print((char)i);
} 

- See more at: http://tutorialspointexamples.com/filereader-and-filewriter-in-java/#sthash.QxSyiKRe.dpuf

1 Answer 1

1

Filereader.read reads a single character. It returns an int, which represents the character read.

in your code

while((i=fr.read())!=-1)
{ 
   System.out.print((char)i);
} 

(i=fr.read()) resolves first, giving you the character read. The while loop will resolve this each time it loops, moving through the file.

The outer while then compares this value against -1.

  • If it does, the while loop ends.
  • If it doesn't match it outputs the character.
Sign up to request clarification or add additional context in comments.

3 Comments

but how does the loop proceed since there is no updation for the counter variable( in this case i) in the loop.
i isn't a counter variable. i is the value that is returned by the read call. fr has an internal file pointer - read just tells it to read the next character. see tutorialspoint.com/java/java_filereader_class.htm
@pradeep in other words, when there are no more characters to read, i would be set to -1, and the loop would terminate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.