|
-
Jun 21st, 2000, 04:24 AM
#1
Thread Starter
Registered User
Hello, i have a question i just was reading threw this book and i tried both codes but it didn't make sense to me why you had to use End if, but in the other Do Until loop, you didn't have too! well here is the code
Code:
'this is a little program to convert F to C
'and he didn't even Dim all the variables, tisk tisk
Prompt = "Enter a Fahrenheit temperature."
Do
FTemp = InputBox(Prompt, "Fahrenheit to Celcius")
If FTemp <> "" Then
CTemp = Int((FTemp - 32) * 5 / 9)
MsgBox (CTemp), , "Celcius Temperature"
End If
Loop While FTemp <> ""
that was the program that needed hte "END IF" but i don't know why, and here is the one that didn't need the "END If"
[/CODE]
Do
InpName = Inputbox("Enter your name or type Done to quit.")
If InpName <> "Done" then Print InpName
Loop Until InpName = "Done"
as you can see, he used the "If then Statement" but he didn't have to use the "END IF" unlike the top code, witch he used it in the same way and had to use "END IF" to make the program work, becuase i did it without the "END IF" and it woudlnt' work! so can you please tlel me why he had to use the "END IF" statement on the top code but not the bottem code? thanks for listening! 
-
Jun 21st, 2000, 04:27 AM
#2
Hyperactive Member
You don't have to use endif when the if statment only takes up one line. If you will notice the one missing the endif is a one line comparison where the other uses several lines as part of the conditional statment. Make sense? >)
-
Jun 21st, 2000, 04:49 AM
#3
Lively Member
You can also put the following on one line.
if beginning then text1.text = "HELLO WORLD" else text1.text = "Been there! Done that"
-
Jun 21st, 2000, 04:50 AM
#4
Thread Starter
Registered User
oh i c!
-
Jun 21st, 2000, 04:57 AM
#5
While it's not recommended, you can also put more than one "line" of code on the same line like
If InpName <> "Done" Then Print InpName: End If
-
Jun 21st, 2000, 05:05 AM
#6
Cory, these guys gave you good advice; in summary, a "single-line" IF statement must not have and End If, while a "multi-line" IF must have one.
One other thing to watch out for: As you may or may not know at this point in your studies, a single-line VB statement may be broken up on multiple lines by using a "line continuation" character: a space followed by an underscore.
For example:
C = A + B
is the same as:
C = _
A + B
So it is possible to have a single-line IF statement that "looks like" a multi-line one:
If Temp <= 32 Then _
Print "It's freezing!"
Technically, the above statement is a single-line IF statement because of the line continuation character, and must not use End If. If you take away the underscore there, then the End If would be required.
"It's cold gin time again ..."
Check out my website here.
-
Jun 21st, 2000, 06:07 AM
#7
Yeap straight to the point but....
You can use the one liner if you only need one line of code to be execute after the test
e.g
If A = B Then C = A Else C = B
If multiple lines then ya need the End If
If A = B Then
C = A
MsgBox "Answer = " & C
Else
C = B
MsgBox "Answer = " & B
End If
Most Basic dialects support this sort of stuff, though there are arguments about not using the first iteraction
Why all the Kiss by-lines, those guys were turkeys.
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
|