Recent

Author Topic: how to break out early of a case ?  (Read 55764 times)

wp

  • Hero Member
  • *****
  • Posts: 9736
Re: how to break out early of a case ?
« Reply #15 on: August 15, 2018, 03:21:57 pm »
Why not put the code of that case into a procedure?
Code: Pascal  [Select][+][-]
  1. procedure DoSomething;
  2. begin
  3.   statement1;
  4.   statement2;
  5.   if SomeCondition then exit;
  6.   statement3;
  7.   statement4;
  8. end;
  9.  
  10. begin
  11.   case I of
  12.     somevalue:
  13.       DoSomething;
  14.     ...
  15.     <other cases>
  16.   end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #16 on: August 15, 2018, 03:54:29 pm »
emulate it.

Why not put the code of that case into a procedure?

Absolutely.  Those are both reasonable workarounds.     There is no shortage of ways to implement the logic.  It's just unfortunate that the simplest and cleanest solution, which is to break out of the case, simply isn't available.  Oh well...

Thank you for the suggestions, I appreciate them.

Allowing "break" in a case statement would be a nice addition to the language and, best of all, it isn't hard to implement.  May be a feature the developers might be willing to consider.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

taazz

  • Hero Member
  • *****
  • Posts: 5364
Re: how to break out early of a case ?
« Reply #17 on: August 15, 2018, 04:21:00 pm »
emulate it.

Why not put the code of that case into a procedure?

Absolutely.  Those are both reasonable workarounds.     There is no shortage of ways to implement the logic.  It's just unfortunate that the simplest and cleanest solution, which is to break out of the case, simply isn't available.  Oh well...

Thank you for the suggestions, I appreciate them.

Allowing "break" in a case statement would be a nice addition to the language and, best of all, it isn't hard to implement.  May be a feature the developers might be willing to consider.
No, the choices presented here are as easy and straight forward as a call to break. I see no reason to change break.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #18 on: August 15, 2018, 04:36:12 pm »
No, the choices presented here are as easy and straight forward as a call to break. I see no reason to change break.
Those choices are easy and straightforward but, they break linear flow. Those choices could be used in any loop too.  Following your logic, there is no need for "break" in a loop either.

It's about controlling logical flow.  Not having to nest into ifs or call function/procedures when a linear solution is possible.

At this point, it looks like we should probably agree to disagree.

Bottom line is simple: there is no way in Pascal to break out early of a case statement.  When that is needed, which is quite common, C provides a better, simpler, cleaner solution.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

taazz

  • Hero Member
  • *****
  • Posts: 5364
Re: how to break out early of a case ?
« Reply #19 on: August 15, 2018, 04:41:38 pm »
No, the choices presented here are as easy and straight forward as a call to break. I see no reason to change break.
Those choices are easy and straightforward but, they break linear flow. Those choices could be used in any loop too.  Following your logic, there is no need for "break" in a loop either.
true break is just a familiar construct nothing more.
It's about controlling logical flow.  Not having to nest into ifs or call function/procedures when a linear solution is possible.
there are proper flow control constructs no need to extend a hack to a flow control construct.
At this point, it looks like we should probably agree to disagree.
on all accounts I guess.
Bottom line is simple: there is no way in Pascal to break out early of a case statement.  When that is needed, which is quite common, C provides a better, simpler, cleaner solution.
There are. you simple choose to reject them.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: how to break out early of a case ?
« Reply #20 on: August 15, 2018, 04:47:07 pm »
To add to the list, with a sense of humor. Let us use exceptions:
Code: Pascal  [Select][+][-]
  1. type
  2.   ECaseExit=class(Exception)
  3.   end;
  4. ...
  5.   try case I of
  6.     somevalue :
  7.     begin
  8.       statement ...
  9.       statement...
  10.  
  11.       if someconditionhere then raise ECaseExit.Create('Take me out.');
  12.  
  13.       statement ...
  14.       statement....
  15.     end;
  16.  
  17.     <more cases here>
  18.   end; { case } except On ECaseExit do; end;

