Forum > General

how to break out early of a case ?

<< < (2/39) > >>

Zoran:
Invert the condition and use one more begin-end pair:


--- 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 // the opossite condition       statement ...      statement....    end;  end;   <more cases here>end; { case }  
Or you can put a label under end {case} and use goto.

Zoran:
You can also use "repeat - until True" instead of "begin - end":


--- 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 :   repeat //begin    statement ...    statement...      if someconditionhere then break;  //Now, FPC IS happy with this     statement ...    statement....  until True; // end;   <more cases here>end; { case }  
"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:

--- Quote from: Zoran on August 15, 2018, 12:09:32 pm ---Invert the condition and use one more begin-end pair:


--- 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 // the opossite condition       statement ...      statement....    end;  end;   <more cases here>end; { case }  
Or you can put a label under end {case} and use goto.

--- End quote ---

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.

Cyrax:
Again, there is no need for break in Pascal case of clause statements. If one statement is true, the rest are ignored.

440bx:

--- 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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version