What line prevents the loop from ending?
int var = 0; // 1
while ( var < 10) // 2
{
printf("%d\n", var); //3
var--;
}
Ans] line number 2 which prevents the loop from ending.
Explanation: because if you change condition of while loop then according to the condition while loop excute.now in above case while loop print the value of var 'n' time because every time while loop execute value of var will decrease.that means condition of while loop is right it prevents the loop from ending.
In above example value of var printing infinite time because var <10 and and inside loop body var--; that means for every iteration it decreases var value that less than 10 so it print var value continuously.
If we chage line 2 to
int var=0;
while(var<-10){
printf("%d\n", var); //3
var--;
}
above prgram print the value of var as
0
-1
-2
03
-4
-5
-6
-7
-8
-9
after that it terminate loop
or you can also change while loop condition as you want bute 2 line prevents the loop from ending.
Get Answers For Free
Most questions answered within 1 hours.