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) |
RE: To Nest or Not to Nest Else - Pete - 08-19-2024 Hey @TerryRitchie Have you seen the Baker and the Candlestick Maker? We have a tee time in an hour! Pete RE: To Nest or Not to Nest Else - TerryRitchie - 08-19-2024 (08-19-2024, 10:10 PM)Pete Wrote: Hey @TerryRitchie Have you seen the Baker and the Candlestick Maker? We have a tee time in an hour!Last I saw them they were with Tom the piper's son ... something about having pork for dinner. RE: To Nest or Not to Nest Else - SMcNeill - 08-19-2024 (08-19-2024, 10:05 PM)TerryRitchie Wrote: 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 That's almost exactly what my brain did! After thinking about it a bit, it makes sense though. It's just not intuitive to me. If x < 300 Then 'it's left of the box ElseIf x > 400 Then 'it's right of the box Elseif y < 100 Then 'it's above the box ElseIf y > 200 Then 'it's below the box Else 'it's not left nor right, nor above nor below the box... it has to be IN the box! It works, and I might use it for some code obfuscation challenge, but it hurts my brain to think backwards like that. RE: To Nest or Not to Nest Else - TerryRitchie - 08-19-2024 (08-19-2024, 10:19 PM)SMcNeill Wrote:The only issue I have with the code is that the IF and all ELSEIF statements must be processed. The method I've always used allows for dropping out of the statements when any of them go FALSE.(08-19-2024, 10:05 PM)TerryRitchie Wrote: 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 Imagine feeding this kind of backwards code thinking to LLMs. Let what appears to be hallucinations along with real hallucinations begin! RE: To Nest or Not to Nest Else - SMcNeill - 08-19-2024 Actually, this exclusionary thinking may be quite useful to consider. Think of this scenario: A 20 pointed star spaceship is battling aliens in a space invaders style game. Pew! Pew! Bullets and aliens everywhere! Checking every possible point of impact with those 20 star points is going to be processing heavy... So why not create an EXCLUSION check first? If the object isn't within this simple circle distance of the center of my Starship, then skip it. Now, you're not checking everything against every point of the ship. You're only checking things that are outside that exclusion zone. That should run and process a TON smoother! (08-19-2024, 10:24 PM)TerryRitchie Wrote:(08-19-2024, 10:19 PM)SMcNeill Wrote:The only issue I have with the code is that the IF and all ELSEIF statements must be processed. The method I've always used allows for dropping out of the statements when any of them go FALSE.(08-19-2024, 10:05 PM)TerryRitchie Wrote: 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 Nope. Think about it a little deeper. IF x < 300 THEN 'Do nothing, but we could if we wanted too... ELSEIF x > 400 THEN <-- we'll skip this and all other ELSEIF and ELSE statements, if the first condition was true. RE: To Nest or Not to Nest Else - TerryRitchie - 08-19-2024 (08-19-2024, 10:30 PM)SMcNeill Wrote: Nope. Think about it a little deeper.Doh! ... brain .. hurts ... ow RE: To Nest or Not to Nest Else - Pete - 08-19-2024 I've actually made an exclusion routine I liked better because it was easier to code. Of course maybe it was just because the inclusion code was pissing me off at the the time. Sorry, this was several years ago, and I can't recall the project off hand. Anyway just another thing I love about BASIC and coding in general, a lot of different recipes that end up baking the same cake. Hmmm, now if I can fit a candlestick maker into the next discussion, I'll have hit the trifecta! Pete RE: To Nest or Not to Nest Else - SMcNeill - 08-19-2024 (08-19-2024, 10:35 PM)TerryRitchie Wrote:(08-19-2024, 10:30 PM)SMcNeill Wrote: Nope. Think about it a little deeper.Doh! ... brain .. hurts ... ow See if this little snippet doesn't help clear up any confusion of how it actually works. Code: (Select All)
As you can see, running this, it only prints one single condition -- the first one it comes to. Doesn't matter if you're both above and left of the box. It'll print the first exclusion and then skip the rest. It's still a routine which only processes until it finds a condition which isn't true any more. It's just... backwards from how most folks would normally do it. RE: To Nest or Not to Nest Else - luke - 08-20-2024 Y'all are overcooking it, you don't need any conditionals: Code: (Select All) RectCollide% = R1.right >= R2.left _andalso _ RE: To Nest or Not to Nest Else - OldMoses - 08-20-2024 I was not aware of _ANDALSO, so there's a new toy to play with. Thanks for that heads up Luke. I'll just leave this out here as the way I typically check for a value falling in a rectangular range. Code: (Select All) SCREEN _NEWIMAGE(640, 480, 32) In fact, I also combine two InRange% calls into a second function called InRegion% then only call that with all parameters in a single call. I find it highly readable that way. Only both InRange% results of TRUE will result in a TRUE InRegion% result. FUNCTION InRegion% (xpos%, ypos%, xl%, xh%, yl%, yh%) InRegion% = -(InRange%(xpos%, xl%, xh%) * InRange%(ypos%, yl%, yh%)) END FUNCTION To reduce parameter count, I frequently set up click fields with a UDT array that contains upper left and lower right x/y data for all screen click points, then just ship off the UDT in an array loop to InRegion%, which then passes the appropriate data to the two InRange% calls. Once set up, click field recognition is a snap. |