Study notes: Initiation à la programmation

| updated

Notes from the edX course Initiation à la programmation

Contents

  1. Basis of programming: operations + data
    1. Variables
    2. Values of expressions
    3. Initialisation
    4. Numeric sizes
    5. ‘const’ vs. ‘constexpr’
  2. Control structures
    1. ‘switch’ as alternative to if-else chains
    2. ‘for’ loops
    3. ‘while’ loops
    4. Variable scope

Basis of programming: operations + data

operations:

data:

Variables

Uninitialised variables have undefined value, e.g. in int x;, x ≠ 0.

Assigning a double to an int truncates it silently.

Values of expressions

Assignment

The effect of a = expression is assignment, but its value is the value of the rval (what value gets assigned). Thus e.g. i = j = 3; means i = (j = 3);, which has the same effect as j = 3; i = 3;.

Better not to use the value though: this construct mixes side effect and eval/return.

Incrementing

Effect of i++; is identical to ++i;, but the value differs:

This difference is only noticeable if you use the value somehow (as opposed to the effect). Better not to anyway, since the expression then has both an effect and side effect.

Always use the ++i form:

Initialisation

Init by copy

int x = 1;

Direct init

int x(1);

Newer syntax

int x{1};

Numeric sizes

See limits.h to check limits on given compiler/arch combination: limits::numeric_limits<int>::max()

The above can all be prefixed with unsigned.

‘const’ vs. ‘constexpr’

const int i(1); does not mean the value in memory is constant, since it can still be modified by assigning a new value via a pointer to i. But not via the name i: i is read-only.

Variables declared as const need not be known at compile time; it can be initialised from user input, e.g. int read; cin >> read; const int i (read);, to signal that i will not be modified later on.

constexpr (variable or expression) means constant and must be known at compile time, e.g. constexpr double pi (3.1415);. Use this one whenever conditions are met.

Control structures

Be careful not to use assignment in the condition, e.g. if (x = 1) { }: assignment has a value (coerced to bool).

Lazy evaluation of chained logical expressions (same as in Python): e.g. (i != 0) and (25/i > 12) is safe, since 25/i is not evaluated if i == 0.

‘switch’ as alternative to if-else chains

switch (expr) {
  case 1: stmt 1; break;
  case 2:
  case 3: stmt 2; break;
  default: stmt;
}

Note: compiler does not check for non-exhaustive matches!

‘for’ loops

for (int i(0) ; i < 5 ; ++i ) { ...  }
//   decl+init  cond    incr    body

i is only in scope until the end of the loop.

Since C++ 11:

for (auto x : xs) { ... }

to iterate over elements in a collection.

‘while’ loops

Run body one or more times:

do {
  cout << "Provide n > 0";
  cin >> n;
} while (n <= 0);

Run body 0 or more times (none if cond is initially false):

while (n < 10) {
  ++n;
}

Use:

Variable scope

Until the end of the block in which it was declared – except for globals declared even outside main().

Can mask other variables of the same name with broader scope. Better to choose a different name, though, and to limit scope.