One Loop And Two Loops Handles all cases in C Programming Language ?
Solution:
- /** option 1: one loop handles all cases */
- switch = read_switch();
- for (light = 0; i < 8; light++) {
- if (switch == 0 || light <= switch) {
- light_on(light);
- delay();
- light_off(light);
- }
- }
- /** option 2: two loops */
- switch = read_switch();
- if (switch != 0) {
- for (light = 0; light < 8; light++) {
- light_on(light);
- delay();
- light_off(light);
- }
- } else {
- for (light = 0; light <= switch; light++) {
- light_on(light);
- delay();
- light_off(light);
- }
- }