Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторні, на англійській.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
605 Кб
Скачать
☆

1.7. The continue Statement

The continue statement terminates the current iteration of a loop and instead jumps to the next iteration. It applies to the loop immediately enclosing the continue statement. It is an error to use the continue statement outside a loop.

In while and do loops, the next iteration commences from the loop condition. In a for loop, the next iteration commences from the loop’s third expression. For example, a loop which repeatedly reads in a number, processes it but ignores negative numbers, and terminates when the number is zero, may be expressed as:

do {

cin >> num;

if (num < 0) continue;

// process num here...

} while (num != 0);

When the continue statement appears inside nested loops, it applies to the loop immediately enclosing it, and not to the outer loops.

1.8. The break Statement

A break statement may appear inside a loop (while, do, or for) or a switch statement. It causes a jump out of these constructs, and hence terminates them. Like the continue statement, a break statement only applies to the loop or switch immediately enclosing it. It is an error to use the break statement outside a loop or a switch. For example, suppose we wish to read in a user password, but would like to allow the user a limited number of attempts:

for (i = 0; i < attempts; ++i) {

cout << "Please enter your password: ";

cin >> password;

if (Verify(password)) // check password for correctness

break; // drop out of the loop

cout << "Incorrect!\n";

}

Here we have assumed that there is a function called Verify which checks a password and returns true if it is correct, and false otherwise.

1.9. The goto Statement

The goto statement provides the lowest-level of jumping. It has the general form:

goto label;

where label is an identifier which marks the jump destination of goto. The label should be followed by a colon and appear before a statement within the same function as the goto statement itself. For example, the role of the break statement in the for loop in the previous section can be emulated by a goto:

for (i = 0; i < attempts; ++i) {

cout << "Please enter your password: ";

cin >> password;

if (Verify(password)) // check password for correctness

goto out; // drop out of the loop

cout << "Incorrect!\n";

}

out:

//etc...

Because goto provides a free and unstructured form of jumping (unlike break and continue), it can be easily misused. Most programmers these days avoid using it altogether in favor of clear programming.

1.10. The return Statement

The return statement enables a function to return a value to its caller. It has the general form:

return expression;

where expression denotes the value returned by the function. The type of this value should match the return type of the function. For a function whose return type is void, expression should be empty:

return;

The only function we have discussed so far is main, whose return type is always int. The return value of main is what the program returns to the operating system when it completes its execution. Under UNIX, for example, it its conventional to return 0 from main when the program executes without errors. Otherwise, a non-zero error code is returned. For example:

int main (void) {

cout << "Hello World\n";

return 0;

}

When a function has a non-void return value (as in the above example), failing to return a value will result in a compiler warning.