Jump statements are used to transfer control from one code block to another under certain conditions. Various jump statements can be used only in specific language structures. There are the following types of jump statements in Fore:
The Break statement breaks the currently running cycle and directs the control to the operator following the cycle. If there is no enclosing cycle in the statement’s block, the statement does nothing.
Sub Main;
Var
i: Integer;
Begin
For i := 1 To 20 Do
Debug.WriteLine("Hi");
Break;
Debug.WriteLine("Hello");
End For;
End Sub Main;
After executing the example the console window displays only "Hi".
The Continue statement breaks the current iteration cycle and goes to the next iteration. If there is no enclosing cycle in the statement’s block, the statement does nothing.
Sub Main;
Var
i: Integer;
Begin
For i := 1 To 2 Do
Debug.WriteLine(i);
Continue;
Debug.WriteLine("Hello");
End For;
End Sub Main;
After executing the example the cycle will run twice, and the word "Hello" will not be displayed in the console window.
The Return statement is used to exit a subprogram.
For procedures, this statement is used without parameters and returns to a procedure call point.
For functions, an expression should follow the Return keyword that is returned to the procedure call point. The type of expression should correspond to the function type.
The Return statement may be mentioned several times in the program body. If none of the Return statements was executed during the subprogram execution, exit from the program will be executed at the end of program body. If a subprogram is a function, and exit from the function is executed not by the Return statement but by the end of the function body, some random value will be the function result.
Sub ReturnSample;
Function GetSummAB(a: Integer; b: Integer): Integer;
Begin
a := b;
b := 10;
Return a + b;
End Function GetSummAB;
Var
a: Integer;
Begin
a := 2;
If GetSummAB(a, 2) = 12 Then
Return
End If;
Debug.WriteLine("End");
End Sub ReturnSample;
When the ReturnSample method is executed, the GetSummAB nested function is called. The function results in the sum of two arguments. The procedure checks the result of the GetSummAB function. If the result is equal to 12, exit from the procedure is executed.
See also: