Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

9.6. The conditional operator ?:

The conditional operator ?: takes three arguments - expressions. It is used in the following way: e1 ? e2 : e3 The expression e1 must be of boolean type. The other expressions (e2 and e3) must be of the same type or of types convertible to some common type. The expression built with the conditional operator is called the conditional expression. It is evaluated as follows. The expression e1 is evaluated. If it is true then the expression e2 is evaluated and its value becomes the value of the whole expression. The expression e3 is ignored in such case. If e1 is false, then the expression e2 is ignored and the value of the e3 becomes the value of the whole conditional expression. The precedence of the ?: operator is low (but higher than the assignment operators). It is right-associative. Low precedence allows omitting parentheses although it is worth using them for greater clarity. The conditional expression may replace the if-else statement in many simple cases:

  In the context:       int a = 1,d;       float b = 0.7, c;       .....       int func1() {...}       int func2(int) { ...}       void func4() { ... }

The if statement

Corresponding conditional expression

if (a > b) c = a + 2; else c = b;

c = a > b ? a + 2 : b;

if (a < b) c = a + 2; else d = b + 2;

a < b ? c = a + 2 : d = b + 2;

if (b>0) d= func2(a) + 2; else b *= 3.3;

b > 0 ? d = func2(a) + 2 : b *= 3.3;

if (b > 0) d = func1(); else a++;

b > 0 ? d = func1() : a++;

if (b > 0) func4(); else d = a + 7;

This cannot be noted as a conditional expression because the operand e2 is of type void whereas the operand e3 is of type int.

As we see, the replacement of the if statement with a conditional expression is not always possible. But the conditional operator is not meant to replace the if statement. The main advantage of the conditional operator is that it may be apllied where the if statement cannot or where the application of the if statement would seem awkward. Consider the initialization of a variable (a common example):             int x = (a > b ? 1 : 0);  

9.7. Summary

The lecture discusses in detail the statements and the operators designed to make decisions in a program. They constitute the essence of programming.

9.8. Exercises

  1. Write a program which would translate the numeric grades (A, B, C, D, E, F) given in input dialog into descriptive grades:

    1. excellent

    2. good

    3. fair

    4. poor

    5. below standard

    6. fail

Separate data entering from translation and result writing, protect the program from entry errors, by informing a user that he or she has entered improper data and asking for the correction of that data.

  1. Modify the class Publication from the lectures 7-8 so as to make setting of the non-positive prices and setting of non-positive quantities of purchase and sale impossible. How a program should react on such errors? In a test program assure interactive (using dialogs) creation of publication objects (with verification of the correctness of input data), setting their prices, purchase and sale of publications and reporting about changes in the bookshop.

  2. Write a program that checks whether the given number fits in on of the intervals: 1-10, 11-100, 101-1000, 1001-10000 or is less than 0 or greater than 10000.

  3. Write a program asking the user to input 5 strings and checking whether there are two equal among them. It should print the number of equal strings.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]