Results 1 to 35 of 35

Thread: IF then question

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    IF then question

    Just wondering, how would do multiple ifs work in each other?

    1 If x = 10
    2 While Now = Now
    3 Text1.Text = Text1.Text + 1
    4 If text1.text = 56 then
    5 msgbox "56"
    6 end if
    7 end if
    8 Wend

    Will 1 correspond with 6 or 7? will 4 correspond with 6 or 7?
    this is what confuses me, this is a really crappy example so ignore it.

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    Switch the Wend with #7 and End If with #8

    And if you have learned to indent your code, it wouldn't be so confusing:

    VB Code:
    1. If x = 10
    2.  
    3.      While Now = Now
    4.          
    5.           Text1.Text = Text1.Text + 1
    6.  
    7.           If text1.text = 56 then
    8.  
    9.               MsgBox "56"
    10.          
    11.           End If
    12.  
    13.      Wend
    14.  
    15. End If

    Now you know which one it corresponds with

  3. #3
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: IF then question

    Quote Originally Posted by Jacob Roman
    Switch the Wend with #7 and End If with #8

    And if you have learned to indent your code, it wouldn't be so confusing:

    Now you know which one it corresponds with
    I agree
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    If you learned some C++, or know a little at least, you would have learned a thing called If Blocks. There is no Then and End If in C++ for if statements. Instead they use curly brackets {} to block in the code that's executed when the condition is true

    Code:
    if(x = 10)
    {
    
    //Do stuff here in this block
    
    }
    So think of the code in between If condition is true Then and End If as an if block without the curly brackets {}.

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: IF then question

    but, what happeneds if you need to put 2 ifs inside of another if, how does it know which one to go to..does it work like this:


    | - if
    | | - if
    | | | - if
    | | | | - if
    | | | | - if
    | | | - if
    | |- if
    |- if
    ?

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: IF then question

    VB Code:
    1. If x > 10 Then
    2.     If SomeThingElse = False Then
    3.         If YouNeedMoreHelpJustAsk(Someone) = vbYes Then
    4.             'do something
    5.         End If 'end of If YouNeedMore...
    6.     End If 'end of If SomeThingElse
    7. End If 'end of If x
    Just look at the indentation. The first If ends at the last line.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: IF then question

    You could have IF statements nested like this:
    Change values around to see if you can guess what will print.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim Condition1 As Boolean
    5. Dim Condition2 As Boolean
    6. Dim Condition3 As Boolean
    7. Dim Condition4 As String
    8. Condition1 = True
    9. Condition2 = True
    10. Condition3 = True
    11. If Condition1 = True Then
    12.   If Condition2 = True Then
    13.     If Condition3 = True Then
    14.       If Condition4 = "Black" Then
    15.         MsgBox "1,2,3,Black"
    16.       Else
    17.         MsgBox "1,2,3,Not Black"
    18.       End If
    19.     Else
    20.       MsgBox "1,not2,not 3"
    21.     End If
    22.   Else
    23.     If Condition3 = True Then
    24.       MsgBox "Not 1, not 2, 3"
    25.     End If
    26.   End If
    27. Else
    28.   MsgBox "Not 4"
    29. End If
    30. End Sub

  8. #8
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    Hehe, this is getting interesting :

    VB Code:
    1. Dim Condition(7) As Boolean
    2. Dim I As Long
    3.  
    4. For I = 0 To 7
    5.  
    6.      Condition(I) = True
    7.  
    8. Next I
    9.  
    10. If Condition(0) = True Then
    11.      
    12.      MsgBox "Condition 0 is True", vbExclamation
    13.  
    14.      If Condition(1) = True Then
    15.  
    16.           MsgBox "Condition 1 is True", vbExclamation
    17.  
    18.           If Condition(2) = True Then
    19.  
    20.                MsgBox "Condition 2 is True", vbExclamation
    21.  
    22.                If Condition(3) = True Then
    23.  
    24.                     MsgBox "Condition 3 is True", vbExclamation
    25.  
    26.                     If Condition(4) = True Then
    27.  
    28.                          MsgBox "Condition 4 is True", vbExclamation
    29.  
    30.                          If Condition(5) = True Then
    31.  
    32.                               MsgBox "Condition 5 is True", vbExclamation
    33.  
    34.                               If Condition(6) = True Then
    35.  
    36.                                    MsgBox "Condition 6 is True", vbExclamation
    37.  
    38.                                    If Condition(7) = True Then
    39.  
    40.                                         MsgBox "It's All True!!!",vbExclamation
    41.                                    
    42.                                    End If 'End Condition 7 Block
    43.        
    44.                               End If 'End Condition 6 Block
    45.  
    46.                           End If 'End Condition 5 Block
    47.                        
    48.                     End If 'End Condition 4 Block  
    49.  
    50.                End If 'End Condition 3 Block    
    51.  
    52.           End If 'End Condition 2 Block
    53.  
    54.      End If 'End Condition 1 Block
    55.  
    56. End If 'End Condition 0 Block

  9. #9
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: IF then question

    i guess you can see now with Jacob Roman's example you can tell now which is which.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: IF then question

    okay i thought it worked like that, but i got super confused and was like *** mate

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: IF then question

    I suppose one should use 'end if' in every 'if' for code readability....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    How come no one mentions the ElseIf?
    VB Code:
    1. If Something = True Then
    2.     MsgBox "Blah"
    3. ElseIf SomethingElse = True Then
    4.     MsgBox "Blah, Blah"
    5. ElseIf SomeOtherThing = True Then
    6.     MsgBox "Blah, Blah, Blah"
    7. Else
    8.     MsgBox "?"
    9. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: IF then question

    Quote Originally Posted by dee-u
    I suppose one should use 'end if' in every 'if' for code readability....
    its not for code readability. it is how the language was design there's a term use for that in programming i just can't remember it. Let's just say they're like husband and wife.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  14. #14
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: IF then question

    ...another one i don't see much...

    'IIf(expr, truepart, falsepart)
    VB Code:
    1. MsgBox IIf(1 = 1, "hello", "goodbye")
    2.         MsgBox IIf(1 = 2, "Bull", 5 * 5)

  15. #15
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: IF then question

    Ah I think it is called context free grammar.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: IF then question

    its not for code readability. it is how the language was design there's a term use for that in programming i just can't remember it. Let's just say they're like husband and wife.
    Is that term 'structure'?

    ...another one i don't see much...

    'IIf(expr, truepart, falsepart)
    I just use it when I'm saving data to a database, it helps a lot in that area.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: IF then question

    whats elseif?

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    Its just another option in an If Then Else structure.

    All these statements are called Decision Structures.

    The If Then ElseIf Else End If decision structure is similar to a Select Case decision structure.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    What's ElseIf?!!! Ok I'll show ya an example to clear things up. It's pretty obvious if you use common sense

    VB Code:
    1. Dim Value As Long
    2.  
    3. Value = (Rnd * 2) + 1 'Any random value within 1 - 3
    4.  
    5. If Value = 1 Then
    6.  
    7.      MsgBox "Value is 1", vbExclamation
    8.  
    9. ElseIf Value = 2 Then
    10.  
    11.      MsgBox "Value is 2", vbExclamation    
    12.  
    13. ElseIf Value = 3 Then
    14.  
    15.      MsgBox "Value is 3", vbExclamation
    16.  
    17. End If

    Notice that you didn't need to use End If in there for the ElseIf's. An equivilant to this would be a Select Case statement

    VB Code:
    1. Dim Value As Long
    2.  
    3. Value = (Rnd * 2) + 1 'Any random value within 1 - 3
    4.  
    5. Select Case Value
    6.  
    7.      Case 1
    8.  
    9.           MsgBox "Value is 1", vbExclamation
    10.  
    11.      Case 2
    12.  
    13.           MsgBox "Value is 2", vbExclamation    
    14.  
    15.      Case 3
    16.  
    17.           MsgBox "Value is 3", vbExclamation
    18.  
    19. End Select

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    Quote Originally Posted by RobDog888
    How come no one mentions the ElseIf?
    VB Code:
    1. If Something = True Then
    2.     MsgBox "Blah"
    3. ElseIf SomethingElse = True Then
    4.     MsgBox "Blah, Blah"
    5. ElseIf SomeOtherThing = True Then
    6.     MsgBox "Blah, Blah, Blah"
    7. Else
    8.     MsgBox "?"
    9. End If
    Guess you missed it.

    for completing the example with an Select Case example.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    Hehe, thanks. And believe it or not. If you were to make any games to where speed is a must, this first thing I'm about to show you is slightly faster than the other method, although the other method is good for code readability:

    Slightly Faster method (No "End If" is needed)

    VB Code:
    1. If Condition = True Then MsgBox "Kool"

    The other method

    VB Code:
    1. If Condition = True Then
    2.  
    3.      MsgBox "Kool"
    4.  
    5. End If

    I'm no assembly guru or anything, but something tells me there is less assembly being executed with the single line If Then statement which made it faster.

  22. #22

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: IF then question

    i didnt miss it, i just wanted an explanation for it. And the select case made me understand it..What are the advantages over select case if there are any?

  23. #23
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    The Select Case method is good for readability. I heard that it's slightly slower than the If Then ElseIf statements though.

  24. #24
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: IF then question

    Quote Originally Posted by Jacob Roman
    Slightly Faster method (No "End If" is needed)
    VB Code:
    1. If Condition = True Then MsgBox "Kool"
    The other method
    VB Code:
    1. If Condition = True Then
    2.      MsgBox "Kool"
    3. End If
    Hmmm.... Are you sure about this Jacob? In my mind the exact same native code should be generated when you compile it, otherwise the VB compiler does a pretty poor job with that.

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    Anybody up for putting this to a performance test?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: IF then question

    Quote Originally Posted by RobDog888
    Anybody up for putting this to a performance test?
    OK here a simple test I did:
    VB Code:
    1. Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
    2.  
    3. Private Sub OneLineIf()
    4.     Dim n As Long, x As Long
    5.    
    6.     For n = 0 To 1000000
    7.         If x < 1000000 Then x = x + 1
    8.     Next
    9. End Sub
    10.  
    11. Private Sub BlockIf()
    12.     Dim n As Long, x As Long
    13.    
    14.     For n = 0 To 1000000
    15.         If x < 1000000 Then
    16.             x = x + 1
    17.         End If
    18.     Next
    19. End Sub
    20.  
    21. Private Sub Main()
    22.     Dim t As Long
    23.     Dim nOne As Long, nBlock As Long
    24.    
    25.     t = GetTickCount
    26.     OneLineIf
    27.     nOne = GetTickCount - t
    28.     t = GetTickCount
    29.     BlockIf
    30.     nBlock = GetTickCount - t
    31.     MsgBox "Single line If = " & nOne & vbCrLf & "Block If = " & nBlock
    32. End Sub
    Not the most perfect performance tester in the world, but I though it would be enough for this.

    The result: When running from the IDE the one line If statement was slightly faster but when I run it in compiled form the block was faster some time and the single line others so they tied.

    My conclusion: When compiled to native code the same binary code is made, but there is a slight speed advantage with the single line approach if you compile to P-code.

  27. #27
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    Sure no problem

    OMG. That is weird. After running it a few times in IDE mode, the If Then End If was super slow compared to the single line If statement! But in EXE mode I got the complete opposite! What gives

    VB Code:
    1. 'If Value = 1 Then Value2 = 2
    2.        
    3.  
    4.                 'Results
    5.                 '------------------
    6.                 'IDE - 1015072
    7.                 'EXE - 1092362
    8.            
    9.            
    10.             If Value = 1 Then
    11.            
    12.                  Value2 = 2
    13.                  
    14.             End If
    15.            
    16.                 'Results:
    17.                 '-------------------
    18.                 'IDE - 439249
    19.                 'EXE - 1137829

  28. #28
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: IF then question

    I only got faster code with the block if some of the times.
    Quote Originally Posted by Jacob Roman
    OMG. That is weird. After running it a few times in IDE mode, the If Then End If was super slow compared to the single line If statement! But in EXE mode I got the complete opposite! What gives
    Probably this:

    Quote Originally Posted by Joacim Andersson
    My conclusion: When compiled to native code the same binary code is made, but there is a slight speed advantage with the single line approach if you compile to P-code.

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    JR, did youu use the same code that JA posted for testing?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: IF then question

    I got single line slightly faster about 50% of the time in the IDE over 20 runs.
    When compiled I got 0 to 0 over 20 runs all using JA's code.
    I think my system is too fast to measure the exe test.

    Edit: I adjusted the loops by * 10 and I got measureable exe results. Turns out
    50% of the time they are the same. The rest is 50/50 either way.
    Last edited by RobDog888; Apr 6th, 2005 at 11:27 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  31. #31
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: IF then question

    Quote Originally Posted by RobDog888
    When compiled I got 0 to 0 over 20 runs all using JA's code.
    I think my system is too fast to measure the exe test.
    You have to increase the loop before you compile it. However I think Jacob has a more advanced tester.

  32. #32
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: IF then question

    great learning thread ! ... now this way doesn't the test show how the comparitive statement impacts the process time...more like real life..??

    VB Code:
    1. Sub A2A2()
    2.  
    3.     Dim n As Long
    4.     Dim x As Long
    5.     Dim t1 As Long
    6.     Dim t2 As Long
    7.     Dim t3 As Long
    8.    
    9.     t1 = 0
    10.     t2 = 0
    11.     t3 = 0
    12.  
    13. 'SINGLE
    14.     Sleep 1000
    15.     n = 0
    16.     x = 0
    17. '--------------
    18.     t1 = GetTickCount
    19.     For n = 0 To 1000000
    20.         If x < 1000000 Then x = x + 1
    21.     Next
    22.     t1 = GetTickCount - t1
    23.     MsgBox x
    24. '=====================================
    25.  
    26. 'BLOCK
    27.     Sleep 1000
    28.     n = 0
    29.     x = 0
    30. '-------------
    31.     t2 = GetTickCount
    32.     For n = 0 To 1000000
    33.         If x < 1000000 Then
    34.             x = x + 1
    35.         End If
    36.     Next
    37.     t2 = GetTickCount - t2
    38.     MsgBox x
    39. '=====================================
    40.    
    41. 'SELECT CASE
    42.     Sleep 1000
    43.     n = 0
    44.     x = 0
    45. '-------------
    46.     t3 = GetTickCount
    47.     For n = 0 To 1000000
    48.         Select Case True
    49.             Case x < 1000000
    50.             x = x + 1
    51.         End Select
    52.     Next
    53.     t3 = GetTickCount - t3
    54.     MsgBox x
    55. '=====================================
    56.      
    57.      MsgBox "Single line If = " & t1 & vbCrLf & "Block If = " & t2 & vbCrLf & "Select Case = " & t3
    58.  
    59. End Sub

  33. #33
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: IF then question

    still trying to pick some meat from these bones...

    > i used sleep so each test would begin the same.
    > msgbox x for the result.
    now the timing approach is limited to ms (the gettickcount thing)
    ... im thinking a consecutive repetition occurs faster due to microprocessor caching..??...so break it with sleep & msgbox...
    ...anyway i showed fastest is one liner, select case last...and i also tested IIF ...wicked slow...

  34. #34
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: IF then question

    I heard that the Select Case method is slightly slower than ElseIf. Might as well test it to make sure

    I knew the single line IF statement was faster

    Try it on EXE mode to make sure though.

  35. #35
    Hyperactive Member
    Join Date
    May 2001
    Location
    TZI Transition Date
    Posts
    272

    Re: IF then question

    ...consistantly....

    Single line If = 47
    Block If = 63
    Select Case = 94
    ElseIf = 78
    IIF = 281

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