if - else

Branches

If condition holds true, then we do this, else we do that

if (condition)
    this
else
    that

this und that are statements …

if (a < 0)
    a = -a;
else {
    a = a;
    fprintf(stderr, "alright\n");
}

True or False? What is Truth?

if (condition)
   ...
  • condition is an expression

  • An expression has a value

  • In if (other similar statements) its value is used as condition

  • 0 … condition does not hold ⟶ false

  • Everything else … ⟶ true

else is optional (1)

if may be followed by an else branch (but need not)

if (condition)
    if (another-condition)
        this
    else
        that

Ambiguity: where does the else branch belong?

else is optional (2)

Dangling else: compiler does not care about indentation ⟶ careful!

if (condition)
    if (another-condition)
        this
else
    that

Braces required!

if (condition) {
    if (another-condition)
        this
}
else
    that