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


Messages In This Thread
DAY 041: ELSEIF - by Pete - 12-21-2022, 02:29 AM
RE: DAY 041: ELSEIF - by mnrvovrfc - 12-21-2022, 04:08 PM
RE: DAY 041: ELSEIF - by SMcNeill - 12-22-2022, 01:55 AM
RE: DAY 041: ELSEIF - by Pete - 12-22-2022, 02:05 AM



Users browsing this thread: 2 Guest(s)