Instruction for loop implements an algorithmic structure loop with parameter(or a loop with a counter). The for loop is used when in the program, before executing the instructions of the loop, the number of steps of this loop becomes known (or predetermined). In the flowchart, the for statement is depicted as follows:

Syntax:

For( initialization; condition; modification) ( Loop body instructions; )

If there is one instruction in the loop body, then ( ) can be omitted. The cycle parameter variable (counter) can be of any numeric type. This makes the C++ for loop as versatile as a while loop. In the modification section, the postfix or prefix increment (or decrement) operation is most commonly used, but any assignment expression that changes the value of the loop parameter can be used. The loop works like this:

  • At the beginning, the description and initialization of the counter variable takes place.
  • Next, check the condition: if the expression has a value true, there will be an iteration
  • After executing the instructions of the loop body, the counter value is modified

Note: in C++ it is a rule to make the description of the counter variable in the loop header. But this is not necessary, especially if you plan to initialize several variables in the initialization section as implemented in Program 9.2. However, the use of a counter variable declaration in the loop header results in a local variable declaration that is automatically destroyed when the loop terminates. Therefore, unless absolutely necessary, the description of the counter variable outside the for loop should not be performed.
While the for loop is running, it is not recommended to change the operands in the loop header expressions - this will lead to all sorts of errors! But the values ​​of variables (or constants), including mutable values(counter), you can use. Consider a classic example.

Program 9.1 Given a natural number N. Print all divisors of this number.

#include << "N = "; cin >>N; for (int i = 2; i< N / 2; i++) { if (N % i == 0) cout << i << " "; } return 0; } N = 16000 2 4 5 8 10 16 20 25 32 40 50 64 80 100 125 128 160 200 250 320 400 500 640 800 1000 1600 2000 3200 4000

Using the continue statement in a for loop

When using the continue statement in a for loop, it is necessary to take into account the peculiarities of the operation of this loop:

  • Statements following continue will be skipped
  • Then the counter is modified.
  • Moving on to the next iteration (otherwise, checking the condition)

Let's show this with an example: int main() ( for (int i = 1; i< 20; i++) { if (i % 2 == 0) continue; cout << i << " "; } 1 3 5 7 9 11 13 15 17 19

Note. Please note: although the output of numbers by condition is skipped, the counter is incremented. This example is for illustration purposes only, you shouldn't program the loop like this! This problem is better solved in the following way:

int main() ( for (int i = 1; i< 20; i += 2) cout << i << " ";

Multiple expressions in the initialization and modification section

As we noted earlier, there should be three sections in the head of the for statement. Expressions in these sections can be omitted, but ";" cannot be omitted. . After all, you can only leave; . Title in the form:

For (;;) ( ... )

is the heading of an “infinite” loop. (The exit from the loop must be programmed inside the loop body).
C++ supports multiple expressions in the initialization and modification sections of the for statement header. In this case, the condition for continuing the cycle must be one!
For example. Problem statement: Calculate the factorial of a number not exceeding 20.
Program 9.2

#include using namespace std; int main() ( unsigned long long n; int i, k; cout<< "k = "; cin >>k; // 0<= k <= 20 for(n = 1, i = 1; i <= k; n *= i, ++i); cout << k << "! = " << n << endl; return 0; } k = 20 20! = 2432902008176640000

Note: note that the output stream on line 12 does not refer to the body of the loop! (At the end of the title - ;). Thus, this loop has an empty instruction in the body, and all expressions are evaluated in the header. Program 9.2 calculates the factorial of a number from 0 to 20 correctly.

Range-based for loop

To iterate through the elements of an array or container, you have to perform the same type of actions, while using cumbersome code. To simplify working with containers in C++, there is a special form of the for loop - range-based for (loop for based on range or range for).
Syntax:

For( ad : sequence_name) loop_statement

Using range-based for on a C-array example:
Program 9.3

#include using namespace std; int main() ( int x ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ); for (auto &s: x) ( cout<< s << " "; } return 0; }

In order for the elements of the array to be changed, the variable s must be a reference variable (as in the example above). If the variable is not a reference, then the data will be copied. For automatic type inference, the auto specifier is used in this loop. range-based for has a limitation for working with dynamic arrays: it does not support array resizing, since it contains a fixed array end pointer. When dealing with arrays that have a fixed size, the ranged for is a great and safe alternative to the regular for .

Nested for loops

Just like other loop statements, for supports the structure of nested loops. Using nested for loops to organize the input and output of two-dimensional arrays is much more compact than using a while loop.
However, when traversing such arrays, the use of the if statement should be avoided. Often, the task can be implemented more rationally by manipulating the indices (loop variables i and j). That is, to make the change of one index dependent on the value of the other. Let's consider two examples.
Program 9.4 Given a square matrix of size n, the elements of which are equal to 0. Fill the elements below and on the main diagonal with ones.

#include using namespace std; int main() ( int n; cout<< "n = "; cin >>n; intmas[n][n]; // Fill with zeros for(int i = 0; i< n; i++) for(int j = 0; j < n; j++) mas[i][j] = 0; // Реализация for(int i = 0; i < n; i++) for(int j = 0; j <= i; j++) mas[i][j] = 1; // Вывод for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cout.width(2); cout << mas[i][j]; } cout << "\n"; } return 0; } n = 10 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1

Program 9.5 Write a program to fill an array with the numbers of Pascal's triangle and output this array. Pascal's triangle looks like:


This triangle has 1's at the top and sides (in program 9.5 the triangle is “lay on its side” – the sides of the triangle: the first column and the main diagonal). Each number is equal to the sum of the two numbers above it. The lines of the triangle are symmetrical about the vertical axis and contain binomial coefficients.

#include using namespace std; int main() ( int n; cout<< "n = "; cin >>n; int pass[n][n]; for (int i = 0; i< n; i++) for (int j = 0; j < n; j++) pas[i][j] = 0; pas = 1; for (int i = 1; i < n; i++) { pas[i] = 1; for (int j = 1; j <= i; j++) { pas[i][j] = pas + pas[j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { cout.width(4); cout << pas[i][j]; } cout << "\n"; } return 0; } n = 12 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1

Questions
  1. Can a for loop statement be replaced by a while loop statement in a program? Is it always possible to do this?
  2. When is it better to use the for statement for looping? while?
  3. Are the following expressions possible in the header of the for statement: a) for (;a > b && !(a % 2);) b) for (a > b;;) c) for (;;i = 0) d) for ( ;i = 0;) e) for (;;i++, --b) f) for (--i;;) g) for (b = 0; b != a;) ?
  4. The variable i is the parameter of the outer loop, and j is the parameter of the nested one. Will the j variable be available in the outer loop? i in a nested loop?
Textbook
Homework
  1. Rear 29. Write a program that inputs natural numbers a and b, and the display shows all prime numbers in the range from a before b(algorithm idea Program 8.5)
  2. Rear 30. A perfect number is a number equal to the sum of all its divisors less than itself (for example, the number 6 = 1 + 2 + 3). Write a program that takes a natural number N and determines if N is perfect.
  3. Write a program that displays an n x n square numerical table that looks like this for n = 10: 1 * * * * * * * * * * 2 * * * * * * * * * * * 3 * * * * * * * * * * 4 * * * * * * * * * * 5 * * * * * * * * * * 6 * * * * * * * * * * * 7 * * * * * * * * * * * 8 * * * * * * * * * * 9 * * * * * * * * * * * 10
Literature
  1. Laforet R. Object-Oriented Programming in C++ (4th ed.). Peter: 2004
  2. Prata, Stephen. C++ programming language. Lectures and exercises, 6th ed.: Per. from English. - M.: I.D. William, 2012
  3. Lippman B. Stanley, Josy Lajoye, Barbara E. Moo. C++ programming language. Basic course. Ed. 5th. M: LLC “I. D. Williams”, 2014
  4. Elline A. C++. From lamer to programmer. St. Petersburg: Peter, 2015
  5. Schildt G. C++: Basic course, 3rd ed. M.: Williams, 2010



Hello dear readers! Here we come to the study of cycles. Cycles in Pascal. What it is? How to use it? What are they needed for? These are the questions I will answer today.
If you have read, then you know that there are three types of algorithms: linear, branching and cyclic. We already know how to implement algorithms in Pascal. Let's start studying the last type of algorithms.
In Pascal, as in most programming languages, there are three types of looping constructs.

Any cycle consists of a body and a header. The loop body is a set of repeating statements, and the condition is a logical expression, depending on the result of which the loop is repeated.

Let's take one problem, which we will solve using various types of cycles.

Task 1. Display all numbers from 1 to the number entered from the keyboard.

While, or a loop with a precondition

As you probably already understood from the name, while is a loop in which the condition comes before the body. Moreover, the loop body is executed if and only if the condition true; as soon as the condition becomes false

While has the format:

while < условие> do<оператор 1>; (Bye…do….)

This loop is suitable for only one statement, if you want to use multiple statements in your code, you should enclose them in statement brackets - begin and end;.

The solution of the problem.

Program example_while; var i, N: integer; (declaring variables) begin i:= 1; ( Set i to 1 ) readln(N); (Read the last number) while i<= N do {Как только i станет больше N, цикл прекратится (можно было бы написать просто <, но пришлось бы добавлять 1 к N) } begin {Открываем операторные скобки} write(i, " "); {Выводим i} Inc(i); {увеличиваем i на один.} end; { закрываем скобки } end.

Repeat, or a loop with a postcondition

Repeat- complete opposite while. Repeat is a loop in which the condition is after the body. Moreover, it is executed if and only if the result of the condition false;as soon as the boolean expression becomes true, the loop is terminated.

Repeat has the format:

repeat( repeat … )
<оператор 1>;
< оператор 2>;

until(before…) <условие>

Begin and end not required.

The solution of the problem.

Program example_repeat; var i, N: integer;( declare variables) begin i:= 1; ( Set i to 1 ) readln(N); ( Read last number ) repeat ( no need for begin and end after repeat ) write(i, " "); (Display i) Inc(i); (Increase i by one.) until i = N + 1; (For example, i = 11 and N = 10. The loop will stop, so the condition becomes true.) end.

For, or a loop with a parameter

For is a loop in which the body is executed a given number of times.

There are two ways to write this loop:

First form

for<счетчик1> := <значение1>to<конечное_значение>do<оператор1>;

<счетчик1>will increase by 1.

<значение1>is the initial value of the counter. It can be a variable or a number.
<конечное_значение>: as soon as the value<счетчик1>will become more<конечное_значение>

If you want to write several statements in the loop body, use begin and end.

And<счетчик1>, and<конечное_значение>, and<значение1>- variables the whole type.

Most often, the variable i is used as a counter.

Second form

for<счетчик2> := <значение2>downto<конечное_значение>do<оператор1>;

After each iteration, the value<счетчик2>will decrease by 1.

<значение2>is the initial value of the counter.
<конечное_значение>: as soon as the value<счетчик2>will become less<конечное_значение>, the loop is terminated.

Two important notes:

  1. The loop repeats as long as the value of the counter lies in the segment [value; end_value].
  2. Change the value of the counter inside the body it is forbidden! Here is what the compiler outputs:

The solution of the problem:

Program example_for; var i, N: integer; begin read(N); (suppose we entered 10) for i:= 1 to N do write(i, " "); (number of iterations - 10 - 1 + 1 = 10) end.

Agree, this code is simpler and more concise than all the previous ones. And cycle for- not quite an ordinary cycle, there is no logical condition in it. Therefore, a loop with a parameter in programming is called syntactic sugar. Syntactic sugar is an addition to the syntax of a programming language that does not add new features, but makes the language more human-friendly to use.

Let's solve a couple of problems.

For1. Integers K and N (N > 0) are given. Output N times the number K.

We organize a simple cycle from 1 to the required number.

Program for1; var K, N, i: integer; begin read(K, N); for i:= 1 to N do write(K, " "); (We write K separated by a space) end.

For2. < B). Вывести в порядке возрастания все целые числа, расположенные между A и B (включая сами числа A и B), а также количество N этих чисел.

Since A< B, то цикл должен будет выводить все числа от А до B. Чтобы сосчитать количество чисел, используем формулу: <конечное_значение> — <начальное_значение> + 1.

Program for2; var A, B, i, count: integer; begin read(A, B); for i:= A to B do write(i, " "); (write numbers from smallest to largest) count:= B - A + 1; (count number of numbers) writeln; write("Number of numbers - ", count); end.

For9. Given two integers A and B (A< B). Найти сумму квадратов всех целых чисел от A до B включительно.

We organize the same cycle as in the previous problem, but at the same time sum the squares of all numbers. To calculate the square, use the function.

Program for9; var A, B, i, S: integer; begin read(A, B); S:= 0; (PascalABC does this automatically, but if you have a different compiler we advise you to set the variables to zero manually) for i:= A to B do S:= S + Sqr(i); (add all squares) writeln; write("Sum of squares - ", S); end.

For13°. An integer N (> 0) is given. Find the value of the expression 1.1 - 1.2 + 1.3 - ... (N terms, signs alternate). Do not use the conditional operator.

In order to change the sign, each iteration of the loop we change the value of the special variable to the opposite.

Program for13; var N, A, i: integer; S: real begin Write("N = "); readln(N); S:= 1.1; A:= 1; (Positive first) for i:= 2 to N do (we have already done the first iteration of the loop, so we start counting from 2) begin A:= -A; (Now negative) S:= S + A * (1 + i / 10); (add) end; WriteIn(S:5:1); (Let's give one familiarity for the fractional part) end.

While1°. Positive numbers A and B (A > B) are given. On a segment of length A, the maximum possible number of segments of length B (without overlaps) is placed. Without using multiplication and division, find the length of the unoccupied part of segment A.

Each time subtract B from A until A - B >= 0.

Program while1; var A, B: integer; begin readln(A, B); while (A - B) >= 0 do A:= A - B; (While the difference is positive, we subtract. It is necessary to provide a variant with a multiplicity of A and B, therefore >=) write(A); end.

While4°.An integer N (> 0) is given. If it is a power of 3, then output True, if not, output False.

We act as follows: while N is divisible by 3, we divide N by 3. Then, if N = 1, the number is a power of three; if N<>1, then the number is not a power of three. In order to solve this problem, you need to know what is and how they work.

Program while4; var N: integer; begin readln(N); while N mod 3 = 0 do N:= N div 3; (As long as the remainder of division by three is zero, divide N by 3) writeln(N = 1); (boolean expression) end.

That's all for today! Do not forget to visit our site more often and click on the buttons that are located in front of the comments.

The cycle with a parameter has already been considered by us in the "Algorithm" section in the "Types of Algorithms" topic.
A loop with a parameter is used,when it is known in advance how many times the loop should be executed.

Cycle record format:

Here for, to, do- reserved words (for, before, perform);

<пар. цикла> - cycle parameter - variable integer type (integer type);
<нач. знач.> - initial value - number or variableinteger type (integer type);
<кон. знач.> - end value - number or
variableinteger type (integer type);
<оператор> is an arbitrary Pascal operator.

Example: For i:=1 to n do<оператор>
here i is the loop parameter
1 - initial value
n - final value
If several operators are used in the loop body, then operator brackets are used: begin ... end.
When executing the for statement, the expression is first evaluated<нач.знач.>and assigning its value to the loop variable<пар.цикла> := <нач. знач.>. Next are compared<пар.цикла>and <кон.знач.>. Until they become equal, the statement(s) will be executed. Loop variable value<нач.знач>automatically incremented by one during the loop.It should be noted right away that it is impossible to set a cycle step other than 1 in this operator.
Example:
The following loop statements are possible:

1) for i:= 1 to n do s1;

2) for i:= 3 to 10 do s1;

3) for i:= a to b do s1;

4) for i:= a to b do
begin

s1;
s2;
...
sn

end;

Here s1, s2, s3, ... sn are loop operators.

Example:
Write a program to display numbers from 1 to 10.

Example:
Write a program for calculating the factorial of a number n, i.e. n!. (0!=1)

Program explanation:
The variable n is for the number entered by the user, the factorial of which is to be found; f - a variable in which the value of the factorial of the number n will be "accumulated"; i - loop variable.
The initial value of the variable f:= 1 is set.
Then the cycle begins. The variable i is initialized to 1; it is compared with the final - n (1<= n), если условие истинно, тогда выполняется оператор (в этой программе он один): f:= f*i, 1*1=1; значение переменной цикла увеличивается на 1, т. е. станет равным: i:= i + 1, 1 + 1 = 2 и цикл повторяется.
When the value of i becomes equal to n, then the loop will execute for the last time, because the next value of i will be n + 1, which is greater than the final value of n, the condition i<= n - ложно, цикл не выполняется.

There is another form of the For loop statement:
Cycle record format:

Replacing the reserved word to with downto means that the step of the loop parameter is (-1).
The change in the parameter value goes from a larger value to a smaller one, i.e.<нач. знач.> <кон. знач.>.

Example:
The following loop statements are possible:

1) for i:= n downto 1 do s1;

2) for i:= 10 downto 3 do s1;

3) for i:= b downto a do s1; (assuming b>a)

4) for i:= b downto a do
begin

S1;
s2;
...
sn

end; (assuming b>a)

Here s1, s2, s3, ... sn are loop operators.

Example: Calculation program factorial numbers can be composed using this loop statement.


Tasks

  1. Given 10 numbers, print those that are perfect squares.
  2. Given 10 numbers, find their product.Make a block diagram and program.
  3. Given 10 numbers, find the sum of the even numbers.Make a block diagram and program.
  4. Given 10 numbers, find the number of negative ones.Make a block diagram and program.
  5. Given n real numbers. Find the maximum and minimum.Make a block diagram and program.
  6. Given n real numbers. Find the arithmetic mean of all elements.Make a block diagram and program.
  7. Given n real numbers. Find the arithmetic mean of negative and positive elements.Make a block diagram and program.
  8. Given n natural numbers. Find the sum and product of elements that are multiples of 3 and 5.Make a block diagram and program.
  9. Given n natural numbers. Withdraw those numbers whose values ​​are powers of two (1, 2, 4, 8, 16, ...).Make a block diagram and program.
  10. Given n natural numbers. Withdraw those numbers whose values ​​are in the segment.Make a block diagram and program.
  11. Given n natural numbers. Display those numbers whose values ​​are the squares of some number.Make a block diagram and program.
  12. Given a natural number n. Find n 2.Make a block diagram and program.
  13. Given natural numbers a, n. Find a n.Make a block diagram and program.
  14. Given a natural number n. Determine its capacity, increase the most significant digit of the number by 2
  15. Given a natural number n. Swap the first and last digits of a number
  16. Given a natural number n. Digits of a number that are multiples of 2 are replaced by 0.
  17. Given a natural number n. Digits of a number that are multiples of 3 are replaced by 1.
  18. Given a natural number n. Calculate the product (2n-1)*(3n-1)*(4n-1)*...*(10n-1).Make a block diagram and program.
  19. Calculate the sum 2+4+6+...+100.Make a block diagram and program.
  20. Given a natural number n, real x. Calculate the product x+x/2+x/3+...+x/n.Make a block diagram and program.
  21. Given a natural number n. Calculate P=(1-1/2)(1-1/3)...(1-1/n), where n>2.Make a block diagram and program.
  22. Given a natural number n. Calculate P=(1+x)/n+(2+x)/(n-1)+...+(n+x)/1.Make a block diagram and program.
  23. Given n natural numbers. Calculate the sum of a series1+x/1!+x 2 /2!+x 3 /3!+ ...+x n/n!. Make a block diagram and program.

The loop operator with a parameter is used precisely in such cases when it is necessary to organize a loop with a given number of repetitions

for <параметр_цикла>:=<начальное_знач> to <конечное_знач> do <оператор>;

for <параметр_цикла>:=<конечное_знач> downto <начальное_зна.> do <оператор>;

The statement, which is the body of the loop, can be simple or compound.

The loop parameter, as well as the range of its change, can only be of an integer or enumerated type.

The parameter is described together with other variables.

The step of the for loop is always constant and is equal to "1" or "-1".

Display the first ten positive integers

var i: integer; //counter is entered

fori:=1to10do//while the counter value is from 1 to 10 do the following

writeln(i); //output counter value

vari,sum:integer;

sum:=0; //zeroing the value of the variable

fori:=10to99do//enumeration of two-digit positive numbers

if i mod 3=0 then //multiplicity 3

sum:=sum+i; //sum of the previous value of the variable and the number corresponding to the condition

Display the product of the first ten positive even numbers

vari,pr:integer;

pr:=1; //when finding the product, the initial value of the variable is not 0, but 1

for i:=1 to 10 do

if i mod 2=0 then //determine parity

Given two integers A and B (A< B). Вывести в порядке возрастания все целые числа, расположенные между A и B (в том числе A и B), a также количество N этих чисел .

var i,pr: integer;

k:=0; //zero the value of the variable, which means the number

fori:=AtoBdo//enumeration of numbers from the given range

writeln(i); //output in ascending order

k:=k+1; // counting the number of numbers

writeln(k); //output of the amount occurs outside the loop because issued once

Enter N different numbers. Find the arithmetic mean of all numbers.

Varn,i,a:integer;

For i:=1 to N do

Writeln("arithmetic mean= ",s/n:4:2);

Loop statement with while ... Do precondition

The while ... do statement is designed to implement loops with a precondition.

The condition of the body of the while loop is checked before the beginning of each step. Therefore, if the condition is not met immediately, then the loop body is ignored, and control is transferred to the operator immediately following the loop body.

Contacting the operatorwhile ... do translates as "bye ... to do" and looks like this:

while <условие> do <оператор>

The while loop implies the following algorithm: while the condition is true, the statements of the loop body are executed.

The condition itself can be a boolean constant, a variable, or a boolean expression.

Keep the following in mind when writing loops with a precondition.

    for a loop to ever have a chance to end, the contents of its body must necessarily affect the condition of the loop.

    the condition must consist of valid expressions and values ​​defined before the first execution of the loop body.

If the loop condition is false, the loop will never execute!

In most programs, there is a need to repeatedly execute some statement (or block of statements). Loop statements can be used to organize such constructions. The Pascal programming language uses the following types of loop statements: for, while, repeat (PascalABC.NET also uses the foreach loop operator).

A block of statements that needs to be executed repeatedly is called the loop body.

for statement in Pascal

If a number of body repetitions cycle is known in advance, then it is used for loop statement, which is also often referred to as a loop statement with a parameter.

The for statement consists of two parts: the loop body and the header, which is intended to describe the initial and final values ​​of the loop parameter, as well as the option to change it.

Depending on the direction of change of the loop parameter (increase - to or decrease - downto ) in Pascal, the for loop operator can be written in one of two forms:

  • for parameter := start_value to end_value do
  • operator;
  • for parameter := start_value downto end_value do
  • operator;

The loop parameter variable can take any ordinal type. In this case, the initial and final values ​​must have a type compatible with the type of the parameter variable.

Let's consider the work of the for loop.

Before the execution of the loop statement begins, the initial value assigned to the parameter variable and the final value are calculated. Then, the following operations are performed cyclically:

  1. Compares the current value of the parameter with the final value.
  2. If the condition parameter end_value is true, then the loop body is executed, otherwise the for statement terminates and control is transferred to the statement following the loop.

Attention: in the Pascal language, the cycle parameter, regardless of the increase or decrease, changes by one each time.

A task. Display a list of squares of integers from 10 to 1.

Solution. In the problem posed, the cycle parameter decreases.

(Program code snippet)

  • for i:= 10 down to 1 do
  • writeln(i:2, " ", i * i);

Attention: if it is necessary to use more than one statement in the body of the loop, then a compound statement is used (operator brackets begin and end ).

Example 2. An applicant's grades in four exams are known. Determine the amount of points they have earned.

A task. The grades of the applicant in four exams are known. Determine the amount of points they have earned.

Solution. We will use a loop operator with parameters in the program, since the number of repetitions of the actions performed is known (the applicant received exactly four marks)

(Program code snippet)

  • s:= 0;
  • for i:= 1 to 4 do
  • begin
  • readln(mark);
  • s:= s + mark;
  • writeln(s);