How many times will the following loop runfor(c = 1; c < 20; c=c+5) {document.write("C : " + c );document.write("<br />");
Question
How many times will the following loop run?
for(c = 1; c < 20; c = c + 5) {
document.write("C : " + c );
document.write("<br />");
}
Solution
To determine how many times the loop will run, we need to analyze the loop condition and the increment step.
The loop condition is c < 20
, which means the loop will continue as long as the value of c
is less than 20.
The increment step is c = c + 5
, which means the value of c
will increase by 5 in each iteration of the loop.
Starting with c = 1
, the loop will run as follows:
-
Iteration 1:
c = 1
,c < 20
is true, so the loop runs. The output will be "C : 1". -
Iteration 2:
c = 6
,c < 20
is true, so the loop runs. The output will be "C : 6". -
Iteration 3:
c = 11
,c < 20
is true, so the loop runs. The output will be "C : 11". -
Iteration 4:
c = 16
,c < 20
is true, so the loop runs. The output will be "C : 16". -
Iteration 5:
c = 21
,c < 20
is false, so the loop terminates.
Therefore, the loop will run 4 times.
Similar Questions
How many times is the loop block entered when the following code executes?fоr(int i=16; і < 28; i+=2) { <code inside loop block>}
How many times will the following loop execute? What will be the final value?int a=1,x=0;do{x=a++ *a,}while(a<=5),System out.println(x);}
How many times will the printf statement be executed?main( ){unsigned int i=3; while( i >=0)printf( "%d", i--);}A0
How many times does the statement execute in the following code?for(int n = 0; n < 7; n++){ for(int m = 0; m < 7;m++){ statement; }}
___________________loops through a block of code a specified number of times0.5 Marksfor loopforeach loopBoth A and Cwhile loop
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.