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

Thread: condition

  1. #1
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default condition

    hi
    please help me understand this piece of code

    Java Code:
    boolean condition = false;
    while (!condition){
    //do something
    }
    
    if(!condition){
    // do something
    }
    I know that !true means false and vice versa
    I also have some statements which are familiar for me like
    if (something == true)
    or while (something == true)
    but actually I don't understand the previous piece of code
    which is equal to "!condition" ??
    I know it means while (true) but does this mean that the loop won't terminate ?
    and what about if(true)? which is equal to true ?

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: condition

    The loop will only not terminate if there is no code in the loop to terminate it.
    This could be a 'break;' command or some code changing the value of 'condition' to 'true'.
    What happens with the if statement is entirely dependent on how the loop exited.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: condition

    what if you considered them as separate pieces? I mean what if the if statement was dependant (no loops before it).

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: condition

    Then the if condition would be utterly pointless as it would always be true.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: condition

    so if !condition becomes false this should breaks out of the code and it wouldn't execute the if's body, right ?

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: condition

    so if !condition becomes false this should breaks out of the code and it wouldn't execute the if's body, right ?
    It doesn't break, but you are correct, it merely skips the if body because the result of the boolean evaluation is false.
    Kareem Mesbah likes this.

Similar Threads

  1. Why twice condition check?
    By megabull in forum New To Java
    Replies: 4
    Last Post: 09-11-2012, 06:14 PM
  2. Condition ignored.
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 12-09-2011, 02:21 AM
  3. Waiting on a condition
    By nephos in forum New To Java
    Replies: 9
    Last Post: 04-22-2011, 10:05 AM
  4. basic condition
    By ts96 in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 12:23 PM
  5. While loop condition
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:14 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
  •