Next: Example Program: Wages Calculation Up: The if-else Statement Previous: The if-else Statement

Examples of if-else statements

The following if-else statement adds x to a sum of positive numbers and increments a count of positive numbers if it is positive. Similarly if x is negative it is added to a sum of negative numbers and a count of negative numbers is incremented.

if (x >= 0.0)
  {
    sumpos += x;
    poscount++;
  }
else
  {
    sumneg += x;
    negcount++;
  }



Next: Example Program: Wages Calculation Up: The if-else Statement Previous: The if-else Statement