Last Updated:

Break operator in Pascal

Many interrupt operators in Pascal provide an early exit from a block of code. However, the disadvantage is that there are several of these operators, and they are used in different designs. For example, the same break can only be used in loops. There is no use in other contexts.

goto operator

 

The goto operator works in conjunction with labels. The syntax for this statement is as follows: goto label name; Here it is indicated - go to the place of the code where the label is set. The goto statement specifies which label to navigate to, which can be set in any part of the code.

Example:

label go_here; declare a label for the goto
var digit: real operator;

begin
go_here: // set the label - the write algorithm
will be returned here ('Enter digit: ');
readln (digit);

if digit < 0 then // if the number is greater than zero, then return to the
goto label go_here;

digit := sqrt(digit);

write ('The square root of the number is: ', num:5:2);

readln
end.

Example: Infinite loop

label here;
var i: integer

begin
here: // label
for i:=0 to 3 do begin
write(i);
if i>2 then goto here; Switch to the end label
.
end.
//Conclusion: 0123012301230123... and so on ad infinitum

Here, if the user enters a number less than zero, the program again prompts him to enter a number. This will continue until the user enters a number equal to zero or greater.

Continue statement

This operator is designed to skip iterations in a loop. If there are 5 iterations, you can use continue to reduce them by a certain number.

Example:

var
digit, total: real;
i: integer;

begin
digit := 0;

for i := 1 to 8 do begin
write ('Enter digit: ');
readln (digit);
if digit < 0 then
continue; If you enter a number less than zero, the iteration is skipped.
total := digit + digit;
end;

write (total:10:2);

readln
end.

Example: Reducing the number of iterations

var i: integer;
begin
for i:=0 to 15 do begin
if i>8 then continue;
write(i);
end;
end.
// Output: 9 10 11 12 13 14 15

Exit statement

The exit statement is used to force exit from a block of code and return to the main thread of the algorithm. If you put it in the main thread, the main program will be forcibly terminated.

Example:

var
chislo: integer;

begin
write('Enter any number');
readln(chislo);
if chislo < 0 then
begin
writeln('Fatal ERROR!');
exit; Force termination of the program and termination of further end instructions
;
writeln('Square root = ', sqrt(chislo));
readln
end.

Unlike the break and continue, exit and goto operators, you can use not only in a loop, but also in procedures, functions, and the main thread of the program.

Example:

var x,t: integer;

procedure sum(y) begin
if y<1 then exit; if y is zero, then the exit from the procedure
x:= y*y;
end;

begin
again: // if there was a forced exit from the procedure, the operation is repeated write
("Enter a number");
read(t);
if sum(t) then exit; checking for execution of the
procedure else goto again;
end.
//If the procedure is performed, it will force you to exit the program. otherwise the program will prompt you to repeat the number entry

Keep in mind that the exit statement forcibly stops the execution of a block of code in which it resides.

Break statement

Break operator in Pascal

 

This statement performs the same function as exit, but only within a loop. Usually it is used in conjunction with a condition. If it is followed or not, then the break stops the loop, and the algorithm continues to execute further statements.

Example:

var
number: real;
j: integer;

begin
for j := 1 to 5 do begin
write ('Write a number: ');
readln (number);
if number = 0 then
break; If the input number is zero, then the loop exits and goes to the main context of the program
writeln (number)
end;

readln;
end.

Example:

var
stroka : String;
begin
while True do // endless line
input begin
ReadLn(S);
if S = » then // if the string is not empty, the loop continues to execute
Break; if the line is empty, exit the WriteLn(S) loop
;
end;
end.

Can I use break outside of loops?

 

Outside of loops, that is, in functions and code blocks, break does not carry a semantic load. Its presence will lead to an error. To exit the code block, use the exit statement.

Do I need a break statement in the case of construct?

 

In C-like programming languages, the break statement interrupts the execution of the switch statement, which is analogous to case of. In Pascal, you do not need to set a break in the case, the compiler itself exits this block if it finds a match.

Example:

label again; var
chosen_digit label: integer;

begin
again: // go to the label here
readLn(chosen_digit);

case chosen_digit of
1: write(«1»); Unlike C-like languages, the break operator is not required
2: write("2");
3: write(«3»);
4: write(«4»);
5: write(«6»);
6: write(«6»);
else begin
write (The number must be no more than 6");
goto again; If the number does not meet the requirement, then re-enter
the end;
end.

Here, when you execute one of the cases, the algorithm will not continue to execute further. In C-like languages, the remaining numbers would be derived after a coincidence.

There is no return statement in Pascal

Procedures in Pascal do not return anything. Functions return! Other languages use the return statement to return the result. Pascal is arranged a little differently. To return, use a local variable that corresponds to the value of the function.

Example:

var a: real;
function Summa(x: real, y: real);
begin
Summa:=x+y; this will be the end return value
;
begin
a:=Summa(5,5);
write(a); 10
end.

How do I exit a function without an exit statement?

Nohow! In other languages, it is enough to write return. In pascal, only the exit operator is used for this purpose.

Example:

var b: real;
function Addition(c: real, d: real);
var z: integer
begin
z:=c+d;
if z=0 then exit // if z is zero, exit the function without returning
else Addition:=z; this will be the end return value
;
begin
b:=Addition(0,0);
write(a); the variable is not initialized to
end.

Difference between exit and break statement

It would seem that they perform the same function. However, there are situations where exit has disastrous consequences for the algorithm. These situations occur when there is a loop in the function.

If you exit the loop by using the exit statement, the output is also from the function or procedure. And we need the way out of the loop and only. This is where the break operator is applied, which stops the loop, and the function continues its execution. If you put exit in the loop, the code will run on the main thread, and the function will stop executing.

Example: exit in a procedure loop

procedure sign(s: Sring)
var i:integer;
begin
for i:=0 to 10 do begin
if i=4 then exit // complete exit from
write(s);
end;
write("Continuation of the procedure"); the end will be
ignored;

begin
sign(«hello»); hello will be written 4 times
end.

Example: Same function, only with break

procedure sign(s: Sring)
var i:integer;
begin
for i:=0 to 10 do begin
if i=4 then break //only exit the write(s) loop
;
end;
write("Continuation of the procedure"); the end will be executed
;

begin
sign(«hello»); hello will be written 4 times and the continuation of the procedure
will end.

Total

It is worth remembering that individual interrupt operators have their own characteristics. Break completes the execution of the loop, but not the function or procedure. Exit completely closes the context of a function or procedure.

The goto operator is used in conjunction with the label. It translates the algorithm to the point of the set label. It can be compared to a cycle. However, he has a more flexible job. It can translate the algorithm to any point in the program — from a function to the main context, and vice versa.

The continue operator is called to skip loop iterations. It is used exclusively in cycles. It does not interrupt the entire loop algorithm, but only one iteration. In functions, procedures, and the main block, its use is not allowed, as well as the break statement. The exit statement remains the universal statement, but it should be used carefully because it exits the current block.