I think using break inside a case would break some code that was supposed to exit the loop:
Code: Pascal  [Select][+][-]
  1.   while somecondition do
  2.      case I of
  3.        val1: if shouldLeaveLoop then
  4.          Break
  5.        val2: ....
  6.      end;

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #21 on: August 15, 2018, 04:52:55 pm »
I think using break inside a case would break some code that was supposed to exit the loop:
Code: Pascal  [Select][+][-]
  1.   while somecondition do
  2.      case I of
  3.        val1: if shouldLeaveLoop then
  4.          Break
  5.        val2: ....
  6.      end;
Now, that is a reasonable objection and, your example shows that it is, at least somewhat likely, that it would.   That's unfortunate since it is a good reason not to implement break in case statements.

I got a chuckle out of using an exception to break out of a case. :)  Good one.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: how to break out early of a case ?
« Reply #22 on: August 15, 2018, 09:43:51 pm »
Allowing "break" in a case statement would be a nice addition to the language and, best of all, it isn't hard to implement.  May be a feature the developers might be willing to consider.

What did you think of using Repeat/Until in place of Begin/End, which would facilitate Break?
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Thaddy

  • Hero Member
  • *****
  • Posts: 11623
Re: how to break out early of a case ?
« Reply #23 on: August 15, 2018, 10:00:28 pm »
< >:D >:D> I still think he means the break in the case of a C case. Which I already answered...
Black themes should be banned.

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #24 on: August 15, 2018, 11:57:55 pm »
What did you think of using Repeat/Until in place of Begin/End, which would facilitate Break?
I like it.  It's a nice trick to implement a scope that can be broken out of.   Sort of a begin/end pair that can be broken out of.  Can be very handy,  Of course, it needs to be commented as to its real purpose so the programmer who sees it knows why it's being done.

Used correctly, it can also be used to replace some try/finally pairs resulting in much better assembly code.

Thanks.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

mangakissa

  • Hero Member
  • *****
  • Posts: 1130
Re: how to break out early of a case ?
« Reply #25 on: August 16, 2018, 08:23:01 am »
if shouldLeaveLoop is a condition, why not reverse the code
Code: Pascal  [Select][+][-]
  1. while somecondition do
  2.   if shouldLeaveLoop then
  3.   begin
  4.      case I of
  5.        val1: ....
  6.        val2: ....
  7.      end;
  8.   end;
  9.  
I'm agreed with Thaddy (not always :D). 
You said you don't want to use goto, but break is doing the same.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #26 on: August 16, 2018, 08:32:02 am »
I'm agreed with Thaddy (not always :D). 
You said you don't want to use goto, but break is doing the same.
There is a big difference between break and a goto.  The destination of a break is preset by where it occurs.  A goto can go anywhere.  Break and goto do the same thing only for people who are better at babbling than programming.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: how to break out early of a case ?
« Reply #27 on: August 16, 2018, 02:22:25 pm »
Break and goto do the same thing only for people who are better at babbling than programming.

You're wrong, you know. Break, Continue, Exit, etc. are just specialized Gotos; the fact that their destination is preselected doesn't negate this.

One could almost say that they were created to allow you to sneak a "goto" while avoiding that culpable feeling that real gotos produce  :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 2812
Re: how to break out early of a case ?
« Reply #28 on: August 16, 2018, 03:40:26 pm »
One could almost say that they were created to allow you to sneak a "goto" while avoiding that culpable feeling that real gotos produce  :)
No.  They were created because they solve the inherent problem of the goto statement, which is that the destination is, unlike with break, continue, etc, not predetermined.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 11623
Re: how to break out early of a case ?
« Reply #29 on: August 16, 2018, 04:47:17 pm »
Break and goto do the same thing only for people who are better at babbling than programming.

You're wrong, you know. Break, Continue, Exit, etc. are just specialized Gotos; the fact that their destination is preselected doesn't negate this.

One could almost say that they were created to allow you to sneak a "goto" while avoiding that culpable feeling that real gotos produce  :)
Actually you are a bit incomplete in  your explanation: those goto's are assembler jumps generated by the compiler. As such they have little to do with an actual high level language construct.
But your answer is more correct than that babbler. 8-)
Black themes should be banned.

 

TinyPortal © 2005-2018