![]() |
Forum Index : Microcontroller and PC projects : Multi line ELSE IF
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
DigitalDreams Newbie ![]() Joined: 03/05/2025 Location: United KingdomPosts: 21 |
Slightly confusing and adding a further endif on the end makes no difference or error !?.... If ~~~~~~ Then ~~~~~~~~ Elseif ~~~~~~~ Then ~~~~~~~~~~ Endif Elseif ~~~~~~~~ Then ~~~~~~~~~~~ Endif Endif Do I have something wrong here ? |
||||
vhofst![]() Newbie ![]() Joined: 29/01/2024 Location: ChilePosts: 4 |
Hello. It depends on what you want to do in the code. If ~~~~~~ Then ~~~~~~~~ if ~~~~~~~ Then ~~~~~~~~~~ Endif ELSEIF ~~~~~~~~ Then ~~~~~~~~~~ Endif Edited 2025-06-30 07:10 by vhofst |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
If you use the CODE button your indentation is preserved. e.g. If ~~~~~~ Then ~~~~~~~~ Elseif ~~~~~~~ Then ~~~~~~~~~~ Endif Elseif ~~~~~~~~ Then ~~~~~~~~~~~ Endif Endif It's still a screwy piece of code though. :) It could equally be: If ~~~~~~ Then ~~~~~~~~ Elseif ~~~~~~~ Then ~~~~~~~~~~ Endif Elseif ~~~~~~~~ Then ~~~~~~~~~~~ Endif Endif 'this line is superfluous but won't flag an error because the first example will parse it! . Edited 2025-06-30 07:45 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2579 |
The last condition is just ELSE and there is only 1 ENDIF SELECT CASE can do a similar thing. . Edited 2025-06-30 08:07 by phil99 |
||||
DigitalDreams Newbie ![]() Joined: 03/05/2025 Location: United KingdomPosts: 21 |
Thanks, that looks more like it. Can't use select case though as each elseif is followed by a complicated set of comparisons on several variables. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6266 |
The extra endifs will totally screw up the logic. What happens depends on any other if..endif structures. To expand on Phils example TEMP = 22 IF x = 0 THEN ' change to x = 1 and uncomment the extra endif IF TEMP < 0 THEN PRINT "Freezing" ELSEIF TEMP < 20 THEN PRINT "Cold" 'endif ELSEIF TEMP < 35 THEN PRINT "Warm" ELSE PRINT "Hot" ENDIF ENDIF TEMP = 22 IF x = 0 THEN ' change to x = 1 and uncomment the extra endif IF TEMP < 0 THEN PRINT "Freezing" ELSEIF TEMP < 20 THEN PRINT "Cold" ENDIF ELSEIF TEMP < 35 THEN PRINT "Warm" ELSE PRINT "Hot" ENDIF ENDIF x=0 and the inner code runs but it will find the superfluous endif You may not receive any error but the code will not run as expected. Jim VK7JH MMedit |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1332 |
Select Case can evaluate. What can't it handle? |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
If you are doing a lot of complex comparisons then your program is probably overcomplicated. Go back and think it out again. Such constructs are rarely needed min my experience! Also, each comparison slows down your program as the two variables have to be looked up and retrieved in order to compare them. You may be able to simplify it by using a couple of FUNCTION blocks that are called several times to get rid of repeated comparisons. Don't try to escape from an IF tree using ENDIF. That should only appear once, as the last statement. It *might* work but it equally well might not. In any case it will be confusing to both you and the interpreter! SELECT/CASE is often much faster than a comparison tree as non-true values are skipped quickly. If arranged so that the most frequent true values come first then they can be very fast. Remember that the CASE values can be more than simple values, they can be ranges as well so CASE X<43 and CASE X = 22 TO 30 are valid commands. If a piece of code doesn't look "clean" then it probably isn't and you'll probably have a hell of a time trying to figure out what you did if you have to come back to it later for any reason. :) . Edited 2025-06-30 16:47 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1566 |
Have you tested it? It contradicts my experience; I thought so too until recently. Why not do a little comparison test? I agree 100%. ![]() Regards Michael causality ≠ correlation ≠ coincidence |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
I'll test later today if I have time (I have cooker repairs to look at first :( ). I'm certain that IF will beat SELECT on any simple comparisons as it's much more fundamental, however I'm pretty sure that things like window comparisons, which would require multiple, stacked IF statements, are faster in SELECT. Also, be careful when testing comparisons against absolute values vs variables. They are much faster when there is no variable to look up. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
DigitalDreams Newbie ![]() Joined: 03/05/2025 Location: United KingdomPosts: 21 |
Thanks guys, my code looks much tidier now. It's been decades since I used ELSEIF. In my own past testing 'Select Case' was faster but not suitable in my situ. |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
The fastest I've found so far has been SELECT CASE used with TO to select ranges of values. It can be very fast indeed Also CASE <40 is a lot faster than CASE X < 40 as this causes a second variable lookup. Something to watch out for in CASE statements. I know, I've done it. :) I don't know if MMBasic does it, but compilers sometimes optimize SELECT trees, something not always possible with IF statements. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
DigitalDreams Newbie ![]() Joined: 03/05/2025 Location: United KingdomPosts: 21 |
I don't know if MMBasic does it, but compilers sometimes optimize SELECT trees, something not always possible with IF statements. Oh to have MMBasic compile to machine code ! |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1332 |
Oh to have MMBasic compile to machine code ! The single best thing about MMBasic is that it's an interpreter. If we have portions that require a boost, we have C-function option. I have traditionally been a speed freak but in so many cases, the code is only waiting faster. |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
This is very true. It's easy to get hung up on the program execution speed. You don't gain in program development speed when using a compiler. It's much slower, especially if using MMBasic's built-in editor. Program errors, editing cursor is on the line where the error occurred. Nothing else that I know of does that. If you want fast IO operations there's always a PIO to play with. :) Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1566 |
A small select-if benchmark (RP2350A Edition V6.00.02RC14/HDMI): Dim integer a,b,c,MAX_LOOPS=10000 Print "'IF'loop " c=1 Timer =0 Do a=1 b=1 If a<b Then Inc c ElseIf a>b Then Inc c Else Inc c EndIf Loop While c<MAX_LOOPS Print Timer Print "'Select Case' with 'a' variable" c=1 Timer =0 Do a=1 b=1 Select Case a Case a<b Inc c Case a>b Inc c Case Else Inc c End Select Loop While c<MAX_LOOPS Print Timer Print "'Select Case' without 'a' variable" c=1 Timer =0 Do a=1 b=1 Select Case a Case <b Inc c Case >b Inc c Case Else Inc c End Select Loop While c<MAX_LOOPS Print Timer Print "'IF' again" c=1 Timer =0 Do a=1 b=1 If a<b Then Inc c ElseIf a>b Then Inc c Else Inc c EndIf Loop While c<MAX_LOOPS Print Timer Result: 'IF'loop 437.357 'Select Case' with 'a' variable 434.884 'Select Case' without 'a' variable 415.708 'IF' again 437.197 With a larger number of comparisons, if/then is even significantly worse! Generally, I would prefer select/case for clarity. Edited 2025-06-30 23:00 by twofingers causality ≠ correlation ≠ coincidence |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2579 |
If speed is what you are after and most of the ranges have a span that follows some pattern it may be possible to replace SELECT CASE with a lookup table in the form of an array. This:- IF Temp < 0 THEN PRINT “Freezing” ELSE IF Temp < 20 THEN PRINT “Cold” ELSE IF Temp < 25 THEN PRINT “Cool” ELSE IF Temp < 30 THEN PRINT “Mild” ELSE IF Temp < 35 THEN PRINT “warm” ELSE IF Temp < 40 THEN PRINT “Very warm” ELSE IF Temp < 45 THEN PRINT “Hot” ELSE PRINT “Too Hot” ENDIF Becomes this:- Dim Temp, range%, Heat$(7)=("Freezing","Cold","Cool","Mild","warm","Very warm","Hot","To Hot") 'main t=timer For Temp = -5 To 55 range% = Temp \ 5 - 3 If range% > 7 Then range% = 7 If Temp < 20 Then range% = 1 If Temp < 0 Then range% = 0 Print Temp, Heat$(range%) Next Print timer-t;" mS" End 31.294 mS If the Print in the loop is commented out:- 8.824 mS Edited 2025-06-30 23:24 by phil99 |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2523 |
I don't use logic like if x<5 and x>10 then I use if x<5 then if x>10 then in first case it does 2 tests. in the second if first test not true it skips second test |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7820 |
SELECT x CASE 5 TO 10 'do something END SELECT also does a single variable lookup but combines two comparisons. I'm not a great fan of convoluted IF trees, where one IF loop is conditional on another IF loop. There are often much more readable ways to do it. Certainly, indentation is critical when you use lots of IF-THEN-ELSE-ENDIF statements as they quickly get confusing otherwise. Lose that indentation and you may as well give up on ever understanding the program. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1332 |
Loops: It has been a while and I'm not at my desk to verify but I seem to remember finding for-next loops to be faster than do-loops: For i = 1 to [ridiculously big number] if condition = true then i = [ridiculously big number] end if Next Could be wrong. ![]() |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |