Skip to content
Topos Uranos

Summary

This lesson opens the third unit of the course, devoted to expressions and their operators. It explains what the unit is for: to teach how to build expressions whose value can be predicted with certainty, knowing which operation is performed first, what type the result has, what side effects they have and which expressions have no meaning; it presents its four lessons and the thread that joins them, from integer and real arithmetic to bits and the math library, by way of conditions and the catalog of undefined behavior; it recalls what the unit takes for granted from the second, namely the type of each piece of data, its limits and its conversions; it formulates the problem the unit solves, that of a program that compiles without a single warning and whose answers to six school questions are all wrong, when they are not missing altogether; and it shows where it leads, in the following units and in the course Programming in C++ II.

Learning objectives

  1. Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
  2. Recall what is needed from unit 2 (the integer and real types, their ranges, overflow and conversions) and review it if necessary.
  3. Anticipate, with an example, that the value of an expression depends on evaluation rules (precedence, the type of the operands, the order of the effects) that do not always coincide with those of school mathematics.
  4. Recognize what one is expected to be able to do at the end of the unit, and distinguish it from what will be learned in the following units.

Entry assessment

Before working through the unit, it is worth measuring the starting point. This assessment is a self-assessment: it is not graded, and its only purpose is to guide your study. The first four questions check what the unit takes for granted from the second: division between integers, writing in base 2, unsigned subtraction and signed overflow. The next six touch, in the order of the lessons, on the central ideas of the unit, and several of them start from school mathematics or from elementary logic. No one is obliged to get them right yet, and in two of them “I don’t know yet” is an honest answer: each answer, right or not, explains the point and names the lesson that deals with it. The same skills are measured again at the end, in the synthesis lesson.

Entry assessment

Before starting the unit, measure your starting point. It is a short self-assessment: it is not graded and its result is stored only in your browser.

  1. Question 1From earlier unitsA program declares const double r{7 / 2}; and writes r. What appears?

  2. Question 2From earlier unitsHow is the number 13 written in base 2, with eight bits?

  3. Question 3From earlier unitsWhat does std::println("{}", 0u - 1); write with a 32-bit unsigned int?

  4. Question 4From earlier unitsA program executes int count{std::numeric_limits<int>::max()}; and then count = count + 1;. What does the standard guarantee about the value of count?

  5. Question 5What are the values of 2+3⋅42 + 3 \cdot 4 and (2+3)⋅4(2 + 3) \cdot 4?

  6. Question 6According to the division theorem of mathematics, with the remainder rr subject to 0≤r<20 \leq r < 2, what are the quotient qq and the remainder rr of −7-7 divided by 22?

  7. Question 7What is the negation of “it is raining and it is cold”?

  8. Question 8What does std::println("{}", 3 < 5); write?

  9. Question 9After int a{5}; and int b{a++ * 2};, what are the values of a and b?

  10. Question 10What does std::println("{}", 2 ^ 3); write?