Skip to content
Topos Uranos

Summary

This lesson opens the fourth unit of the course, devoted to control flow. It explains what the unit is for: to teach how to govern the order in which the statements of a program are executed, with decisions and with loops, and how to reason about their correctness, that is, how to make sure that every case is handled, that every loop does what it should and that it terminates; it presents its four lessons and the thread that joins them; it recalls what the unit takes for granted, above all the compound conditions of unit 3; it formulates the problem the unit solves, that of a program that computes the greatest common divisor with a fixed number of steps and that, depending on the data, answers wrongly or stops at a division by zero; it anticipates the invariant as the tool with which the unit will justify its loops; 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 the compound conditions of unit 3 (the connectives, short-circuit evaluation and negation by De Morgan’s laws) and review them if necessary.
  3. Anticipate, with an example, the invariant as a reasoning tool: a relation among the variables that a repetition preserves and that, at the end, gives the result.
  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: the negation of a compound condition, short-circuit evaluation, the trace of a repetition from lesson 1.1 and the limit of an int. The next seven touch, in the order of the lessons, on the central ideas of the unit, and several of them start from school mathematics: the greatest common divisor, summation notation, the termination of a procedure. No one is obliged to get them right yet, and in some 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 unitsWhat is the negation of the condition 1 <= n && n <= 10?

  2. Question 2From earlier unitsWith int d{0}; and int n{5};, what happens when d != 0 && n / d > 2 is evaluated?

  3. Question 3From earlier unitsDivision by repeated subtraction from lesson 1.1 starts with q=0q = 0 and r=ar = a, and repeats “while r≥br \geq b, subtract bb from rr and add 1 to qq”. With a=20a = 20 and b=5b = 5, how many times is the body of the repetition executed?

  4. Question 4From earlier unitsWith a 32-bit int, what does the standard guarantee about the value of 46341 * 46341?

  5. Question 5A program contains the chain of decisions if (score >= 50), which writes “Passed”; else if (score >= 90), which writes “Excellent”; and else, which writes “Failed”. What does it write with score equal to 95?

  6. Question 6Which of the following expressions denotes the sum 1+2+⋯+n1 + 2 + \cdots + n?

  7. Question 7With int total{0};, what does std::println("{}", total); write after for (int i{0}; i < 5; ++i) { total += i; }?

  8. Question 8In division by repeated subtraction (q=0q = 0, r=ar = a; while r≥br \geq b, rr becomes r−br - b and qq becomes q+1q + 1), what relation among the variables holds every time the condition is evaluated?

  9. Question 9A procedure starts from an arbitrary integer nn (positive, zero or negative) and repeats “while n>0n > 0, subtract 2 from nn”. Does it terminate for every nn?

  10. Question 10What is the greatest common divisor of 84 and 36?

  11. Question 11To decide whether 101 is prime by trying divisors, up to which divisor is it enough to go?