Standards-8
Conditionals


Major errors

Do not use if(x = y) [15 points each, 2% max]

Even if it is what you intend to write, do not use an assignment as the test in an if, while, do or for statement. If you ask for warnings (which you always should), g++ will warn you about this.

The body of an if-statement must not be a semicolon [5 points each, 2% max]

Never write
  if(condition);
Also, never write
  else;

Do not overuse tests [4-8 points each, 2% max]

Do not have special cases where they are not needed. For example, the following computes the maximum of two integers.
  int maximum(int x, int y)
  {
    if(x == y)
    {
      return x;
    }
    else if(y > x)
    {
      return y;
    }
    else
    {
      return x;
    }
  }
The special case for x = y is not needed. The function works perfectly well without it. So get rid of it.


Minor errors

Do not duplicate code at the beginning or end in the bodies of an if-statement [2 points for each duplicated line, 2% max]

Do not write an if-statement such as
  if(condition)
  {
    x = 1;
    ...
    return 0;
  }
  else
  {
    x = 1;
    ...
    return 0;
  }
Pull out the common parts, as in
  x = 1;
  if(condition)
  {
    ...
  }
  else
  {
    ...
  }
  return 0;

Do not use a ? b : c as a statement [4 points each, 2% max]

An expression of form a ? b : c should only be used to produce a value that the program uses.

Do not write an explicit empty else [2 point each, 1% max]

Instead of
  if(x > 0) 
  {
    doSomething();
  }
  else 
  {
  }
write
  if(x > 0) 
  {
    doSomething();
  }

Do not use an if-statement with an empty statement preceding else [4 points each, 1% max]

Instead of
  if(x > 0) 
  {
  }
  else 
  {
    doSomething();
  }
write
  if(x <= 0) 
  {
    doSomething();
  }

A loop body should not have the form

  if(...)
  {
     continue;
  }
  else
  {
    ...
  }
Statement continue; says to do the next iteration of the loop. But that is what the program would normally do at the end of the loop body.


Do not use conditions that are always true or always false [6 points each, 3% max]

Do not use an if-statement whose condition is always true or always false, except strictly for debugging or code-checking purposes. For example, if at a particular place it is not possible for i and k to have the same value, then do not say
  if(i == k) 
  {
    ...
  }

Do not use redundant tests in if-statements [6 points each, 3% max]

The else part of an if-statement is done if the condition being tested is false. Do not test whether it is false. For example,
  if(x > 0)
  {
    step1();
  }
  else if(x <= 0)
  {
    step2();
  }
should be replaced by
  if(x > 0)
  {
    step1();
  }
  else
  {
    step2();
  }

If code is only performed by one branch of an if-statement, then it must be written inside that branch [4 points each, 2% max]

Look at the following function definition.
  int demo(int x)
  {
    int y;
    if(x > 0)
    {
      y = x + 1;
    }
    else
    {
      return -x;
    }
    return y;
  }
Notice that statement return y can only be performed when x > 0. Moving it into the if-statement yields
  int demo(int x)
  {
    int y;
    if(x > 0)
    {
      y = x + 1;
      return y;
    }
    else
    {
      return -x;
    }
  }
A better form is
  int demo(int x)
  {
    if(x > 0)
    {
      return x + 1;
    }
    else
    {
      return -x;
    }
  }

Only use boolean operations on boolean values [4 points each, 1% max]

Boolean operations (!, && and ||) should only be used on boolean values (which are known to be 0 or 1). Do not use them on integers, pointers, etc. (But see the exception in the next entry.)

Conditions should be boolean [5 points each, 2% max]

The condition in an if-statement, while-statement, for-statement, do-statement, or any other construct that performs a yes/no test should be a boolean value. It should not be a number or a pointer.

An exception is an expression that uses the ctype library, such as isdigit((unsigned) c) and isalpha((unsigned) c). Those can be used in tests as boolean values.


Switch fallthrough [2 points each, 1% max]

Each case in a switch statement must logically end on a break statement or a return statement. (That means the case body must either perform a break or a return in all cases, not that the break or return must physically be the last statement.) You cannot let one case fall into the next. However, you are allowed to have multiple case labels that share a single block of code.