Results 1 to 11 of 11
  1. #1
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Unhappy [SOLVED] need help with logic operator

    hello all
    i'm a newbie in java
    actually i'm a c++ programmer

    but now i think i need help
    i have this code in c++:
    while((h = hashTab[++j].Key) && (h - HashKeyHi && --i));

    all variables are integer

    i've got this error message:
    operator && cannot be applied to int,int

    well, i guess that's why i love c++ so much... :D
    any idea what should i do?
    how can i deal with this && operator in java?

    thanks all...!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ints can't substitute for booleans in Java as they can in C++. You'll need to put some == and != in there to make it work.

  3. #3
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    ahh... now i know..
    you inspired me...

    while(true){
    if((h = hashTab[++j].Key) == 0
    || (h - HashKeyHi) == 0
    || (--i) == 0)
    break;
    }

    thanks fubarable...
    now i know why i love c++...
    :)

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default May not...

    Your initial logic indicated logic "and", yet you implemnted it with logic "or". Shouldn't your code be:
    Java Code:
    while(true){
    if((h = hashTab[++j].Key) == 0 
    [B][COLOR="Red"]&&[/COLOR][/B] (h - HashKeyHi) == 0 
    [B][COLOR="red"]&&[/COLOR][/B] (--i) == 0)
    break;
    }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    I don't think so...
    first: operator && will return 0 if at least one operand value is zero...

    second:looking at my first code... i want it to break out of while loop if at least one of these condition is zero:
    -> h = hashTab[++j].Key
    -> h - HashKeyHi
    -> --i

    third: that's why i compare them with zero and use or...

    fourth: your code will execute break if all conditions are zero...

    fifth: i've tried it... :)

    sixth: thanks a lot for your feedback...
    i really-really appreciate it...
    :D

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default huh...

    I'm not sure we're on the same frecuency here... If the code is doing what you want it to do, then great, but you're statements of logic are not right...

    first: operator && will return 0 if at least one operand value is zero...
    second:looking at my first code... i want it to break out of while loop if at least one of these condition is zero:
    -> h = hashTab[++j].Key
    -> h - HashKeyHi
    -> --i
    The the above statements say the same thing.
    third: that's why i compare them with zero and use or...
    The "compare with zero" part I agree with, but the "or" part I do not agree with. With "or" all the comparasion statements have to be != 0 before it the while breaks out.
    fourth: your code will execute break if all conditions are zero...
    Not so... using "&&" is the same condition as the above points one and two.
    fifth: i've tried it...
    Again, if it's working the way you want it to, that's good.

    Luck,
    CJSL






    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    i dont get it... :confused:

    Quote Originally Posted by CJSLMAN View Post
    With "or" all the comparasion statements have to be != 0 before it the while breaks out.
    i used or and compared it with zero...
    so if 0==0 ==> true
    since i use or, i need only one condition to be true...
    [TRUE] OR [DON'T CARE] = TRUE

    dont you thik so...:)

    Thx...

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default My head hurts

    Yes, again, I agree with you that with "or" if you have one true statement in your "if", the whole statement is true, but I thought you were look for conditions to "break" out of the "while" loop using "and" logic (as your original code stated).
    Before I go on with this discussion (apparently pointless), you want ALL the condition statements to be false (!=0), before you break out of the loop, correct?

    Java Code:
    while(true){
    if((h = hashTab[++j].Key) == 0 
    || (h - HashKeyHi) == 0 
    || (--i) == 0)
    break;
    }
    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    yup....
    :D

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ok...

    Glad it's working for you...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    auralius is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks...now if i have other questions, i won't hesitate to ask...
    because you people care so much...
    thanks again...

    :)

Similar Threads

  1. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 02:46 PM
  2. Logic to generate a pattern
    By vijay_2008 in forum Advanced Java
    Replies: 3
    Last Post: 11-23-2008, 02:40 AM
  3. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 02:13 AM
  4. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM
  5. iterate HashMap with logic
    By Heather in forum Web Frameworks
    Replies: 2
    Last Post: 07-03-2007, 09:47 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
  •