Author Topic: Feature request - Exit Case/Break  (Read 8651 times)

0 Members and 1 Guest are viewing this topic.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Feature request - Exit Case/Break
« on: September 06, 2020, 11:29:38 am »
Hi Folks,

I cant seem to find a command to exit a select case structure in QB64, i know in C it's break...if one does exist then please tell me what it is, if not then can a command be added?

Thanks

Unseen

Code: QB64: [Select]
  1.  
  2.   CASE 1
  3.        
  4.         '// If something happend that means  dont want to run the rest of this case then _BREAK
  5.  

FellippeHeitor

  • Guest
Re: Feature request - Exit Case/Break
« Reply #1 on: September 06, 2020, 11:31:36 am »
Quick and dirty GOTO.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #2 on: September 06, 2020, 12:38:41 pm »
How's this ?

Code: QB64: [Select]
  1.  
  2.   CASE 1
  3.  
  4.         IF NOT "something happend" THEN
  5.  
  6.             '// that means  I don't want to run this
  7.  
  8.         END IF
  9.  
  10.  

clean, readable and easy...
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

FellippeHeitor

  • Guest
Re: Feature request - Exit Case/Break
« Reply #3 on: September 06, 2020, 12:47:14 pm »
_CONTINUE is a goto replacement for loops, I don't oppose a _BREAK be added to the language for SELECT CASE blocks, it could serve as a general alias for EXIT DO/WHILE/FOR as well. Let's see how discussion in this thread progresses.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #4 on: September 06, 2020, 01:06:30 pm »
Sounds good Fellippe, no objections from my side either.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #5 on: September 06, 2020, 02:29:34 pm »
Hi Unseen

I find singular your request because I think to the great difference between SELECT CASE END SELECT of Qbasic and SWITCH of C/C++ ...
while in Qbasic multiple cases are of different data type and can be grouped in many ways
Code: QB64: [Select]
  1. DIM Scelta, Intero
  2.  
  3.     CLS
  4.     INPUT "EXIT? Please make your choice Yes/No/Don't know ", Scelta
  5.     Scelta = UCASE$(LEFT$(Scelta, 1))
  6.  
  7.     SELECT CASE Scelta
  8.         CASE "N", "D"
  9.             PRINT "You're  still here"
  10.         CASE "Y"
  11.             PRINT "You're gone"
  12.         CASE ELSE
  13.             PRINT "Error of Input"
  14.     END SELECT
  15.     SLEEP 1
  16. LOOP UNTIL Scelta = "Y"
  17.  
  18.     CLS
  19.     INPUT "EXIT? Please make your choice 1.Yes/2.No/3.Don't know ", Intero
  20.  
  21.     SELECT CASE Intero
  22.         CASE 3, 2
  23.             PRINT "You're  still here"
  24.         CASE 1
  25.             PRINT "You're gone"
  26.         CASE ELSE
  27.             PRINT "Error of Input"
  28.     END SELECT
  29.     SLEEP 1
  30. LOOP UNTIL Intero = 1
  31.  
No fallthrough rule, so why do you need of _BREAK/ EXIT SELECT?
Moreover No DEFAULT case but there is an ELSE case... so you must invert the logic of C code to translate it into Qbasic!
In C/C++ you must use
- an integer or char or enumerable variable and
- you cannot use variable for CASE values
- you cannot group many value on the same line of code
- you cannot make an evaluation on each CASE

so I am very interesting to understand in what CASE a _BREAK can be useful
I want be clear, I am not against this, but curious about its

@FellippeHeitor
WoW
Quote
_CONTINUE is a goto replacement for loops

http://qb64.org/wiki/CONTINUE
I have missed this!

Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #6 on: September 06, 2020, 04:37:54 pm »
Quote
Quick and dirty GOTO.
Lol, Seriously, i never knew!!! (Hmmmmm....)(sarcasm Felippe?)
Quote
I don't oppose a _BREAK be added to the language for SELECT CASE blocks

Thats what i wanted but
Quote
it could serve as a general alias for EXIT DO/WHILE/FOR as well.
Is an even better implementation so i'd support that!
Quote
so I am very interesting to understand in what CASE a _BREAK can be useful
I want be clear, I am not against this, but curious about its
I cant remember why i wanted it but it came up when i was coding something recently so as i couldnt find a built in command i found it resonable to request it, i suppose it's just down to individual coding techniques (the way we each like to code)rather than an absolutley needed command.

Unseen

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #7 on: September 06, 2020, 05:04:14 pm »
Why would you need to again? 
SELECT CASE is little more than a suped up IF\THEN.
There should be no need to 'break' out of a SELECT CASE. As it is only going to run a matching case anyway.
Granted after becoming radioactive I only have a half-life!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Feature request - Exit Case/Break
« Reply #8 on: September 06, 2020, 05:05:11 pm »
Honestly, I'm not so certain I'd like to see such a command added to the language.  All a SELECT CASE is, is a series of IF.. THEN statements, which use the same base condition to check against.  Heck, when translated into the c-code, which is compiled, they're translated into IF THEN ELSE type statements.

Let's take a quick look at some QB64 code, and its translated output:

