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

1.2. The if Statement

It is sometimes desirable to make the execution of a statement dependent upon a condition being satisfied. The if statement provides a way of expressing this, the general form of which is:

if (expression)

statement;

First expression is evaluated. If the outcome is nonzero then statement is executed. Otherwise, nothing happens.

For example, when dividing two values, we may want to check that the denominator is nonzero:

if (count != 0)

average = sum / count;

To make multiple statements dependent on the same condition, we can use a compound statement:

if (balance > 0) {

interest = balance * creditRate;

balance += interest;

}

A variant form of the if statement allows us to specify two alternative statements: one which is executed if a condition is satisfied and one which is executed if the condition is not satisfied. This is called the if-else statement and has the general form:

if (expression)

statement1;

else

statement2;

First expression is evaluated. If the outcome is nonzero then statement1 is executed. Otherwise, statement2 is executed. For example:

if (balance > 0) {

interest = balance * creditRate;

balance += interest;

} else {

interest = balance * debitRate;

balance += interest;

}

If statements may be nested by having an if statement appear inside another if statement. For example:

if (callHour > 6) {

if (callDuration <= 5)

charge = callDuration * tarrif1;

else

charge = 5 * tarrif1 + (callDuration - 5) * tarrif2;

} else

charge = flatFee;

1.3. The switch Statement

The switch statement provides a way of choosing between a set of alternatives, based on the value of an expression. The general form of the switch statement is:

switch (expression) {

case constant1:

statements;

...

case constantn:

statements;

default:

statements;

}

First expression (called the switch tag) is evaluated, and the outcome is compared to each of the numeric constants (called case labels), in the order they appear, until a match is found. The statements following the matching case are then executed. Note the plural: each case may be followed by zero or more statements (not just one statement). Execution continues until either a break statement is encountered or all intervening statements until the end of the switch statement are executed. The final default case is optional and is exercised if none of the earlier cases provide a match.

For example, suppose we have parsed a binary arithmetic operation into its three components and stored these in variables operator, operand1, and operand2. The following switch statement performs the operation and stored the result in result.

switch (operator) {

case '+':

result = operand1 + operand2;

break;

case '-':

result = operand1 - operand2;

break;

case '*':

result = operand1 * operand2;

break;

case '/':

result = operand1 / operand2;

break;

default:

cout << "unknown operator: " << operator << '\n';

}

As illustrated by this example, it is usually necessary to include a break statement at the end of each case. The break terminates the switch statement by jumping to the very end of it. There are, however, situations in which it makes sense to have a case without a break.