Recent

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

440bx

  • Hero Member
  • *****
  • Posts: 2847
how to break out early of a case ?
« on: August 15, 2018, 06:20:25 am »
Hello,

I've probably been using C too long.  In C, I can break out early in a case statement.  it is often convenient.

I thought this was possible in Pascal too but, FPC is not happy when it finds a "break" in a case. 

Question is: how do I break out early of a case ?

example:
Code: Pascal  [Select][+][-]
  1. case I of
  2.   somevalue :
  3.   begin
  4.     statement ...
  5.     statement...
  6.  
  7.     if someconditionhere then break;  //  FPC is not happy with this
  8.  
  9.     statement ...
  10.     statement....
  11.   end;
  12.  
  13.   <more cases here>
  14. end; { case }
  15.  
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

guest48180

  • Guest
Re: how to break out early of a case ?
« Reply #1 on: August 15, 2018, 06:35:37 am »
if (RadioButton.Checked) then
    Exit;

440bx

  • Hero Member
  • *****
  • Posts: 2847
Re: how to break out early of a case ?
« Reply #2 on: August 15, 2018, 07:00:23 am »
if (RadioButton.Checked) then
    Exit;

Exit would break out of the enclosing function/procedure, not just the case statement. 
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: how to break out early of a case ?
« Reply #3 on: August 15, 2018, 08:12:27 am »
There is no need for breaking from case of clause in Pascal. If one case statement is true, then rest aren't executed like they are in C/C++.

Thaddy

  • Hero Member
  • *****
  • Posts: 11633
Re: how to break out early of a case ?
« Reply #4 on: August 15, 2018, 08:52:03 am »
There's a big difference with a case in Pascal and a case in C:
C behaves like a fall-through (evaluates everything else too, unless break) where Pascal exits when a condition is satisfied.
Hence in C you need to break, whereas in Pascal, you don't need break,
This can be very confusing.
With a fall-through I mean that C evaluates the next cases too, unless break is called.
A pseudo code example would look like this:
Code: Pascal  [Select][+][-]
  1.  case avalue of
  2.  0..9:executeAvalue; // if case 0 to nine is satisfied... Execute something...
  3.  3: Execute third value; // will be executed in C, provided break is [b]not[/b] called,  but not in Pascal. This will never execute in Pascal.
  4. end;

See the point? There is not a one to one relationship between a C (or C++) case and a Pascal case.
In the above case (no break in C) we would solve that with if then without else..
« Last Edit: August 15, 2018, 08:55:15 am by Thaddy »
Black themes should be banned.

Zoran

  • Hero Member
  • *****
  • Posts: 1730
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how to break out early of a case ?
« Reply #5 on: August 15, 2018, 12:09:32 pm »
Invert the condition and use one more begin-end pair:

Code: Pascal  [Select][+][-]
  1. case I of
  2.   somevalue :
  3.   begin
  4.     statement ...
  5.     statement...
  6.  
  7.     if not someconditionhere then begin // the opossite condition
  8.  
  9.       statement ...
  10.       statement....
  11.     end;
  12.   end;
  13.  
  14.   <more cases here>
  15. end; { case }
  16.  

Or you can put a label under end {case} and use goto.
« Last Edit: August 15, 2018, 12:12:13 pm by Zoran »

Zoran

  • Hero Member
  • *****
  • Posts: 1730
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how to break out early of a case ?
« Reply #6 on: August 15, 2018, 12:20:18 pm »
You can also use "repeat - until True" instead of "begin - end":

Code: Pascal  [Select][+][-]
  1. case I of
  2.   somevalue :
  3.   repeat //begin
  4.     statement ...
  5.     statement...
  6.  
  7.     if someconditionhere then break;  //Now, FPC IS happy with this
  8.  
  9.     statement ...
  10.     statement....
  11.   until True; // end;
  12.  
  13.   <more cases here>
  14. end; { case }
  15.  

"repeat - until True" makes a block which executes only once (so, just like simple "begin - end", only now you can use Break.
Yes, a hack. :)

440bx

  • Hero Member
  • *****
  • Posts: 2847
Re: how to break out early of a case ?
« Reply #7 on: August 15, 2018, 01:00:16 pm »
Invert the condition and use one more begin-end pair:

Code: Pascal  [Select][+][-]
  1. case I of
  2.   somevalue :
  3.   begin
  4.     statement ...
  5.     statement...
  6.  
  7.     if not someconditionhere then begin // the opossite condition
  8.  
  9.       statement ...
  10.       statement....
  11.     end;
  12.   end;
  13.  
  14.   <more cases here>
  15. end; { case }
  16.  

Or you can put a label under end {case} and use goto.

