Results 1 to 4 of 4
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default While loop condition

    Hi new to java here,

    Just a bit confused with while loops.

    Can a while loop test against a boolean variable? All the examples I find on the net are always of < or > expressions. So how would we write the condition of the while loop.

    while(???) {
    }

    If i wanted to lets say make a condition so that a boolean variable isEmpty is not equal to false that the while loop will keep continueing. Is it written as:

    while(!isEmpty == false)

    or

    while(isEmpty != false)

    I been trying this but I keep getting infinite loops and stuff I just would appreciate if anyone could explain this in laymans terms, also how would in the body of the while loops would be a practical way of ending the loop by changing isEmpty to true.

    Thanks in advance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Quote Originally Posted by counterfox View Post
    Hi new to java here,

    Just a bit confused with while loops.

    Can a while loop test against a boolean variable? All the examples I find on the net are always of < or > expressions. So how would we write the condition of the while loop.

    while(???) {
    }

    If i wanted to lets say make a condition so that a boolean variable isEmpty is not equal to false that the while loop will keep continueing. Is it written as:

    while(!isEmpty == false)

    or

    while(isEmpty != false)

    I been trying this but I keep getting infinite loops and stuff I just would appreciate if anyone could explain this in laymans terms, also how would in the body of the while loops would be a practical way of ending the loop by changing isEmpty to true.

    Thanks in advance.
    :confused:

    while(isEmpty==false) or
    while(!isEmpty)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If i wanted to lets say make a condition so that a boolean variable isEmpty is not equal to false that the while loop will keep continueing. Is it written as:

    while(!isEmpty == false)

    or

    while(isEmpty != false)
    Rather than thinking of a variable being equal to false, just think of the variable being false.

    (isEmpty != false) means the same as (isEmpty): "is-empty is not false"

    (!isEmpty == false) means exactly the same thing: "is-not-empty is false"

    If boolean comparisons between boolean expressions are confusing (and they are!) don't use them. Just choose between while(isEmpty) and while(!isEmpty) to express what you mean.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by eRaaaa View Post
    while(isEmpty==false) or
    while(!isEmpty)

    With respect, I don't think the first of these is an option to be seriously considered or suggested.

    At least no more serious than, say, while(isEmpty == !isEmpty == isEmpty)

Similar Threads

  1. difficulties with IF statement condition
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 10-12-2010, 08:38 AM
  2. IF-Condition in a String
    By lenaz in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 12:07 PM
  3. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:17 AM
  4. problem with using string in if condition
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 10:40 PM

Posting Permissions

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