You get a bonus - 1 coin for daily activity. Now you have 1 coin

Cycle operators in Java. Operator continue and label. Break operator

Lecture



Cycle operators

The main loop operator, the while statement, looks like this:

while (log) operator

First, the logical expression LogVyr is calculated; if its value is true, then the statement that forms the loop is executed. Then, the log expression is calculated again and the operator acts, and so on until false is obtained. If logVyr initially equals false, then the statement will not be executed once. A preliminary check ensures the safety of the cycle, avoids overflow, division by zero and other troubles. Therefore, the while statement is the main, and in some languages, the only loop operator.

The statement in the loop may be empty, for example, the following code fragment:

int i = 0;

double s = 0.0;

while ((s + = 1.0 / ++ i) <10);

calculates the number i of additions that must be done so that the harmonic sum s reaches a value of 10. Such a style is characteristic of the C language. Do not get involved in it so as not to turn the program text into an encryption that you yourself will look at in a couple of weeks with bewilderment.

You can organize and infinite loop:

while (true) statement

Of course, from such a cycle it is necessary to provide some way out, for example, by the break operator, as in Listing 1.5. Otherwise, the program will loop, and you will have to stop its execution with a combination of three fingers <Ctrl> + <Alt> + <Del> on MS Windows 95/98 / ME, a combination of <Ctrl> + <C> on UNIX or Task Manager in Windows NT / 2000.

If you need to include several statements in the loop, then you should form a block of {} statements

The second loop operator, the do-while operator, has the form do, the while operator (logVyr)

Here, the operator is first executed, and then the logical expression log is computed. The loop runs as long as logging remains true.

Connoisseurs of Pascal

In the do-while loop, the continuation condition is checked, not the end of the loop.

The only significant difference between these two loop statements is that in the do-while loop, the statement will be executed at least once.

For example, let some function f (x) be given, having on an interval, [o; B] exactly one root. Listing 1.5 shows a program that calculates this root approximately by the method of dividing it in half (bisection, dichotomy).

Listing 1.5. Finding the root of a nonlinear equation by bisection method

class Bisection {

static double f (double x) {

return x * x * x - 3 * x * x +3; // Or something different

}

public static void main (String!] args) {

double a = 0.0, b = 1.5, s, y, eps = le-8;

do {

c = 0.5 * (a + b); y = f (s);

if (Math.abs (y) <eps) break;

// The root is found. We leave cycle

// If at the ends of the segment [a; with]

// function has different signs:

if (f (a) * y <0.0) b = c;

// So the root is here. Transfer point b to point with

//Otherwise:

else a * s;

// Transfer point a to point with

// Continue until segment [a; B] will not be small

} while (Math, abs (ba)> = eps);

System.out.println ("x =" + c + ", f (" + c + ") =" + y);

}

}

The Bisection class is more complicated than the previous examples: in addition to the main () method there is also a method for calculating the function f (x). Here, the f o method is very simple: it calculates the value of a polynomial and returns it as the value of a function, all this being done by one operator:

return expression

In the main about method, another new break statement appeared, which simply stops the execution of the cycle if, by luck, we stumbled upon an approximate root value. The attentive reader also noticed the appearance of the static modifier in the declaration of the f () method. It is necessary because the method f o is called from the static method main o.

The third loop statement, the for statement, looks like this:

for ( List of Logs ; LogNr; SlisokVyr2) Operator

Before executing the loop, the list of expressions is calculated. These are zero or more expressions, separated by commas. They are calculated from left to right, and in the next expression you can already use the result of the previous expression. As a rule, initial values ​​are set here for the loop variables.

Then the logical expression log is computed. If it is true, true, then the operator acts, then the expressions from the list of expressions in the list Vyr2 are calculated from left to right . Then logvir is checked again. If it is

muddy, then the statement and the list of Exe2 are executed , etc. As soon as logyyr becomes equal to false, the execution of the loop ends.

In short, a sequence of statements is executed.

List Vyr1; while (logspace) {

operator

Vyr2; }

with the exception that if the operator in the loop is the operator

continue, then the sliskvr2 is still executed.

Instead of the list, Pyr1 can have one definition of variables with an initial value. Such variables are known only within this cycle.

Any part of the for statement may be missing: the loop may be empty, the expressions in the header, too, with semicolons being preserved. You can set an infinite loop:

for (;;) operator

In this case, in the body of the cycle should provide some way out.

Although there are great opportunities in the for statement, it is used mainly for enumerations when their number is known, for example, a code fragment,

int s = 0;

for (int k = 1; k <= N; k ++) s + = k * k;

// Here, the variable k is already unknown

computes the sum of the squares of the first N natural numbers.

Operator continue and label

The continue statement is used only in loop statements. It has two forms. The first form consists only of the word continue and implements an immediate transition to the next iteration of the loop. In the next code fragment, the continue statement allows you to bypass the division by zero:

for (int i = 0; i <N; i ++) {

if (i '== j) continue;

s + = 1.0 / (i - j);

}

The second form contains a label:

continue label

the label is written, like all identifiers, from Java letters, numbers and underscores, but does not require any description. The label is placed before the operator or opening brace and separated from them by a colon. This is how a tagged statement or tagged block appears.

Connoisseurs of Pascal

The label does not require a description and can not begin with a number.

The second form is used only in the case of several nested loops for an immediate transition to the next iteration of one of the inclusive loops, namely, the marked loop.

Break operator

The break statement is used in loop operators and the variant operator for immediate exit from these structures.

Break statement label

used inside a looped statement, a variant statement, or a tagged block to immediately exit these statements. The following diagram explains this construction.

Ml: {// External block

M2: {// Nested block - second level

M3: {// The third level of nesting ...

if (something happened) break M2;

// If true, nothing is done here.

}

// nothing is done here either

}

// Control is passed here.

}

At first, it is confusing that the label is placed in front of the unit or operator, and control is transferred for this unit or operator. Therefore, you should not get involved in the break operator with a label.

created: 2014-10-09
updated: 2024-11-14
307



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming