How many times does the code give a digital HIGH signal at pin1? [code] int pin1=11; int x=3; void setup() { pinMode(pin1,OUTPUT); } void

How many times does the code give a digital HIGH signal at pin1?
Code:
int pin1=11;
int x=3;
void setup() {
    pinMode(pin1,OUTPUT);
}
void loop() {
    for(int i=x;i<6;i++) {
        if(i%2==0) {
            x=x+1;
            loop();
        }
        else {
            digitalWrite(pin1,HIGH);
            digitalWrite(pin1,LOW);
        }
    }
}
A. 1
B. 2
C. 3
D. 4
 
Answer: B
Explanation: The code takes the global variable ‘x’ and sets it as the lower limit or the starting point for the for-loop. Then whenever the value of ‘x’ becomes an even number it increments the value of ‘x’ and then calls the loop() function again thus the control returns to line 7 and starts executing from there. This is an example for nested loops using both recursive and iterative methods.
 

Tìm kiếm

Back
Top