Programming > General

how to break out early of a case ?

(1/7) > >>

440bx:
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  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 someconditionhere then break;  //  FPC is not happy with this     statement ...    statement....  end;   <more cases here>end; { case }  

Landslyde:
if (RadioButton.Checked) then
    Exit;

440bx:

--- Quote from: Landslyde on August 15, 2018, 06:35:37 am ---if (RadioButton.Checked) then
    Exit;

--- End quote ---

Exit would break out of the enclosing function/procedure, not just the case statement. 

Cyrax:
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:
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  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 avalue of 0..9:executeAvalue; // if case 0 to nine is satisfied... Execute something... 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.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..

Navigation

[0] Message Index

[#] Next page

Go to full version