How many times will the following loop run if the input to the code is a constant 5V signal? [code] int pin1=10; void setup() { Serial.begin

Thành Nguyễn

New member
How many times will the following loop run if the input to the code is a constant 5V signal?
Code:
int pin1=10;
void setup() {
    Serial.begin(9600);
    pinMode(pin1,INPUT);
}
void loop() {
    for(int i=0;i<10;i--) {
        if(digitalRead(pin1)==1) {
            Serial.println(“HELLO”);
        }
    }
}
A. 1
B. 2
C. infinitely
D. 5
 
Answer: C
Explanation: The above code is going to run infinitely since the in the increment/decrement section the condition given is i--. Which literally means that the loop will execute until i becomes 10-1, and each time the loop executes, we decrement it by 1. So in effect, i will continuously become lesser and lesser and never be equal to 10-1.
 

Tìm kiếm

Back
Top