Theory
Common mistakes
Mistakes with conditions and loops would often compile without any warning. The course flags catch most of them, so learn to recognise the messages.
Common mistakes
=instead of==.suggest parentheses around assignment used as truth value: there is an assignment in the condition.;afterif.if (x >= 5);: the condition ends at the;, and the{ }always runs. GCC:suggest braces around empty body in an 'if' statement.;afterfororwhile.for (...);spins an empty loop, and the body runs once after it. GCC:this 'for' clause does not guard....- A forgotten
break.this statement may fall through: withoutbreak, the nextcase's code runs too. 5 <= x <= 10.comparisons like 'X<=Y<=Z' do not have their mathematical meaning. Write5 <= x && x <= 10.- A loop without an end. The program "hangs": nothing inside the loop changes the condition (a forgotten
i++or a newread_int). Stop it with ■ (Ctrl+F2). - One time too many or too few.
for (int i = 0; i <= n; i++)repeats n + 1 times. From 0 usei < n, from 1 usei <= n. - Division by 0. When the loop didn't run even once,
sum / countdivides by 0. Checkcountbefore the loop.