Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: While statement with a constant condition

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default While statement with a constant condition

    I have came across variations of this code on multiple websites now:

    Java Code:
    while ((sCurrentLine = br.readLine()) != null) {
    				System.out.println(sCurrentLine);
    			}
    So, as long as the line being read is not null, print the line. However, surely if the condition was not met to begin with, it will never be met and an infinite loop will be started, right? The variable is never changed during the while loop.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: While statement with a constant condition

    On the contrary, every time around the loop the variable is changed. Or did you think that every call to br.readLine() would return the same value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: While statement with a constant condition

    Quote Originally Posted by DarrylBurke View Post
    On the contrary, every time around the loop the variable is changed. Or did you think that every call to br.readLine() would return the same value?

    db
    It will return the same value won't it?

    Doesn't readLine() read the a file up until the a newline then stop reading?

    Maybe I am misunderstanding the method, the javadoc wasn't too helpful.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: While statement with a constant condition

    Quote Originally Posted by kkid View Post
    Doesn't readLine() read the a file up until the a newline then stop reading?
    Yes, and the next time it's called it reads up to the next newline.

    db
    kkid likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: While statement with a constant condition

    Quote Originally Posted by DarrylBurke View Post
    Yes, and the next time it's called it reads up to the next newline.

    db
    oooooohhh... that's clever.

    How does it remember where it left off? Does a BufferedReader object have some sort of counter variable for use with this method?
    Can the count be reset? If so, how? If I was right with my guess and it is a variable in BufferedReader objects, is it merely a case of setting this variable back to 0?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: While statement with a constant condition

    Read the API, it's all there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Why is my condition statement met? (% operator)
    By EscSequenceAlpha in forum New To Java
    Replies: 3
    Last Post: 04-02-2012, 08:02 PM
  2. Cannot use as static constant
    By KiranA in forum Eclipse
    Replies: 1
    Last Post: 12-01-2011, 09:19 AM
  3. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 07:51 AM
  4. constant variables questions
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 05:34 AM
  5. difficulties with IF statement condition
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 10-12-2010, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •