How many times will the body of the loop be executed?
var Total = 1;
for (intIndex = 1; intIndex < 7 ; intIndex += 2)
{
Total += intIndex;
}
Answer of this question given below:-
var Total = 1;
for (intIndex = 1; intIndex < 7 ; intIndex += 2)
{
Total += intIndex;
}
For this code, The body of the loop be executed 3 times.
1st time :- intIndex =1
so, Total =Total + intIndex = 1+1 =2
2nd time :- intIndex =3 (intTndex=intIndex +2=1+2=3 and 3<7)
so, Total =Total + intIndex = 2+3 =5
3rd time :- intIndex =5 (intTndex=intIndex +2=3+2=5 and 5<7)
so, Total =Total + intIndex = 5+5 =10
Now,intIndex = intIndex+2=5+2 =7 then intIndex<7 condition not satisfy.
so,loop will be terminated.
Thus,The body of the loop be executed 3 times.
Get Answers For Free
Most questions answered within 1 hours.