JavaScript is required

In the following program how long will the for loop get executed?
Media VietJack

A.
The for loop would not get executed at all
B.
The for loop would get executed only once
C.
The for loop would get executed 5 times
D.
The for loop would get executed infinite times
Trả lời:

Đáp án đúng: D


The for loop initializes `i` to 0. The loop continues indefinitely until the `break` statement is encountered. The `if` statement checks if `i` is greater than 5. The loop executes as follows: - i = 0: `i > 5` is false. - i = 1: `i > 5` is false. - i = 2: `i > 5` is false. - i = 3: `i > 5` is false. - i = 4: `i > 5` is false. - i = 5: `i > 5` is false. - i = 6: `i > 5` is true, so the `break` statement is executed. The loop executes 6 times (for i = 0, 1, 2, 3, 4, 5). Therefore, the closest answer is 5 times.

Câu hỏi liên quan