A compound statement is several statements combined into one to perform actions in another statement (conditional, loop, selection, etc.)

if x>0 then y:=x+2 else y:=x-2

13. Conditional operator.

b) Branching structure

Conditional jump operator

The conditional jump operator in Turbo Pascal has the form:

if condition then operator 1 else operator 2;

condition is a logical expression, depending on which one of two alternative branches of the algorithm is selected. If the condition value is TRUE, then the operator 1, written after the then keyword. Otherwise it will be executed operator 2, following the word else, while operator 1 skipped. After executing the specified statements, the program proceeds to executing the command immediately after the if statement.

It must be remembered that before keyword else semicolon is never used!

else - part in the if statement may be missing:

if condition then operator 1;

Then in case of failure logical condition control is immediately transferred to the operator that appears in the program after the if construct.

It should be remembered that the language syntax allows only one statement to be written after the then and else keywords, so a group of instructions must be combined into a compound statement (surrounded by operator brackets begin ... end). Otherwise, most often a logical program error occurs when the language compiler does not produce errors, but the program nevertheless works incorrectly.

Examples.

if x > 0 then modul:= x else modul:= -x;

if k > 0 then WriteLn("k is a positive number");

if min > max then begin

14.Transition operator. Labels and jump operators

It can be theoretically shown that the operators considered are quite sufficient for writing programs of any complexity. In this regard, the presence of jump operators in the language seems unnecessary. Moreover, modern structured programming technology is based on the principle of “program without GOTO”: it is believed that the abuse of jump operators makes the program difficult to understand, confusing and difficult to debug.

However, in some cases, using jump operators can simplify a program.

The transition operator has the form:

GOTO<метка>.

Here GOTO is a reserved word (go [to label]);<метка>- label.

A label in Turbo Pascal is an arbitrary identifier that allows you to name a certain program statement and thus refer to it. For compatibility with the standard Pascal language, Turbo Pascal also allows the use of unsigned integers as labels.

The label is located immediately before the marked statement and is separated from it by a colon. An operator can be marked with several labels, which in this case are separated from each other by a colon. Before appearing in the program, the label must be described. A label description consists of the reserved word LABEL followed by a list of labels:

Example

1: WriteLn("Go to label 1");

The effect of a GOTO statement is to transfer control to the corresponding labeled statement. When using labels, the following rules must be followed: the label referred to by the GOTO statement must be described in the description section and it must appear somewhere in the body of the program; labels described in the procedure (functions) are localized within it, so transferring control from outside the procedure (function) to a label inside it is impossible. An empty statement does not contain any actions, an extra semicolon is simply added to the program. Basically, the empty statement is used to transfer control to the end of a compound statement.

An operator is an indivisible element of a program that makes it possible to perform certain algorithmic actions. The difference between an operator and other elements is that it always implies some kind of action. In Pascal, operators consist of function words. Operators used in a program are separated from each other and from other program elements by the symbol (;). All Pascal operators can be roughly divided into two groups:

  1. simple;
  2. structured.

Simple Operators are operators that do not contain other operators. These include:

  • assignment operator (:=);
  • procedure statement;
  • unconditional jump operator (GOTO).

Structured statements are operators that contain other operators. These include:

  • compound operator;
  • conditional operators (IF, CASE);
  • loop operators (FOR, WHILE, REPEAT);
  • join operator (WITH).

Simple Operators

Procedure operator

Procedure operator serves to call a procedure.

Format: [procedure_name] (list of call parameters);

A procedure statement consists of a procedure identifier, immediately followed by a list of call parameters in parentheses. Pascal has procedures without parameters. In this case, when called, there is no list of parameters. Execution of a procedure statement leads to the activation of the actions described in its body. There are two types of procedures in Pascal:

  • Standard ones, which are described in the language itself and are part of the language;
  • User procedures that are created by the user.

To call standard procedures, you must connect in the USES section of the module (library) name, where this procedure is described. A number of procedures located in the SYSTEM module are always connected to the program automatically and their connection in the USES section is not necessary. Standard Pascal procedures are READ, WRITE, REWRITE, CLOSE, RESET.

READ ([file_variable], [input_list])

READ(x,y)

User procedures (non-standard) must be created before their use in the program and are located either in the description section of the program itself, or in separate program units of the module. If the procedure is in a module, then the name of that module must be mentioned in the USES application.

Unconditional GOTO operator

Format: GOTO [label];

GOTO is a reserved word in Pascal. [label] is an arbitrary identifier that allows you to mark a certain program statement and later refer to it. In Pascal, it is possible to use unsigned integers as labels. The label is placed before and separated from the marked operator (:). One statement can be marked with several labels. They are also separated from each other (:). Before using a label in the statement section, it must be described in the LABEL section (description section).

The GOTO action transfers control to the appropriate marked statement. When using tags, you must follow the following rules:

  • the label must be described in the descriptions section and all labels must be used;
  • if integers are used as labels, they are not declared.

Contradicts the principles of technology structured programming. Modern programming languages ​​do not include such an operator, and there is no need to use it. Besides, in modern computers The so-called conveyor method is used. If an unconditional jump operator is encountered in a program, then such an operator breaks the entire pipeline, forcing it to be created anew, which significantly slows down the computational process.

Structured statements

IF condition statements

The conditional operator is used in the program to implement the algorithmic structure - branching. In this structure, the computational process can continue in one of the possible directions. The choice of direction is usually carried out by checking some condition. There are two types of branching structures: fork and bypass structures.

In the Pascal language, the conditional IF operator is a means of organizing a branching computational process.

Format: IF [boolean_expression] Then [operator_1]; Else [operator_2];

IF, Then, Else - function words. [operator_1], [operator_2] - ordinary operations of the Pascal language. The Else part is optional (may be missing).

The IF statement works like this: It first checks the result of a Boolean expression. If the result is TRUE, then [statement_1] following the Then function word is executed, and [statement_2] is skipped. If the result is FALSE, then [statement_1] is skipped and [statement_2] is executed.

If the Else part is missing, then the IF statement is not in full form:

IF [boolean expression] Then [operator];

In this case, if the result is True (TRUE), then the [statement] is executed; if False (FALSE), then control is transferred to the statement following the IF statement.

There are 2 numbers A and B. Find the maximum number.

Compound operator

A compound statement is a sequence of arbitrary operations in a program, enclosed in so-called operator brackets (Begin-End).

Format: Begin [operators]; End;

Compound statements allow a group of statements to be represented as a single statement.

CASE selection statement

Designed to implement multiple branches, since the IF operator can implement only two directions of the computational process, it is not always convenient to use it to implement multiple branches. Multiple branching is implemented by the CASE statement.

Format: CASE [select_key] OF

[selection_constant_1]:[operator_1];

[selection_constant_2]:[operator_2];

[selection_constant_N]:[operator_N];

ELSE [operator];

CASE, OF, ELSE, END - function words. [select_key] is a parameter of one of the ordinal types. [selection_constants] - constants of the same type as the selection key that implement selection. [operator_1(N)] is an ordinary operator. ELSE may be missing.

The selection operator works as follows: before the operator operates, the value of the selection key parameter is determined. This parameter can either be expressed as a variable in the program or in some other way. Then the selection key parameter is sequentially compared with the selection constant. If the value of the selection key matches one of the selection constants, the operator following this constant is executed, and all other operators are ignored. If the selection key does not match any of the constants, the statement following Else is executed. Often Else is optional and if the selection key does not match any of the selection constants and in the absence of Else, control is transferred to the statement following the CASE statement.

The CASE statement does not have the explicit conditional checking that is typical for the IF statement. At the same time, the comparison operation is performed implicitly. CASE introduces dissonance into a Pascal program, since this statement ends with the service word End, which does not have a paired Begin.

Create an algorithm and program for a problem simulating the operation of a traffic light. When you enter the symbol of the first letter of the traffic light colors, the program should display a message about the corresponding color and actions.

The program works as follows: using the Read procedure, the letter symbol of the traffic light color is entered from the keyboard. If the letter ‘z’ corresponding to the green color is entered, then in the CASE statement the value entered in the selection list will find the selection constant ‘z’ and the message “Green color, movement is allowed” will be displayed. When you enter the symbol of the letters 'k' and 'zh', similar messages will be displayed. If you enter any other character, the message “Traffic light does not work” will be displayed, because in this case the Else part of the CASE statement works.

Loop statements

A cyclic algorithmic structure is a structure in which some actions are performed several times. There are two types of loop structures in programming: the loop with a parameter and the iterative loop.

In a cycle with a parameter there are always so-called cycle parameters: X, X n, X k, ∆X. Sometimes a loop with a parameter is called a regular loop. A characteristic feature is that the number of loops and repetitions can be determined before the loop is executed.

In an iterative loop, it is impossible to determine the number of loops before executing it. It is executed as long as the loop continuation condition is satisfied.

The Pascal language has three operators that implement cyclic computational structures:

  • counting operator FOR. It is intended to implement a loop with a parameter and cannot be used to implement an iterative loop;
  • loop operator with WHILE precondition;
  • loop operator with REPEAT postcondition.

The last two are focused on implementing an iterative loop, but they can also be used to implement a loop with a parameter.

FOR operator

Format: FOR [loop_parameter] := [n_z_p_ts] To [k_z_p_ts] Do [operator];

FOR, To, Do are function words. [cycle_parameter] - cycle parameter. [n_z_p_ts] - the initial value of the cycle parameter. [k_z_p_ts] - final value of the cycle parameter. [operator] - arbitrary operator.

The loop parameter must be an ordinal variable. The start and end values ​​of the loop parameter must be of the same type as the loop parameter.

Let's consider the operator's work using its algorithm:

At the first step, the value of the cycle parameter takes [n_z_p_ts], then the cycle parameter is checked to be less than or equal to [k_z_p_ts]. This condition is the condition for continuing the loop. If executed, the loop continues its operation and the [statement] is executed, after which the loop parameter is increased (decreased) by one. Then, with the new value of the loop parameter, the condition for continuing the loop is checked. If it is fulfilled, then the actions are repeated. If the condition is not met, then the loop stops running.

The For operator is significantly different from similar operators in other programming languages. The differences are as follows:

  • body of the For operator. The statement may never be executed because the loop continuation condition is checked before the loop body;
  • the cycle parameter change step is constant and equal to 1;
  • The body of the loop in the For statement is represented by one statement. If the action of the loop body requires more than one simple statement, then these statements must be converted into one compound statement using operator brackets (BEGIN-END);
  • A loop parameter can only be an ordinal variable.

Example of using the FOR operator: create a table for converting rubles to dollars.

WHILE statement (loop statement with precondition)

Format: WHILE [condition] Do [operator];

WHILE, Do - function words. [condition] - expression of logical type. [operator] is an ordinary operator.

;

The While statement works as follows: first, the result of the logical condition is checked. If the result is true, then the statement is executed, after which it returns to checking the condition with the new value of the parameters in the logical expression of the condition. If the result is false, then the loop is terminated.

When working with While, you need to pay attention to its properties:

  • the conditions used in While are the loop continuation condition;
  • in the body of the loop, the value of the parameter included in the condition expression always changes;
  • The While loop may not execute even once, since the condition check in the loop continuation is performed before the loop body.

REPEAT statement (loop statement with postcondition)

Format: REPEAT [cycle_body]; UNTIL [condition];

The REPEAT statement works as follows: first, the statements in the body of the loop are executed, after which the result is checked for the logical condition. If the result is false, then it returns to executing the statements of the next loop body. If the result is true, then the operator exits.

The Repeat operator has the following features:

  • in Repeat, the loop termination condition is checked and if the condition is met, the loop stops working;
  • The body of the loop is always executed at least once;
  • the parameter for checking the condition is changed in the body of the loop;
  • The loop body statements do not need to be enclosed in operator brackets (BEGIN-END), while the role of operator brackets is performed by Repeat and Until.

Calculate y=sin(x), where xn=10, xk=100, step is 10.

It happens that programs need to organize branching. In this case, the process of solving a problem occurs on the basis of the fulfillment or non-fulfillment of some condition.

In the Pascal language, the selection of an action, depending on some condition, can be implemented using the construction

if... then... else... if... then...

2. What type does it have? full form conditional jump operator if in Pascal?

Full form of the conditional jump operator if :

if condition then operator1 else operator2;

The action of the operator is as follows: first, the value of the expression is calculated condition . If it is true (equal to TRUE ), then the statement that follows the word is executed then (operator1). If the value is false (FALSE), then the statement that follows the word is executed else(operator2 ).

Operators operator1 And operator2 can be compound, that is, contain several lines of code. Such operators are placed in operator brackets begin...end. This need arises if after reserved words then or else you need to specify several operators.

In this case, the general form of the conditional jump operator may have, for example, the following form:

if condition then begin // several operators ... end else begin // several operators ... end ;

3. What is the abbreviated form of the conditional jump operator?

The short form of the conditional jump operator does not contain a block else and has the form:

if condition then operator;

In this case, the operator works as follows. First, the value of the logical (Boolean) expression is calculated condition . If the result of a logical expression condition true (equal TRUE), then the operator that follows the word is executed then. If the result is FALSE , then the statement that follows the statement is executed if(in the statement if...then nothing is done).

If, when a condition is met, several operators need to be executed, then the general form of the conditional jump operator can be as follows:

if condition then begin // two or more operator ... end ;

4. Examples of using the conditional jump operator, which has a full representation form.

Example 1. Fragment of a program for finding the maximum value between two real numbers.

var a,b:real; // a, b - variables for which the maximum is sought max:real; // maximum ... begin ... // a, b - are specified if a>b then max:=a else max:=b; ... end ;

Example 2.

A fragment of program code that solves this problem:

... var x,f:real; begin ... // x - specified if -5 then f:= x*x+8 else f:= -x*x*x+2 ; // in the variable f - the result ... end ;

5. Examples of using the conditional jump operator, which has a shortened form of representation.

Example 1. Code snippet that finds the minimum value between two real numbers x And y .

... var min:real; ... begin ... // x, y - given min:= x; if min then min:= y; ... end ;

Example 2. Calculate the value of a function according to a condition. Suppose you need to find the value of a function:

var x, f:real; begin ... // x - specified if x<-6 then f:=3 *x*x-x; if (-6 <=x) and(x<=5 ) then f:=sqrt(7 -x); if x>5 then f:=8 *x-3 ; ... end ;

When solving most problems, computational processes branch out. Selective statements are used to determine the further direction of program execution. This class includes the conditional operator and the selection operator.

The conditional operator, used to branch the algorithm into two directions, is one of the key tools not only in Pascal, but also in any other programming language.

The conditional statement can be in two forms: full and short.

Full form of the conditional statement

The full form of the conditional operator in Pascal is as follows:

  • if expression then
  • operator1
  • operator2

The expression element is a Boolean expression. If the expression evaluates to true, then operator1 (then branch) is executed, otherwise operator2 (else branch) is executed. Then, control is transferred to the operator following the conditional.

Consider a code fragment of a program that determines the minimum value of two numbers:

(Program code fragment)

  • if (a > b) then
  • minDig:= b
  • minDig:= a;
  • writeln(minDig);

If the value of variable a is greater than the value of variable b, then the assignment operator will be executed in the then branch (minDig will receive the value of b), otherwise - in the else branch (minDig will receive the value of a), then the value of the minDig variable will be printed.

In a conditional statement, there can be only one statement after then and after else. Therefore, if you need to use more than one operator, then a compound operator is used.

Short form of the conditional statement

The short form of the conditional operator is written as follows:

  • if expression then
  • operator

If the expression evaluates to true, then the statement is executed, otherwise the transition to the next program statement occurs. So, in the following fragment of program code, if the number x turns out to be odd, then its value will be increased by 1 (i.e., it will become even), otherwise the transition to displaying the value of x on the screen occurs.

This lesson looks at the conditional operator in Pascal ( if). Explains how to use multiple conditions in one construct ( AND And OR). Examples of working with an operator are considered

We remind you that this site does not pretend to provide a complete presentation of information on the topic. The purpose of the portal is to provide the opportunity to learn material based on ready-made solved examples on the topic “Pascal Programming Language” with practical tasks to reinforce the material. The Pascal tasks presented on the site are arranged sequentially as their complexity increases. The website can be used by teachers and lecturers as an auxiliary visual aid.

Before considering this topic, linear algorithms in Pascal were mainly used, which are typical for very simple problems when actions (operators) are performed sequentially, one after another. More complex algorithms involve the use of a branching construct.

Conditional operator block diagram:

The conditional statement in Pascal has the following syntax:

Abridged version:

if condition then statement;

Full version:

if condition then statement else statement;

The conditional operator in Pascal - if - serves to organize the progress of a task in such a way that the sequence of execution of operators changes depending on some logical condition. A logical condition can take one of two values: either true or false, respectively, it can be either true or false.

Compound operator

If, under a true condition, it is necessary to execute several statements, then According to the rules of the Pascal language, they must be enclosed in a block, starting with the function word begin and ending with the function word end . Such a block is usually called operator brackets, and this construction - compound operator:

Operator brackets and compound operator in Pascal:

if logical expression then begin statement1; operator2; end else begin statement1; operator2; end;

Translation from English of the condition operator will make it easier to understand its use:

IF THEN ELSE
IF THAT OTHERWISE


The condition (in a logical expression) uses relational operators.
Consider Pascal's list of relational operators:

  • more >
  • less
  • greater than or equal to in Pascal >=
  • less than or equal to in Pascal
  • comparison in Pascal =
  • not equal in Pascal

Example: find the largest of two numbers

Option 1 Option 2


Understand the work in detail you can use the conditional operator in Pascal by watching the video tutorial:

Example: calculate the value of the variable y using one of two branches

Show solution:

var x,y:real; begin writeln("enter x"); read(x); if x>0 then y:=ln(x) else y:=exp(x); writeln ("y=", y:6:2) (the resulting number will occupy 6 positions and will have 2 decimal places) end.

Please note how in in this example y is output. When withdrawing type variables in pascal, you can use the so-called formatted output, or notation with two colons:
y:6:2
- the number after the first colon (6) indicates how many characters the number will occupy when displayed on the screen
- the number after the second colon (2) indicates how many decimal places there are real number will be displayed

Thus, using such notation in pascal practically allows you to round to hundredths, thousandths, etc.

Task 0. Calculate the value of the variable y using one of two branches:

Task 1. Two numbers are entered into the computer. If the first is greater than the second, then calculate their sum, otherwise - the product. After this, the computer should print the result and the text PROBLEM SOLVED

Task 2. The dragon grows three heads every year, but after it turns 100 years old, only two. How many heads and eyes does a dragon have? N years?

Logical operations in Pascal (in logical expression)

When you need to use a double condition in Pascal, you will need logical operations.

  • Logical operation AND (And), placed between two conditions, says that both of these conditions must be met at once (must be true). The logical meaning of the operation is “conjunction”.
  • Placed between two conditions, the sign OR (OR) says that it is sufficient if at least one of them is satisfied (one of the two conditions is true). The logical meaning of the operation is “disjunction”.
  • In Pascal XOR - a sign of a logical operation that has the meaning of “strict disjunction” and indicates that it is necessary that one of the two conditions be fulfilled (true), and the other not satisfied (false).
  • Logical operation NOT before a logical expression or variable it means “negation” or “inversion” and indicates that if a given variable or expression is true, then its negation is false and vice versa.

Important: Each simple condition must be enclosed in parentheses.

Example: Let's look at examples logical operations V logical expressions in Pascal

1 2 3 4 5 6 7 8 var n: integer; begin n: = 6 ; if (n>5 ) and (n<10 ) then writeln ("истина" ) ; if (n>7 ) or (n<10 ) then writeln ("истина" ) ; if (n>7 )xor (n<10 ) then writeln ("истина" ) ; if not (n>7) then writeln("true"); end.

var n:integer; begin n:=6; if (n>5) and (n<10) then writeln("истина"); if (n>7) or (n<10) then writeln("истина"); if (n>7)xor(n<10) then writeln("истина"); if not(n>7) then writeln("true"); end.

Example: The company recruits employees from 25 to 40 years of age inclusive. Enter the person’s age and determine whether he is suitable for this company (display the answer “suitable” or “not suitable”).
Peculiarity: we need to check whether two conditions are met simultaneously.