Thread: what does this mean

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    what does this mean

    in some program i saw this line
    Code:
    result += array[i] ? array[i] : 90;
    i was wonderind what does this mean. hope you can help.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    it's the same as:
    Code:
    if(array[i])
           result+=array[i];
    else
           result+=90;
    Instead of doing all that C has the tertiary construct (three involved elements), (evaluate)?(if true): (if false).

    Hope this helps

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    yes, thanks , it helps, but i'm still a little confused ,how should i understand
    Code:
    if(array[i])
    that means if value of array[i] is true ,right?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    Pretty much, just checking wether there is anything at array[i]. With "anything" meaning a value other than 0 (or if there is no element at that index)(which would be false), all other values are true(basically there is an element there)(so not only 1).
    Last edited by coolman0stress; 08-14-2003 at 01:17 PM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    ok, thanks, i understand it now

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>(or if there is no element at that index)(which would be false)<<
    There is always a value at that location (index), even if the value of i exceeds the array bounds.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Just conditional operator man. You can refer tutorials.
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed