Cycle with precondition (WHILE operator). Cycle with precondition What is a cycle with a precondition

Cycle while It is called precondition, since testing the condition is carried out before each performance of the cycle body. Used if the number of repetitions of the cycle is in advance unknown.

Performing a cycle operator while Begins with the verification of the condition. If it is true, the operators of the cycle body are performed, then the condition is then checked, etc. As soon as the next step it turns out that the condition is false, then the execution of the cycle will be completed. Cycle while may never be fulfilled if the condition at the very beginning is false.

In the cycle flowchart while It is depicted as shown in Fig. 5.3.

Fig. 5.3. Cycle block diagram while

Entering the operator cycle While It is carried out similarly to the input of the cycle for,after pressing the button while Panels Programmingthe elements shown in Figure appear on the screen. 5.4. In the first field, enter the condition of the cycle, and in the lower field, the operators of the cycle body. If in the body of a cycle of more than one operator, then you need to use the button Add Line.to add additional operators to the cycle body.

Fig. 5.4. Inserting the operator cycle while

In the body of the cycle, at least one operator, changing the cycle condition, so that the cycle through a certain number of iterations is completed, otherwise the cycle will be performed infinitely.

Example 5.4. Find the first negative member of the sequence

Block diagram of an example solution algorithm:

Example 5.5. Calculate function values When changing argument h. from -1 to 3 in increments 0.5 and form from these values \u200b\u200bvector y..

This task has already been considered, but using a cycle Fordeciding it with a cycle While.

Description and Call Program-Functions:

Example 5.6.Find the number and sum of the numbers of a given natural number. Description and call program-function:


This program uses two functions:

· mod - issues the balance during division x. on the y..

· trunc- gives a whole part z.Removing the fractional part.

Example 5.7. Create a program to calculate the amount of members of an infinite series with an accuracy of a member of a series.

At the same time, the calculation of the current member of the series is performed by the formula: -Teering member of the series - Previous member of the series .

An iterative is the computing process in which it is used to determine the subsequent value of the variable. When using iterative processes, the method of consecutive approximations is implemented. The cycle manages the specified calculation error e.. If at the next iteration error ≥e, the cycle continues to calculate the subsequent approximate value of the result, otherwise exit from the cycle.

Description and Call Program-Functions:



Nested cycles

If the cycle body is a cyclic structure, then such a cycle is called nested or complex. The algorithm with the structure of nested cycles is an algorithm containing a cycle within which one or more other cycles are placed.

The simplest case of nested cycles is double. In this case, one cycle is placed inside the other. Various options for nested cycles:

Example 6.1. Calculate .

Description and Call Program-Functions:

The operation of the invested cycle is as follows: first set the first value of the external cycle parameter i., then control is transmitted to the internal cycle and the parameter of the internal cycle j. Takes up all values. When the internal cycle is completed, the second value of the external cycle parameter is set and the internal cycle is completely performed again. The process is repeated until the external cycle parameter accepts all values.

Cycle with precondition

Cycle - A type of control design in high-level programming languages, intended for the organization of multiple execution of the set of instructions. Also, any multiple executable sequence of instructions can be called a cycle, organized by any method (for example, using a conditional transition).

Definitions

The sequence of instructions intended for multiple execution is called body cycle. ONED ORDER OF THE BODY OF THE CYCLE CALLED iteration. The expression is determining, it will once again be executed, or the cycle will end, called condition of exit or condition of the end of the cycle (or term condition Depending on how its truth is interpreted - as a sign of the need to complete or continue the cycle). The variable storing the current iteration number is called counter of Iterations Cycle or simply cycle meter. The cycle does not necessarily contain a counter, the counter is not obliged to be one - the exit condition from the cycle may depend on several variables changed in the cycle, and can be determined by external conditions (for example, the onset of a certain time), in the latter case the counter may not be needed at all.

The execution of any cycle includes the initial initialization of the cycle variables, checking the output conditions, the cycle body execution and updating the cycle variable on each iteration. In addition, most programming languages \u200b\u200bprovide funds for early completion of the cycle, that is, output from the cycle, regardless of the truth, the exit condition.

Types of cycles

Unconditional cycles

Sometimes cycles are used in the programs, the output of which is not provided for by the program logic. Such cycles are called unconditional, or endless. Special syntaxes for creating endless cycles, due to their atpicness, programming languages \u200b\u200bare not provided, so such cycles are created using structures designed to create ordinary (or conditional) Cycles. To ensure endless repetition, the validation of the condition in such a cycle is either missing (if syntax allows, as, for example, in the cycle Loop ... End Loop Hell's language), or is replaced by a constant value ( while True Do ... in Pascal).

Cycle with precondition

The cycle with the precondition is a cycle that is currently true for a certain condition specified before it starts. This condition is checked before The bodies of the cycle, so the body may not be fulfilled (if the condition is false from the very beginning). In most procedural programming languages \u200b\u200bis implemented by the operator whileFrom here it is the second name - the While cycle.

Cycle with postband

Cycle with post-cycle, in which the condition is checked after Body execution cycle. Hence it follows that the body always executed at least once. In Pascal language, this cycle implements the REPEAT..UNTIL operator; In C - Do ... While.

In the interpretation, the conditions of the cycle with the postband in different languages \u200b\u200bthere are differences. In Pascal and languages \u200b\u200bthat occurred from him, the condition of such a cycle is treated as output condition (the cycle is completed when the condition is true, in Russian terminology such cycles are called the "cycle to"), and in the SI and its descendants - as term condition (The cycle is completed when the condition is false, such cycles are sometimes called "cycle while") ... ..

Cycle with a middle

The cycle with the output from the middle is the most common form of the conditional cycle. Syntaxically, such a cycle is drawn up with the help of three designs: the start of the cycle, the end of the cycle and the output commands from the cycle. The design of the start labels the point of the program in which the cycle body begins, the end design is a point where the body ends. Inside the body should be present a command out of the cycle, when performing the cycle ends and control is transmitted to the operator, following the design of the end of the cycle. Naturally, the cycle is executed more than once, the output command must be invalid not definitely, but only when performing the condition from the cycle.

The fundamental difference of this type of cycle from discussed above is that part of the cycle body, located after the start of the cycle and to the output command, is always performed (even if the exit condition from the cycle is true at the first iteration), and a part of the cycle body, which is after the output command, Not performed at the last iteration.

It is easy to see that with the help of a cycle with an output from the middle, you can easily simulate and the cycle with the precondition (by placing the output command at the beginning of the cycle body), and the cycle with the postcondition (placing the output command at the end of the cycle body).

A part of the programming languages \u200b\u200bcontain special designs for organizing a cycle with a middle output. So, in the tongue of hell for this design Loop ... End Loop and the output command EXIT or EXIT WHEN.:

LOOP ... part of the body of the Exit WHEN cycle<условие выхода>; ... part of the body of the IF cycle<условие выхода> Then exit; End; ... part of the body of the End Loop cycle:

Here, inside the cycle there may be any number of commands to exit both types. The commands themselves do not differ in principle, usually EXIT WHEN. Apply when only the exit condition is checked, but simply EXIT - When the output from the cycle is made in one of the variants of a complex conditional operator.

In those languages \u200b\u200bwhere such structures are not provided, the cycle with an output from the middle can be modeled using any conditional cycle and the emergency exit operator from the cycle (such as break in si) or an unconditional transition operator goto..

Cycle Co meter

A cycle cycle - a cycle in which a certain variable changes its value from a given initial value to the final value with some step, and for each value of this variable, the cycle body is performed once. In most procedural programming languages \u200b\u200bis implemented by the operator forwhich specifies the counter (the so-called "variable cycle"), the required number of passes (or boundary value of the counter) and, possibly, the step with which the counter changes. For example, in Oberon-2 language, such a cycle has the form:

For V: \u003d B To. E. By S. Do. ... Cycle body End.

(Here V is the counter, B is the initial value of the counter, E is the boundary value of the counter, S - step).

The question of the value of the variable at the end of the cycle is ambiguous, in which this variable was used as a meter. For example, if in the program in Pascal, the design of the type will meet:

I: \u003d 100; For i: \u003d 0 to 9 Do Begin ... body cycle END; k: \u003d i;

the question arises: what value will be as a result of a variable k.: 9, 10, 100, maybe some other? And if the cycle is completed ahead of schedule? Answers depend on whether the meter value increases after the last iteration and the translator does not change this value additionally. Another question is: what will happen if the meter will be explicitly assigned a new meaning inside the cycle? Various programming languages \u200b\u200bsolve these issues in different ways. In some, the counter behavior is clearly regulated. In others, for example, in the same Pascal, the language standard does not determine the end value of the counter, nor the consequences of its explicit change in the cycle, but does not recommend changing the counter explicitly and use it upon completion of the cycle without re-initialization. The program on the Pascal Ignoring this recommendation can give different results when performing on different systems and the use of different translators.

The question is radically solved in the language of hell: the counter is considered to be described in the title of the cycle, and it simply does not exist. Even if the name of the counter in the program is already used, a separate variable is used inside the cycle as a counter. The meter is forbidden to explicitly assign any meaning, it can only change the internal mechanism of the cycle operator. As a result, the design

I: \u003d 100; For i in (0 ..9) Loop ... The body of the END LOOP cycle; k: \u003d i;

externally similar to the above cycle on Pascal, interpreted uniquely: variable k. A value will be assigned 100 because the variable i.used outside this cycle has nothing to do with the meter i.which is created and varies inside the cycle. It is believed that such a separation of the counter is most convenient and safe: a separate description is not required for it and the likelihood of random errors associated with the random destruction of the external to the variable cycle is minimal. If the programmer needs to be included in the ready-made cycle code with the meter, it may not check if there is a variable with the name it chose as a counter, do not add a description of the new meter to the title of the appropriate procedure, do not try to use one of the available but in this The moment of "free" counters. He simply writes a cycle with a meter variable, whose name is convenient for him, and it can be sure that no collision will happen.

A cycle with a counter can always be written as a conditional cycle, before starting which the counter is assigned the initial value, and the exit condition is the achievement of the end value meter; The operator of the meter changes to the specified step is added to the cycle body. However, the special cycle operators with the counter can be more efficiently broadcast, since the formalized type of such a cycle allows the use of special processor teams of cycles.

In some languages, for example, Si and others that occurred from it, cycle forDespite the syntax form of the cycle with the counter, in reality is a cycle with a precondition. That is, the cycle design:

For (i \u003d 0; i< 10 ; ++i) { ... тело цикла }

in fact, it is another form of design entry:

I \u003d 0; While (I.< 10 ) { ... тело цикла ++i; }

That is, in the design for First, the arbitrary cycle initialization proposal is written, then the continuation condition and finally, performed after each body of the cycle of some operation (it does not have to change the meter; it may be edited a pointer or some completely extraneous operation). For languages \u200b\u200bof this type, the above problem is solved very simply: the meter variable behaves completely predictably and upon completion of the cycle saves its last value.

Nested cycles

It is possible to organize a cycle inside the body of another cycle. Such a cycle will be called nested cycle. The invested cycle in relation to the cycle in the body of which it is invested will be referred to internal cycleand on the contrary the cycle in the body of which there is an invested cycle will be referred to external in relation to the invested. Inside the invested cycle, in turn, another cycle may be invested, forming the following neves level level etc. The number of nesting levels is usually not limited.

The total number of extensions of the inner cycle body does not exceed the product of the number of iterations of the internal and all external cycles. For example, taking three cycles invested in each other, each of 10 iterations, we obtain 10 body versions for the external cycle, 100 for the second level cycle and 1000 in the inner cycle itself.

One of the problems associated with nested cycles is the organization of early exit of them. In many programming languages \u200b\u200bthere is an emergency operator of the cycle ( break in si eXIT In Turbo Pascal, last. In Perl, etc.), but it, as a rule, provides a way out from the cycle of the level from where it is called. Calling it from the attached cycle will be completed only by this internal cycle, the cycle will continue to be performed. The problem may seem contrived, but it really sometimes occurs when programming the complex data processing when the algorithm requires immediate interrupt under certain conditions, the presence of which can be checked only in a deeply attached cycle.

Solutions of the problem of exiting nested cycles.

  • Simplest - use the unconditional transition operator goto. To go to the point of the program, immediately following the nested cycle. This option is criticized by supporters of structural programming, as well as all structures requiring use. goto.. Some programming languages, such as Modula-2, simply do not have an unconditional transition operator, and this design is impossible.
  • Alternative - use regular means of completing cycles, if necessary, establishing special flags that require immediate completion of the processing. Disadvantage - complication of code, reduced performance without any advantages, except the theoretical "correctness" due to the refusal of goto..
  • Placing the invested cycle in the procedure. The idea is that the whole action that may be necessary to interrupt early, to be issued as a separate procedure, and to use the output operator from the procedure (if there is such a programming language). In si, for example, you can build a function with a nested cycle, and the output from it is organized using the operator return.. Disadvantage - the selection of the code fragment in the procedure is not always logically justified, and not all languages \u200b\u200bhave regular means of early completion of procedures.
  • Take advantage of the mechanism for generating and processing exceptions (exclusive situations), which is now available in most Java. In this case, in an abnormal situation, the code in the attached cycle excites the exception, and the exclusion processing unit in which the entire nested cycle is placed, intercepts and processes it. The disadvantage is the implementation of the exception handling mechanism in most cases such that the speed of the program is reduced. True, in modern conditions, this is not particularly important: practically loss of performance is so small, which matters only for very few applications.
  • Finally, there are special language tools to exit nested cycles. Thus, in the tongue of hell, the programmer can mark the cycle (top level of the invested cycle) by the label, and in the command of the early completion of the cycle, specify this label. The output will not happen from the current cycle, but from all nested cycles to the marked, inclusive.

Joint cycle

Another cycle is a cycle that sets the execution of some operation for objects from a given set, without explicitly specifying the order of transferring these objects. Such cycles are called joint (as well as cycles on the collection, view cycles) And represent a formal record of the order instruction: "Perform an operation X for all elements included in the set M". The joint cycle, theoretically, does not define in any way, in what order the operation will be applied to the elements of the set, although specific programming languages, of course, can specify the specific procedure for the bustling of the elements. The arbitrary makes it possible to optimize the execution of the cycle due to the organization of access not in the specified programmer, but in the best basis. If there is a parallel execution of several operations, it is possible to even parallelize the joint cycle, when the same operation is simultaneously performed on different computing modules for different objects, despite the fact that the program remains sequential.

}

Cycle operator while Organizes the execution of one operator (simple or composite) unknown in advance the number of times.

Wheel cycle format: while (b) s;

where B.

S.- Cycle body - operator (simple or composite).

Before each execution of the cycle body, the value of the expression is analyzed in: if it is true, the cycle body is performed, and the control is transmitted to the reassessment of the condition in; If the value in the false cycle is completed and the control is transmitted to the operator following the S. operator

If the result of expression B is false at the first check, the cycle body will not be completed. Note that if the condition b during the cycle operation, it will not be changed, then a situation of looping is possible, that is, the impossibility of exiting the cycle. Therefore, inside the body should be operators leading to a change in the expression value b so that the cycle can be correctly completed.

Example:

n.

static Void Main ()

Console.Write ("n \u003d");

while (I.<= n) // While i less than or equal to n

// Display i on the screen, then increase it on 1

Console.Write ("+ i ++);

Result:

1 2 3 4 5 6 7 8 9 10

Cycle with postband

Cycle operator do while Also organizes the execution of one operator (simple or composite) unknown in advance the number of times. However, in contrast to the WHILE cycle, the condition of the cycle is checked after the bodies of the cycle.

DO WHILE cycle format: dO S While (b);

where IN - the expression, the truth of which is checked (the condition of the completion of the cycle);

S. - Cycle body - operator (simple or block).

First, the operator S is performed, and then the value of the expression is analyzed in: if it is true, the control is transmitted to the operator S if the false cycle is completed, and the control is transmitted to the operator, following the condition B. Since the condition is checked after the bodies of the cycle, In any case, the cycle body will be performed at least once.

In the operator Do While, as well as in the WHILE statement, a situation of looping is possible if the condition in will always remain true.

Example:

Output to the screen of integers from the interval from 1 to n.

static Void Main ()

Console.Write ("n \u003d"); int n \u003d int.parse (Console.ReadLine ());

do Console.Write ("" + i ++);

while (I.<= n); //пока i меньше или равно n

Cycle with parameter

The cycle with the parameter has the following structure:

for (<инициализация>; <выражение>; <модификация>) <оператор>;

Initializationused to declare and / or assign initial values \u200b\u200bof the values \u200b\u200bused in the cycle as parameters (counters). In this part, you can write several operators separated by the comma. The range of variables declared in terms of the cycle initialization is the cycle and nested blocks. Initialization is performed once at the beginning of the cycle execution.

Expression Determines the condition of execution of the cycle: if its result is true, the cycle is performed. The truth of the expression is checked before each cycle body execution, thus, the cycle with the parameter is implemented as a cycle with the precondition. In the block, the expression through the comma can be written several logical expressions, then the comma is equivalent to the operation of logical and (&&).

Modificationit is performed after each iteration of the cycle and it is usually used to change the cycle parameters. In terms of modification, you can write several operators through the comma.

Operator(Simple or composite) is a cycle body.

Any of the parts of the operator for (Initialization, expression, modification, operator) may be absent, but a point with a comma, which determines the position of the transmitted part, must be left.

static Void Main ()

Console.Write ("n \u003d");

int n \u003d int.parse (Console.ReadLine ());

for (int i \u003d 1; i<=n;) //блок модификации пустой

Console.Write ("+ i ++);

Nested cycles

Cycles may be simple or nested (multiple, cycles in the cycle). Invested cycles of any types: while, Do While, For. Each internal cycle must be fully attached to all external cycles. "The intersections" of cycles are not allowed.

As we said, there are 3 ways to organize a cycle (type) in Pascal:

2) Cycle with postcondition

3) cycle with precondition

Cycle In this article, consider the second type of cycle - the cycle with the precondition (While cycle). If the cycle with the meter we use in cases where it is necessary to organize a cycle with a known number of repetitions, the cycle with the precondition is used when the number of repetitions is unknown.

While condition do action; // Body Cycla

The cycle body is performed while the condition is true.

If there are several in the body of a cycle of action - operator brackets are used begin ... End.;

While condition do begin action_1; Action_2; Action_3; ... end;

Consider an example similar to the "cycle with a meter discussed in the topic, but we sell it with a cycle While.

Required on the screen to display:

Hello
Hello
Hello
Hello

To implement this example using a cycle with the above, we will need a variable n.:

While N.<4 do writeln("Привет");

This cycle will execute the command writeln ('Hi') Infinite number of times. Why? Because the variable n. It does not change and will always be less than 4. Therefore, it is necessary to add code in the cycle, changing the variable N. For example: n: \u003d n + 1.

While N.<4 do begin writeln("Привет"); n:=n+1; end;

Now the variable n. will change with each execution of the bodies of the cycle (with each iteration).

Need to remember: so that the loop does not occur in the cycle While - It is necessary to ensure that the variable from the condition changes in the cycle body. In the cycle with a meter, this will not happen (looping), because We specify the exact number of iterations.

Consider several tasks where the number of repetitions in the cycle is clearly not known.

Solution This task is based on the use of the WHILE cycle, because We do not know when zero will be introduced and we will stop entering numbers.

VAR A, S: Integer; Begin S: \u003d 0; writeln ("Enter the number"); readln (a); While (A.<>0) Do Begin S: \u003d S + A; // count the sum s Writeln ("Enter the number"); readln (a); end; Writeln (s); end.

Why do you use the input A (READLN (A);)? For the first time we enter a number A in order to enter a cycle with a certain value of the variable A, which will be used in the WHILE cycle. The second time the READLN (A) command is used inside the cycle - we enter the number until you enter zero.

Task 2.

Two segments A and B (A\u003e B) are given. Without using multiplication and division operations, determine how many segments will fit into the segment A.

Consider the image:

Those. It can be seen from the drawing that we need to add length lengths A. Until the amount becomes larger than the length of the segment IN. This will help us with a cycle with a precondition - cycle while.

VAR A, B, S, K: Integer; Begin. writeln ("Enter the length of the segment A"); readln (a); writeln ("Enter the length of the b"); readln (b); k: \u003d 0; S: \u003d A; While S. k: \u003d k + 1; // Variable K considers the amount of cycle body S: \u003d S + A; // summarizes the length of the segment a end; writeln ("In the segment in contain", k, "segments A"); end.

Consider the work of the program on the example: A \u003d 5., B \u003d 21. . Reasoning We write to the table:

Task 3.

Using the Euclidean algorithm, find the node of the two numbers.

Consider the Euclide algorithm block diagram:

We write this algorithm with Pascal, based on this block diagram. As you can see, we have a cycle with a precondition (M\u003e N). Inside the cycle one more condition (M\u003e N). operator IF ... Then..

VAR M, N: Integer; Begin. Writeln ("Enter M and N"); readln (m, n); While M.<>N DO Begin IF M\u003e N THEN M: \u003d M-N ELSE N: \u003d N-M END; Write ("H0D \u003d", M) END.

Similarly, the task 2 can be verified by this algorithm, writing out the reasoning in the table.

Cycle operator whileit is called a cycle with precondition, since the condition of the cycle is checked before entering it, it has the following format: while (expression) body cycle;

As an expression, it is allowed to use any expression of the SI language, and any operator, including empty or composite as a body. Scheme of execution of the operator while Next:

    An expression is calculated in brackets, then checks it.

    If the expression is false, then the execution of the operator whileends and the following operator is performed. If the expression is true, the operator block is performed (cycle body while).

    The process is repeated from paragraph 1.

Cycle block diagram with precondition.

Thus, the block of operators next to while Will be performed while checking the condition gives the result other than zero, true. (true). Of course, after the exit under the condition, the implementation of the program will continue from the operator following the cycle.

Consider the following fragment of the program:

int.x.=0;

while (X.<10)

(PrintF ("% d \\ n", x);

x ++;

printf ("Theirend. ");

Such a cycle will be executed 10 times by typing values h. from 0 before 9 and only after that the management will go to the second standing behind the cycle printF ().

Cycle with postcondition (operator Do ... While)

On behavior it is very similar to the previous cycle while ()During the exception that the condition is verified after the bodies of the cycle. If the condition turns out to be false, the cycle ends with this, otherwise, the next iteration of the cycle is performed. This type of cycle is used in cases where it is necessary to perform a cycle body at least once. The format of the operator has the following form: do. Cycle body while (expression);

Cycle block diagram with postband.

To interrupt the execution of the cycle before the condition becomes false, you can use the operator break. Print values h. from 0 before 9 using cycle do ... While

int.x.=0;

(PrintF ("% d \\ n", x);

) While (x<10);

printf ("The End");

Example: This cycle will be executed once before it determines that h. Equally zero.

int x \u003d 0;

printf ("h.\u003d% d \\ n ", x);

} while (x.!=0);

Cycle with meter (Operator for)

Operator for- This is the most common way to organize a cycle. It has the following format:

for (expression 1; expression 2; expression 3) cycle body

Expression 1. Usually used to set the initial values \u200b\u200bof the variables used in the cycle ("initialization unit"). Expression 2. Determines the condition in which the cycle body will be performed. Expression 3. Determines regular changes of the variables after each cycle body pass ("Modification unit").

A block diagram of a step-by-step cycle.

Scheme of execution of the operator for:

    An expression is calculated 1, once before entering the cycle.

    Expression 2 is calculated (before each cycle pass), if it is different from zero - true.(truth), then the cycle body is performed, otherwise (if the expression is false) - the cycle stops and the control is transmitted to the operator following the operator for.

    The expression 3 is calculated (modifying the data after each iteration of the cycle), the transition to paragraph 2.

It is essential that the testing of the condition is performed at the beginning of the cycle, which means that the cycle body may never be executed if the first check will be false.

Example: This program, like the two previous, print numbers from 0 before 9

for (x \u003d 0; x<10; x++)

printf.("% d.\ n.", x.);

You can omit any of the three parts of the loop header, or even all three, but it is impossible to lower the points with commas.

Example: This is how the same algorithm will look like the same algorithm with the missing parts of initialization and modification in the cycle. for:

for (x<10 ;)

(PrintF ("% d \\ n", x);

As you can see x \u003d 0.now stands before the cycle, and the operator x ++. He became the last in the cycle body.

You can omit and the exit condition, then the program will assume that the condition is always true and will print the "Hello" string to infinity.

for (;;)

printf.(" hello.\ n.");

similar infinite cycle while Will look like this:

while (1)

printF ("Hello \\ n");

while (1) -etochemot operator entry While (1! \u003d 0), that is, verification conditions 1!=0 will always give the result true.

Infinite cycles are used when the output condition from the cycle is not known in advance, to exit such a cycle requires an additional condition in the cycle body and operator. break.

Example: Calculate the squares of natural numbers from 0 before k.where k. Entered from the keyboard.

#Include.

using Namespace STD;

void Main ()

(int i \u003d 0, b, k;

cout.<<" k.="; // Text output "k \u003d" on the display screen

cin.>> k.; // entering data from the keyboard to the variable k.

cout.<< endl; // row translation (output of the "end of the string" on the screen)

for (;;)

(i \u003d i + 1;

Cout.<

if (i.== k.) break; // Exit the cycle by condition

Complicating the condition of the task, let it be necessary at the beginning of the cycle to set the initial values \u200b\u200bnot one, and two variables (let's say x. and y.), and in the cycle to change the values \u200b\u200bof them both. You can make this work as follows:

int y, x \u003d 0;

for (y \u003d 10; x

Thanks to the operator " , "(Comma) can be done easier:

int y, x;

for (x \u003d 0, y \u003d 10; x

(PrintF ("x \u003d% d \\ ty \u003d% d \\ n", x, y);