I ended up using an if statement as the one you suggested  (like most everyone, I'm not fond of goto.)

It's unfortunate there is no way of exiting a case block early.  An if statement solves the problem but, if there are multiple conditions which would cause the case to break early then that would result in the nesting of multiple if statements to implement what is really very simple, linear not hierarchical, logic.

Allowing break in case statements would be a nice and simple addition to the language which would allow the implementation of simpler and cleaner logic in  some cases.

Zoran, thank you for your help.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: how to break out early of a case ?
« Reply #8 on: August 15, 2018, 01:08:58 pm »
Again, there is no need for break in Pascal case of clause statements. If one statement is true, the rest are ignored.

440bx

  • Hero Member
  • *****
  • Posts: 2847
Re: how to break out early of a case ?
« Reply #9 on: August 15, 2018, 01:28:50 pm »
Again, there is no need for break in Pascal case of clause statements. If one statement is true, the rest are ignored.
Cyrax, yes, that is a fact.  The point is to break out early as the example I posted showed.
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 #10 on: August 15, 2018, 01:35:25 pm »
Again, there is no need for break in Pascal case of clause statements. If one statement is true, the rest are ignored.
Cyrax, yes, that is a fact.  The point is to break out early as the example I posted showed.
Code: Pascal  [Select][+][-]
  1.     case I of
  2.       somevalue :
  3.       begin
  4.         statement ...
  5.         statement...
  6.      
  7.         if not someconditionhere then begin  //  FPC is happy with this
  8.      
  9.           statement ...
  10.           statement....
  11.         end;
  12.       end;
  13.      
  14.       <more cases here>
  15.     end; { case }
  16.  
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: 2847
Re: how to break out early of a case ?
« Reply #11 on: August 15, 2018, 02:08:04 pm »
Hi Taaz,

Of course, it can be done by wrapping the code fragments in if statements, the downside of that becomes more obvious when there are multiple conditions that would cause the case to be exited early.  In those cases, there will be as many nested ifs as there are conditions that would cause an early break.   That is not as clean as simply breaking out of the case, which keeps the code linear (nested ifs free.)

Being able to use "break" would yield simpler code (no nested ifs.)


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 #12 on: August 15, 2018, 02:11:17 pm »
It's unfortunate there is no way of exiting a case block early.  An if statement solves the problem but, if there are multiple conditions which would cause the case to break early then that would result in the nesting of multiple if statements to implement what is really very simple, linear not hierarchical, logic.

In this simple example inverting the condition is probably the best solution but if you find yourself in a situation in which "there are multiple conditions which would cause the case to break early" I would suggest moving the body of that selection to a separate function or procedure where you can use Exit to your heart's content :) :

Code: Pascal  [Select][+][-]
  1. procedure Whatever;
  2.  
  3.   procedure DoSomething;
  4.   begin
  5.     statement ...
  6.     statement...
  7.  
  8.     if somecondition then
  9.       exit
  10.     if othercondition then begin
  11.       statement ...
  12.       statement ...
  13.       statement ...
  14.       Exit; {Early exit here too!}
  15.     end;
  16.     statement ...
  17.     statement....
  18.   end;
  19. begin
  20. {...}
  21. case I of
  22.   somevalue :
  23.       DoSomething
  24.   <more cases here>
  25. end; { case }
  26. {...}
  27.  
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: 2847
Re: how to break out early of a case ?
« Reply #13 on: August 15, 2018, 02:29:31 pm »
In this simple example inverting the condition is probably the best solution but if you find yourself in a situation in which "there are multiple conditions which would cause the case to break early" I would suggest moving the body of that selection to a separate function or procedure where you can use Exit to your heart's content :) :
Absolutely, that's one solution and it is reasonable.  That said, any solution that requires a form of nesting is not as clean and simple as a solution that is fully linear which is what being able to break out early of a case would enable.

Unfortunately, the answer to my question is: there is currently no way to break out early of a case statement.   

I'll keep that in mind when porting C code to Pascal.  The resulting code will not be as simple as in C (which is unusual, usually Pascal code is cleaner than C.)
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 #14 on: August 15, 2018, 03:07:14 pm »
In this simple example inverting the condition is probably the best solution but if you find yourself in a situation in which "there are multiple conditions which would cause the case to break early" I would suggest moving the body of that selection to a separate function or procedure where you can use Exit to your heart's content :) :
Absolutely, that's one solution and it is reasonable.  That said, any solution that requires a form of nesting is not as clean and simple as a solution that is fully linear which is what being able to break out early of a case would enable.

Unfortunately, the answer to my question is: there is currently no way to break out early of a case statement.   

I'll keep that in mind when porting C code to Pascal.  The resulting code will not be as simple as in C (which is unusual, usually Pascal code is cleaner than C.)
emulate it.

Code: Pascal  [Select][+][-]
  1. procedure Test(Choice:Byte);
  2. label AfterCase;
  3. begin
  4.   case Choice of
  5.     1..9 : begin
  6.              process;
  7.              process;
  8.              process;
  9.              process;
  10.              process;
  11.              process;
  12.              If SomeCondition then goto AfterCase;
  13.              process;
  14.              process;
  15.              process;
  16.              process;
  17.              process;
  18.              process;
  19.            end;
  20.     10..19 :begin
  21.              process;
  22.              process;
  23.              process;
  24.              process;
  25.              process;
  26.              process;
  27.              process;
  28.              process;
  29.              process;
  30.              If SomeCondition then goto AfterCase;
  31.              process;
  32.              process;
  33.              process;
  34.            end;
  35.     20..29 :begin
  36.              process;
  37.              process;
  38.              process;
  39.              process;
  40.              process;
  41.              process;
  42.            end;
  43.     end;
  44. AfterCase:
  45.   Process;
  46.   Process;
  47.   Process;
  48.   Process;
  49.   Process;
  50.   Process;
  51. end;
  52.  
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

 

TinyPortal © 2005-2018