Results 1 to 7 of 7

Thread: Hey why do you have to put a End If statement in 1 app, but not the other?

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question

    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!


  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    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? >)
    SCUZ

  3. #3
    Lively Member quadoc's Avatar
    Join Date
    Jan 1999
    Location
    Ga, USA
    Posts
    83
    You can also put the following on one line.


    if beginning then text1.text = "HELLO WORLD" else text1.text = "Been there! Done that"

  4. #4

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Talking oh i c!

    THanks for your help!

  5. #5

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  7. #7
    Guest

    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
  •  



Click Here to Expand Forum to Full Width