To Nest or Not to Nest Else - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: To Nest or Not to Nest Else (/showthread.php?tid=2961) |
To Nest or Not to Nest Else - Dimster - 08-17-2024 Other than code efficiency, I'm wondering why I would nest an Else statement v's using multiple ElseIf statements. You guys nest your Elses? or go with ElseIF's For example" NESTED ELSE If (condition) then .... Else If (condition) then .... Else If (condition) then .... Else If (condition) then End if End if End if UNNESTED ElseIF If (condition) then .... ElseIf (condition) then .... ElseIf (condition) then .... ElseIf (condition) then ... End if Just curious RE: To Nest or Not to Nest Else - SMcNeill - 08-17-2024 In such cases, I try to go with SELECT CASE, nested between IF statements, just for the difference in visibility of flow for me. Too many ELSEIFs, and I get lost trying to track and keep up with them all. Instead, I'll swap to independent IFs and GOTOs, to keep my brain organized better. IF (condition) THEN stuff GOTO done_iffing_around END IF IF (next condition... same IF (same for 4 more ELSEIF type things... same END IF done_iffing_around: RE: To Nest or Not to Nest Else - TerryRitchie - 08-17-2024 I depends on the situation I suppose. Take this rectangular collision code for example: IF R1.right >= R2.left THEN ' check for overlapping coordinates IF R1.left <= R2.right THEN IF R1.bottom >= R2.top THEN IF R1.top <= R2.bottom THEN ' all checks passed, must be a collision RectCollide% = -1 ' return that a collision was detected (TRUE) END IF END IF END IF END IF ELSEIF would not work in this case. Also, at any point one of the IF statements becomes FALSE the remaining IF statements are skipped, speeding up the process. I'm with Steve on this as well. For a situation where many ELSEIFs are required I would rather use SELECT CASE because of its clean style and easy readability. RE: To Nest or Not to Nest Else - PhilOfPerth - 08-17-2024 (08-17-2024, 03:35 PM)SMcNeill Wrote: In such cases, I try to go with SELECT CASE, nested between IF statements, just for the difference in visibility of flow for me. GoTo??? Wash yer mouth out! I've gotten the impression I'm about the only one who uses this outdated command! RE: To Nest or Not to Nest Else - Pete - 08-19-2024 I tend to use SELECT CASE whenever possible so I don't if it up too much. It has that neat SELECT EVERYCASE, too. My next favorite is the ELSEIF, which is useful in some situations. I have done some routines like Terry's example with block nested If/then statements and They are easy enough to read and understand, due to the progressive indenting. Pete RE: To Nest or Not to Nest Else - Dimster - 08-19-2024 I think Terry your nested IF statements would only perform the next test IF the last one was true. Otherwise, as you point out, IF any of the conditions test False no other conditions are tested. I'm thinking if your code did include Else in the nesting then I think all the conditions would be tested...I think... IF R1.right >= R2.left THEN ' check for overlapping coordinates Else IF R1.left <= R2.right THEN Else IF R1.bottom >= R2.top THEN Else IF R1.top <= R2.bottom THEN ' all checks passed, must be a collision RectCollide% = -1 ' return that a collision was detected (TRUE) END IF END IF END IF END IF IF R1.right >= R2.left THEN ' check for overlapping coordinates ElseIF R1.left <= R2.right THEN ElseIF R1.bottom >= R2.top THEN ElseIF R1.top <= R2.bottom THEN ' all checks passed, must be a collision RectCollide% = -1 ' return that a collision was detected (TRUE) END IF Both Else and ElseIF would test each condition, without the Else or ElseIF conditions are tested until a False is incurred....I thinks that's correct. Your code would short circuit the IF run and that could save a lot of time in processing RE: To Nest or Not to Nest Else - SMcNeill - 08-19-2024 This was actually a very interesting exercise in backwards logic. @TerryRitchie @Dimster -- Both of you see if this kind of odd scenario appeals to you: Code: (Select All)
Run that. There's two boxes on the screen and we check mouse collision with each of them. The left box uses Terry's style collision to see if the mouse is inside the box or not. For it to trigger, the mouse must be inside each x/y bound. The right box uses If and ElseIf to exclude the *outsides* of the box, so that the final ELSE has to be the only points left that aren't excluded by our X/Y bounds. Both works, but the second is so foreign to my usual way of thinking that it blows my mind. We're literally "thinking outside the box" here! RE: To Nest or Not to Nest Else - Dimster - 08-19-2024 That's pretty cool Steve. RE: To Nest or Not to Nest Else - Pete - 08-19-2024 Well jus ah dadburn minute there ya galoots... How's about SELECT CASEY? Code: (Select All)
- Sam RE: To Nest or Not to Nest Else - TerryRitchie - 08-19-2024 That second method caused my brain to get stuck in an infinite loop ... or it possibly divided by 0, I can't tell which, LOL Yeah, that's odd looking but works. Way to butcher SELECT CASE Pete! But hey, it works too. |