Results 1 to 14 of 14

Thread: A question about If-else and (condition)?: operator

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    A question about If-else and (condition)?: operator

    What do I save when I substitute the following code:

    Code:
    if ( pItemData->nSelCheck )
         iImg = 1;	
    else 
         iImg = 0;
    with this one:

    Code:
    iImg = ( pItemData->nSelCheck )? 1: 0;

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Three lines
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Originally posted by crptcblade
    Three lines
    Funny.

    Yes definitely...
    I know that but what I mean is in terms of processing, whatever it's called...
    Will the program run faster or slower.
    How does the CPU process the latter statement?

  4. #4
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    I seen the post and was really curious about this and tested it out.

    Seems MSVC generates one less assembly instruction for the first,if branch.
    Whether that translates into faster i guess would depend on the CPU.

    my worthless two cents.


  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    with the VB equivalent there is a functionality difference, I don't know if it works the same way.

    In vb it would be:
    VB Code:
    1. If  pItemData -> nSelCheck Then
    2.      iImg = 1
    3. Else
    4.      iImg = 0
    5. End If
    6.  
    7. 'or:
    8. iImg = Iif( pItemData->nSelCheck, 1, 0)
    (nb: I dont know what the -> operator should be in vb! )

    the second one here also takes slightly longer to execute, but it can easily be made more significant for one important reason: In the first method only the required result is calculated, in the second both possible results are calculated, which will take much longer if there is a complex calculation or a function call in either result.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Depending on the compiler and such simple branching, there is usually no difference at all. If the second version might be faster (by using special CPU instructions like the x86's conditional set), a good compiler should realize that the same is applicable to the first code.

    So, it really depends on the compiler. The real advantage of ?: lies in the brevitiy and (sometimes) readability.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Originally posted by packetVB
    I seen the post and was really curious about this and tested it out.

    Seems MSVC generates one less assembly instruction for the first,if branch.
    Whether that translates into faster i guess would depend on the CPU.

    my worthless two cents.


    How do I know how many assembly instruction a statement can generate in MSVC? I'd like to know how, I'm a newbie at MFC so I have yet to learn a lot of its functionalities...

  8. #8

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Originally posted by si_the_geek
    with the VB equivalent there is a functionality difference, I don't know if it works the same way.

    In vb it would be:
    VB Code:
    1. If  pItemData -> nSelCheck Then
    2.      iImg = 1
    3. Else
    4.      iImg = 0
    5. End If
    6.  
    7. 'or:
    8. iImg = Iif( pItemData->nSelCheck, 1, 0)
    (nb: I dont know what the -> operator should be in vb! )

    the second one here also takes slightly longer to execute, but it can easily be made more significant for one important reason: In the first method only the required result is calculated, in the second both possible results are calculated, which will take much longer if there is a complex calculation or a function call in either result.
    So when is it advisable to use Iif? I did not know about this though and also, what is the great thing about IIF, that MS introcuced this statement?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by debbie_82
    How do I know how many assembly instruction a statement can generate in MSVC? I'd like to know how, I'm a newbie at MFC so I have yet to learn a lot of its functionalities...
    You can tell the compiler to write the assembly it generates into a file. This is only interesting in a release build, because the generated assembly differs greatly between a debug and release build.

    And don't confuse MFC with MSVC or C++. MFC is a class library which I think you should avoid, at least until you've properly learned C++.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Originally posted by debbie_82
    So when is it advisable to use Iif? I did not know about this though and also, what is the great thing about IIF, that MS introcuced this statement?
    it's just an inline-if, most languages have it in some form. It allows you to write shorter & more readable code.

    it just happens that in VB it is a function, and VB calculates all parameters for a function before the function call.

    there's no problem if both parameters are fixed values (which is when you should use it).

    it's only when you start complex calculations that there becomes an issue. if you call functions within the parameters then those functions will always be called (which may or may not be what you want!).

  11. #11

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    Originally posted by CornedBee
    You can tell the compiler to write the assembly it generates into a file. This is only interesting in a release build, because the generated assembly differs greatly between a debug and release build.

    And don't confuse MFC with MSVC or C++. MFC is a class library which I think you should avoid, at least until you've properly learned C++.
    Sure boss... sorry about the mix-up... ...


    Is it in the Settings?
    Whatever... Anyway... I don't know how to set it cause there are so many options and I don't understand the options cause my ide is japanese version and I don't understand japanese... ...(still studying it though)... I have no choice cause the programs won't run if it is not japanese...

    One question... does it really matter if one program is written in a japanese version of MSVC and subsequently you can't run that program in an english version of MSVC and vice versa?

    What we tried that didn't work is if the program is written in japanese version of MSVC and the locale settings of the computer is english... The program does not generate the .pcb file or something...weird errors

  12. #12
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    Originally posted by si_the_geek
    it's just an inline-if, most languages have it in some form. It allows you to write shorter & more readable code.

    it just happens that in VB it is a function, and VB calculates all parameters for a function before the function call.

    there's no problem if both parameters are fixed values (which is when you should use it).

    it's only when you start complex calculations that there becomes an issue. if you call functions within the parameters then those functions will always be called (which may or may not be what you want!).
    Can you tell VB to write an ASM file as well ?
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974


    it's not that kind of inline Neo (although I think I heard you can use ASM somehow), it just means just in-the-same-line as the code, so for example you can build a string with multiple possibilities, and not write lots of lines of If's. dodgy example:
    VB Code:
    1. fred = 0
    2. joe = 0
    3. '...
    4. my_string = "fred is " & iif(fred <> joe, "NOT ","") & iif(joe = 0," zero", joe) & "."
    which without the inline-if would be:
    VB Code:
    1. fred = 0
    2. joe = 0
    3. '...
    4. dim JoeString as string
    5. If joe = 0 Then
    6.   JoeString = "zero"
    7. Else
    8.   JoeString = CStr(Joe)
    9. End If
    10. If fred = joe then
    11.   my_string = "fred is " & JoeString & "."
    12. Else
    13.   my_string = "fred is NOT " & JoeString & "."
    14. End If


    Basically IIF is this function:
    VB Code:
    1. Public Function IIF (condition as Boolean, TruePart, FalsePart)
    2.   If condition Then
    3.      IIF = TruePart
    4.   Else
    5.      IIF = FalsePart
    6.   End If
    7. End Function

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't know that much about VB. I can say for sure that you can't tell VB5 to show the ASM, because it never generates any. I don't know if VB6 generates assembly. I'm pretty sure you can tell the VB.Net compiler to write the MSIL assembly to a file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width