skippart.c 1008 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* skippart.c -- uses continue to skip part of loop */
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5. const float MIN = 0.0f;
  6. const float MAX = 100.0f;
  7. float score;
  8. float total = 0.0f;
  9. int n = 0;
  10. float min = MAX;
  11. float max = MIN;
  12. printf("Enter the first score (q to quit): ");
  13. while (scanf("%f", &score) == 1)
  14. {
  15. if (score < MIN || score > MAX)
  16. {
  17. printf("%0.1f is an invalid value. Try again: ",
  18. score);
  19. continue; // jumps to while loop test condition
  20. }
  21. printf("Accepting %0.1f:\n", score);
  22. min = (score < min)? score: min;
  23. max = (score > max)? score: max;
  24. total += score;
  25. n++;
  26. printf("Enter next score (q to quit): ");
  27. }
  28. if (n > 0)
  29. {
  30. printf("Average of %d scores is %0.1f.\n", n, total / n);
  31. printf("Low = %0.1f, high = %0.1f\n", min, max);
  32. }
  33. else
  34. printf("No valid scores were entered.\n");
  35. return 0;
  36. }