Code: QB64: [Select]
  1.     CASE "A"
  2.     CASE "B"
  3.     CASE "C"
  4.  

Code: C: [Select]
  1. if ((qbs_cleanup(qbs_tmp_base,qbs_equal(__STRING_X,qbs_new_txt_len("A",1))))||new_error){
  2. goto sc_1_end;
  3. }
  4. if ((qbs_cleanup(qbs_tmp_base,qbs_equal(__STRING_X,qbs_new_txt_len("B",1))))||new_error){
  5. goto sc_1_end;
  6. }
  7. if ((qbs_cleanup(qbs_tmp_base,qbs_equal(__STRING_X,qbs_new_txt_len("C",1))))||new_error){
  8. goto sc_1_end;
  9. }
  10. sc_1_end:;


As you can see, all those SELECT CASE statements are translated into nothing more than a series of IF statements, with a GOTO making them skip all the irrelevant sections.

Adding a _BREAK command into the code would be as simple as translating that break into another one of those GOTO statements.  (goto sc_1_end, in this case)

My issue is, if we start making commands dedicated to this type of purpose, how many other commands are we going to need to add the same functionality in other functions?

IF x = 3 then
   'do some stuff
   'if something triggers then _EXIT IF??

There's a reason why the language has GOTO in it.  Sometimes, it's simply the tool we need to make use of, without having to have customized keywords which do nothing more than replicate its functionality.



One problem I foresee, if EXIT _CASE is implemented, is how it might interact with SELECT EVERYCASE.

SELECT EVERYCASE basically translates the code the same as above, but without those GOTO statements between each IF condition.

If we have code like the following, how is it supposed to act?

Code: [Select]
SELECT EVERYCASE x$
    CASE "A"
       z = z + 1
       IF z = 3 then EXIT SELECT
       y = y + 2
    CASE "B"
    CASE "C"
END SELECT

Now, is that EXIT SELECT supposed to exit all the way to the END SELECT?  If we goto our existing label, that's where it'd jump to.

OR is it supposed to exit down to the CASE "B" statement?  If so, then all SELECT CASE statements will need to have labels added to each of them, so we can jump to the proper one.

Both options would be simple enough to implement, if folks really want to add them (personally, I don't think they're necessary), but I'd suggest if added, and if it's supposed to work as the second option (jump to the next CASE), then why not make two commands?  EXIT SELECT which jumps all the way to the end of the SELECT CASE, and an EXIT CASE, which jumps down to the next CASE statement instead.

Another thing to consider, if we're going to be jumping between CASES...  How would that work with a CASE ELSE?

Implementing any changes might not be quite as simple as it first seems, as there's a lot of little things to keep in mind and account for, with the command, to keep the program/logic flow proper.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #9 on: September 06, 2020, 05:15:53 pm »
_CONTINUE is a goto replacement for loops, I don't oppose a _BREAK be added to the language for SELECT CASE blocks, it could serve as a general alias for EXIT DO/WHILE/FOR as well. Let's see how discussion in this thread progresses.

This kind of request begs for creating any number of unintended bugs. I'm kind of with Steve on this. Why not just EXIT (whatever)?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #10 on: September 06, 2020, 07:24:51 pm »
Why would you need to again? 
SELECT CASE is little more than a suped up IF\THEN.
There should be no need to 'break' out of a SELECT CASE. As it is only going to run a matching case anyway.

In case (sorry), one case is really long but there is a condition in which the long part of the case can be skipped.

To me, RhoSigma has the best workaround without having to resort to GOTO, though sometimes coding the NOT condition can be tricky.

But also I kind of like BREAK if it can also serve as an EXIT.

Ah but Steve has a point BREAK out of the Case ( in SELECT CASE that means out of SELECT also) or Break out of the whole EVERYCASE Block?  Yeah, GOTO is nice final resort :)

 
« Last Edit: September 06, 2020, 07:29:36 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Feature request - Exit Case/Break
« Reply #11 on: September 06, 2020, 08:50:43 pm »
In case (sorry), one case is really long but there is a condition in which the long part of the case can be skipped.


does that not denote an IF\THEN\ELSE situation within the SELECT CASE?

Still not seeing a need to do this. maybe its just the drugs but I can't wrap my head around why this would be needed out side of poor coding technique?
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: Feature request - Exit Case/Break
« Reply #12 on: September 06, 2020, 08:51:23 pm »
In any case, Unseen's request would look logical like this:
  [ You are not allowed to view this attachment ]  

_BREAK being a way to exit the SELECT block entirely.

FellippeHeitor

  • Guest
Re: Feature request - Exit Case/Break
« Reply #13 on: September 06, 2020, 08:55:37 pm »
Also, in any case it's a glorified goto, but so are all EXIT variations and _CONTINUE.

FellippeHeitor

  • Guest
Re: Feature request - Exit Case/Break
« Reply #14 on: September 06, 2020, 09:00:39 pm »
One more detail: EXIT SELECT is a thing in VB:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/exit-statement

We probably don't really need another keyword, just a new implementation of existing keywords.

It's probably clear I'm for it now.