Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 041: ELSEIF
#1
Sounds like a keyword my av-ee-tar would like. ELSEIF ya don't, I'm eh gonna blast youse.

Okay, settle down Yosemite, and let's have a look at this handy conditional statement.


SYNTAX:

IF foo1 THEN
ELSEIF foo2 THEN
END IF

Usage: Handles multiple conditions while excluding program flow through unnecessary evaluation statements.

This is a nice alternative to SELECT CASE, and better than forcing your program through multiple IF/THEN statements, which could cause more than one desired condition to be triggered.

Example:

Code: (Select All)
a = 1: b = 2
IF a > b OR j = 0 THEN
    PRINT "1"
ELSEIF a = b OR j = 0 THEN PRINT "2"
ELSEIF a < b OR j = 0 THEN PRINT "3"
END IF

PRINT "---------------------------------"

IF a > b OR j = 0 THEN PRINT "1"
IF a = b OR j = 0 THEN PRINT "2"
IF a < b OR j = 0 THEN PRINT "3"

So by using IF THEN with ELSEIF in a block statement we get an output of '1' and done in the first condition block vs all three numbers printed out in the second IF/THEN only block.

--------------------------------------------------------------------------------------

Edit: Oh, here's a fun coding fact, inspired by mn's post below....

We can code a regular IF/THEN non-block statement with THEN (line number)...
...but with an IF/THEN/ESLEIF, the THEN part cannot reference a line number without using GOTO with it.:

Code: (Select All)
IF a = b THEN 5 ' This is accepted.
PRINT "Skip me!"
5 PRINT "Okay, I skipped you!"

IF a = b THEN
    PRINT "Okay!"
ELSEIF a < b THEN GOTO 5 ' You have to include GOTO here or it won't compile.
END IF

Pete
Reply
#2
BASIC programmers are spoiled for choice!

Try programming in Lua where people have been groveling for years for something that looks like "switch()" in C. There are many user implementations. There is a "select()" function in Lua but quirky to use. It's just better to code with "if... then... elseif... else... end".

The "ELSEIF" is definitely a phenomenon of BASIC which is compiled rather than interpreted (ie. with line numbers).

LOL while line numbers were required in a program, "we" just used "GOTO". It's because some 8-bit BASIC's didn't even have "ELSE", let alone "ELSEIF".
Reply
#3
One thing of note here:  ELSEIF is not the same as ELSE IF.  Some folks think they're the same thing, just formatted slightly different for readability.  They're not!

ELSEIF is a continuation of an existing IF statement, offering a different option to the previous choices.  ELSE IF is a completely new and unrelated IF statement, with no regard to the first beyond the fact that it occurs IF they previous conditions don't occur.

A segment of code will probably highlight the difference between the two better than I could explain:

Code: (Select All)
If a Then
    Print a
ElseIf b Then
    Print b
ElseIf c Then
    Print c
Else
    Print d
End If

If a Then
    Print a
Else If b Then
        Print b
    Else If c Then
            Print c
        Else
            Print d
        End If
    End If
End If


Notice the number of END IFs needed in both versions.  They're definitely not the same command!
Reply
#4
Oh god, I wish you hadn't posted that. Now Vince will be along, any minute, to sing Santa Stevie.

Pete Big Grin
If eggs are brain food, Biden has his scrambled.

Reply




Users browsing this thread: 1 Guest(s)