One Loop And Two Loops Handles all cases in C Program

One Loop And Two Loops Handles all cases in C Programming Language ?


Solution:

  1. /** option 1: one loop handles all cases */
  2.  
  3. switch = read_switch();
  4.  
  5. for (light = 0; i < 8; light++) {
  6.     if (switch == 0 || light <= switch) {
  7.         light_on(light);
  8.         delay();
  9.         light_off(light);
  10.     }
  11. }
  12.  
  13. /** option 2: two loops */
  14.  
  15. switch = read_switch();
  16.  
  17. if (switch != 0) {
  18.     for (light = 0; light < 8; light++) {
  19.         light_on(light);
  20.         delay();
  21.         light_off(light);
  22.     }
  23. } else {
  24.     for (light = 0; light <= switch; light++) {
  25.         light_on(light);
  26.         delay();
  27.         light_off(light);
  28.     }
  29. }


Learn More :