|
-
Apr 5th, 2005, 06:38 PM
#1
Thread Starter
Admodistrator
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.
-
Apr 5th, 2005, 06:44 PM
#2
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:
If x = 10
While Now = Now
Text1.Text = Text1.Text + 1
If text1.text = 56 then
MsgBox "56"
End If
Wend
End If
Now you know which one it corresponds with
-
Apr 5th, 2005, 06:57 PM
#3
PowerPoster
Re: IF then question
 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
-
Apr 5th, 2005, 08:02 PM
#4
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 {}.
-
Apr 5th, 2005, 08:19 PM
#5
Thread Starter
Admodistrator
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
?
-
Apr 5th, 2005, 08:25 PM
#6
Re: IF then question
VB Code:
If x > 10 Then
If SomeThingElse = False Then
If YouNeedMoreHelpJustAsk(Someone) = vbYes Then
'do something
End If 'end of If YouNeedMore...
End If 'end of If SomeThingElse
End If 'end of If x
Just look at the indentation. The first If ends at the last line.
-
Apr 5th, 2005, 08:38 PM
#7
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:
Option Explicit
Private Sub Form_Load()
Dim Condition1 As Boolean
Dim Condition2 As Boolean
Dim Condition3 As Boolean
Dim Condition4 As String
Condition1 = True
Condition2 = True
Condition3 = True
If Condition1 = True Then
If Condition2 = True Then
If Condition3 = True Then
If Condition4 = "Black" Then
MsgBox "1,2,3,Black"
Else
MsgBox "1,2,3,Not Black"
End If
Else
MsgBox "1,not2,not 3"
End If
Else
If Condition3 = True Then
MsgBox "Not 1, not 2, 3"
End If
End If
Else
MsgBox "Not 4"
End If
End Sub
-
Apr 5th, 2005, 08:57 PM
#8
Re: IF then question
Hehe, this is getting interesting :
VB Code:
Dim Condition(7) As Boolean
Dim I As Long
For I = 0 To 7
Condition(I) = True
Next I
If Condition(0) = True Then
MsgBox "Condition 0 is True", vbExclamation
If Condition(1) = True Then
MsgBox "Condition 1 is True", vbExclamation
If Condition(2) = True Then
MsgBox "Condition 2 is True", vbExclamation
If Condition(3) = True Then
MsgBox "Condition 3 is True", vbExclamation
If Condition(4) = True Then
MsgBox "Condition 4 is True", vbExclamation
If Condition(5) = True Then
MsgBox "Condition 5 is True", vbExclamation
If Condition(6) = True Then
MsgBox "Condition 6 is True", vbExclamation
If Condition(7) = True Then
MsgBox "It's All True!!!",vbExclamation
End If 'End Condition 7 Block
End If 'End Condition 6 Block
End If 'End Condition 5 Block
End If 'End Condition 4 Block
End If 'End Condition 3 Block
End If 'End Condition 2 Block
End If 'End Condition 1 Block
End If 'End Condition 0 Block
Last edited by Jacob Roman; Apr 5th, 2005 at 09:02 PM.
-
Apr 5th, 2005, 09:26 PM
#9
PowerPoster
Re: IF then question
i guess you can see now with Jacob Roman's example you can tell now which is which.
-
Apr 5th, 2005, 09:43 PM
#10
Thread Starter
Admodistrator
Re: IF then question
okay i thought it worked like that, but i got super confused and was like *** mate
-
Apr 5th, 2005, 11:54 PM
#11
Re: IF then question
I suppose one should use 'end if' in every 'if' for code readability....
-
Apr 5th, 2005, 11:58 PM
#12
Re: IF then question
How come no one mentions the ElseIf?
VB Code:
If Something = True Then
MsgBox "Blah"
ElseIf SomethingElse = True Then
MsgBox "Blah, Blah"
ElseIf SomeOtherThing = True Then
MsgBox "Blah, Blah, Blah"
Else
MsgBox "?"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 12:45 AM
#13
PowerPoster
Re: IF then question
 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.
-
Apr 6th, 2005, 01:00 AM
#14
Hyperactive Member
Re: IF then question
...another one i don't see much...
'IIf(expr, truepart, falsepart)
VB Code:
MsgBox IIf(1 = 1, "hello", "goodbye")
MsgBox IIf(1 = 2, "Bull", 5 * 5)
-
Apr 6th, 2005, 01:09 AM
#15
PowerPoster
Re: IF then question
Ah I think it is called context free grammar.
-
Apr 6th, 2005, 08:55 AM
#16
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.
-
Apr 6th, 2005, 09:38 PM
#17
Thread Starter
Admodistrator
-
Apr 6th, 2005, 09:47 PM
#18
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 09:49 PM
#19
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:
Dim Value As Long
Value = (Rnd * 2) + 1 'Any random value within 1 - 3
If Value = 1 Then
MsgBox "Value is 1", vbExclamation
ElseIf Value = 2 Then
MsgBox "Value is 2", vbExclamation
ElseIf Value = 3 Then
MsgBox "Value is 3", vbExclamation
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:
Dim Value As Long
Value = (Rnd * 2) + 1 'Any random value within 1 - 3
Select Case Value
Case 1
MsgBox "Value is 1", vbExclamation
Case 2
MsgBox "Value is 2", vbExclamation
Case 3
MsgBox "Value is 3", vbExclamation
End Select
-
Apr 6th, 2005, 09:57 PM
#20
Re: IF then question
 Originally Posted by RobDog888
How come no one mentions the ElseIf?
VB Code:
If Something = True Then
MsgBox "Blah"
ElseIf SomethingElse = True Then
MsgBox "Blah, Blah"
ElseIf SomeOtherThing = True Then
MsgBox "Blah, Blah, Blah"
Else
MsgBox "?"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 10:15 PM
#21
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:
If Condition = True Then MsgBox "Kool"
The other method
VB Code:
If Condition = True Then
MsgBox "Kool"
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.
Last edited by Jacob Roman; Apr 6th, 2005 at 10:19 PM.
-
Apr 6th, 2005, 10:17 PM
#22
Thread Starter
Admodistrator
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?
-
Apr 6th, 2005, 10:18 PM
#23
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.
-
Apr 6th, 2005, 10:22 PM
#24
Re: IF then question
 Originally Posted by Jacob Roman
Slightly Faster method (No "End If" is needed)
VB Code:
If Condition = True Then MsgBox "Kool"
The other method
VB Code:
If Condition = True Then
MsgBox "Kool"
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.
-
Apr 6th, 2005, 10:25 PM
#25
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 10:40 PM
#26
Re: IF then question
 Originally Posted by RobDog888
Anybody up for putting this to a performance test? 
OK here a simple test I did:
VB Code:
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Private Sub OneLineIf()
Dim n As Long, x As Long
For n = 0 To 1000000
If x < 1000000 Then x = x + 1
Next
End Sub
Private Sub BlockIf()
Dim n As Long, x As Long
For n = 0 To 1000000
If x < 1000000 Then
x = x + 1
End If
Next
End Sub
Private Sub Main()
Dim t As Long
Dim nOne As Long, nBlock As Long
t = GetTickCount
OneLineIf
nOne = GetTickCount - t
t = GetTickCount
BlockIf
nBlock = GetTickCount - t
MsgBox "Single line If = " & nOne & vbCrLf & "Block If = " & nBlock
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.
-
Apr 6th, 2005, 10:42 PM
#27
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:
'If Value = 1 Then Value2 = 2
'Results
'------------------
'IDE - 1015072
'EXE - 1092362
If Value = 1 Then
Value2 = 2
End If
'Results:
'-------------------
'IDE - 439249
'EXE - 1137829
-
Apr 6th, 2005, 10:50 PM
#28
Re: IF then question
I only got faster code with the block if some of the times.
 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:
 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.
-
Apr 6th, 2005, 11:19 PM
#29
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 11:24 PM
#30
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2005, 11:29 PM
#31
Re: IF then question
 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.
-
Apr 7th, 2005, 12:27 AM
#32
Hyperactive Member
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:
Sub A2A2()
Dim n As Long
Dim x As Long
Dim t1 As Long
Dim t2 As Long
Dim t3 As Long
t1 = 0
t2 = 0
t3 = 0
'SINGLE
Sleep 1000
n = 0
x = 0
'--------------
t1 = GetTickCount
For n = 0 To 1000000
If x < 1000000 Then x = x + 1
Next
t1 = GetTickCount - t1
MsgBox x
'=====================================
'BLOCK
Sleep 1000
n = 0
x = 0
'-------------
t2 = GetTickCount
For n = 0 To 1000000
If x < 1000000 Then
x = x + 1
End If
Next
t2 = GetTickCount - t2
MsgBox x
'=====================================
'SELECT CASE
Sleep 1000
n = 0
x = 0
'-------------
t3 = GetTickCount
For n = 0 To 1000000
Select Case True
Case x < 1000000
x = x + 1
End Select
Next
t3 = GetTickCount - t3
MsgBox x
'=====================================
MsgBox "Single line If = " & t1 & vbCrLf & "Block If = " & t2 & vbCrLf & "Select Case = " & t3
End Sub
-
Apr 7th, 2005, 09:40 PM
#33
Hyperactive Member
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...
-
Apr 7th, 2005, 09:48 PM
#34
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.
-
Apr 7th, 2005, 10:27 PM
#35
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|