Exit "if" in Delphi

you can exit the while loop with break .

How to get out if .

Does Delphi have a GOTO look?

procedure ... begin if .... then begin here the code to execute if (I want to exit = TRUE) then break or GOTO here the code not to execute if has exited end; here the code to execute end; 
+6
source share
9 answers

You can use exceptions.

Call Abort in an internal if or loop and remove the EAbort exception where u want to continue

 procedure ... begin try if .... then begin (* here the code to execute *) if I_want-to-exit then Abort; (* here the code not to execute if has exited *) end; except on E: EABORT do ; end; (* here the code to execute *) end; 
0
source

Like Piskvor mentioned , use the nested if statement:

 procedure Something; begin if IWantToEnterHere then begin // here the code to execute if not IWantToExit then // here the code not to execute if has exited end; // here the code to execute end; 
+11
source

I am not a fan of using Exit this way, but you asked for it ...

Like @mrabat suggested in a comment to @Arioch 'Answer, you can use the fact that the finally block is always executed regardless of Exit and exceptions to your advantage here:

 procedure ... begin if Cond1 then try // Code to execute if Cond2 then Exit; // Code NOT to execute if Cond2 is true finally // Code to execute even when Exit was called end; end; 
+4
source

This is usually done as follows:

 function testOne: Boolean; begin ... end; function testTwo: Boolean; begin ... end; procedure runTests; var bOK: Boolean; begin bOK = True; if bOK then begin // Test something bOK = testOne; // If we passed, keep going end; if bOK then begin // Test something else bOK = testTwo; // If we passed, keep going end; ... end; 
+3
source

In general, using break or GOTO is not considered an elegant programming style. I suggest you just invert your state and say:

 procedure ... begin if .... then begin here the code to execute If (I want to exit <> TRUE) then here the code to execute if has exited( in your original code) end; 

here is the code to execute

end;

+2
source

After reading various comments, I decided to post an answer to show how to use the GoTo ... BTW instruction , I prefer other methods explained in other answers, avoiding its use:

 procedure ... label CodeToExecute; const iWantToExit = True; begin if ... then begin ShowMessage('here the code to execute 1'); if iWantToExit then goto CodeToExecute; ShowMessage('here the code not to execute if has exited'); end; CodeToExecute: ShowMessage('here the code to execute 2'); end; 
+2
source

Use the local inline procedure instead of the GOTO clause; GOTO clause reduces the visibility of your code.

 procedure ... procedue Check; inline; begin if .... then begin here the code to execute if (I want to exit = TRUE) then exit; here the code not to execute if has exited end; end; begin Check; here the code to execute end; 
+1
source

You can use the following method to exit the if block with break. The following example assumes that the form has two edit fields. repeat ... until true; replaces the usual begin ... end;

  if ... then repeat Edit1.Text := 'test'; if someCondition then break; Edit2.Text := 'test'; until true; 

Edit: Clarification, the solution simply assumes that the question is a brain teaser. This is not my recommended way to handle the situation, and I will never use this in my code.

0
source

readable code

// if cond1, then if cond2, then if cond3, then -

 procedure cond1_cond3; begin if not(Cond1) then exit; cond1_work; if not(Cond2) then exit; cond2_work; if not(Cond3) then exit; cond3_work; end; procedure All_Work; begin All_work_1; cond1_cond3; All_work_2; end 
-1
source

Source: https://habr.com/ru/post/921472/


All Articles