DAY 041: ELSEIF - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Official Links (https://staging.qb64phoenix.com/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://staging.qb64phoenix.com/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://staging.qb64phoenix.com/forumdisplay.php?fid=49) +---- Thread: DAY 041: ELSEIF (/showthread.php?tid=1313) |
DAY 041: ELSEIF - Pete - 12-21-2022 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 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. Pete RE: DAY 041: ELSEIF - mnrvovrfc - 12-21-2022 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". RE: DAY 041: ELSEIF - SMcNeill - 12-22-2022 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 Notice the number of END IFs needed in both versions. They're definitely not the same command! RE: DAY 041: ELSEIF - Pete - 12-22-2022 Oh god, I wish you hadn't posted that. Now Vince will be along, any minute, to sing Santa Stevie. Pete |