Forum > General

how to break out early of a case ?

<< < (3/34) > >>

taazz:

--- Quote from: 440bx on August 15, 2018, 01:28:50 pm ---
--- Quote from: Cyrax 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.

--- End quote ---
Cyrax, yes, that is a fact.  The point is to break out early as the example I posted showed.

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    case I of      somevalue :      begin        statement ...        statement...             if not someconditionhere then begin  //  FPC is happy with this               statement ...          statement....        end;      end;           <more cases here>    end; { case } 

440bx:
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.)


lucamar:

--- Quote from: 440bx on August 15, 2018, 01:00:16 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.

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Whatever;   procedure DoSomething;  begin    statement ...    statement...      if somecondition then      exit    if othercondition then begin      statement ...      statement ...      statement ...      Exit; {Early exit here too!}    end;    statement ...    statement....  end;begin{...}case I of   somevalue :       DoSomething  <more cases here>end; { case }{...} 

440bx:

--- Quote from: lucamar on August 15, 2018, 02:11:17 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 :) :

--- End quote ---
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.)

taazz:

--- Quote from: 440bx on August 15, 2018, 02:29:31 pm ---
--- Quote from: lucamar on August 15, 2018, 02:11:17 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 :) :

--- End quote ---
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.)

--- End quote ---
emulate it.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Test(Choice:Byte);label AfterCase;begin  case Choice of    1..9 : begin             process;             process;             process;             process;             process;             process;             If SomeCondition then goto AfterCase;             process;             process;             process;             process;             process;             process;           end;    10..19 :begin             process;             process;             process;             process;             process;             process;             process;             process;             process;             If SomeCondition then goto AfterCase;             process;             process;             process;           end;    20..29 :begin             process;             process;             process;             process;             process;             process;           end;    end;AfterCase:  Process;  Process;  Process;  Process;  Process;  Process;end; 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version