+ 1

while loop

Could anyone explain the last question about while loop

1st Feb 2017, 3:26 AM
Manuel Paz
Manuel Paz - avatar
1 Odpowiedź
+ 1
syntax:- initialization; while(condition) {//body of the loop starts here.... .... statements... .... Increment/Decrements Operator }//body of the loop ends here... for example:- print values from 1 to 10 int i; i=1; //initialization of the variable i while(i<=10) //condition is i is less or equal to 10 { printf("%d\t",i); //prints the value of i i++; //increment in the value of i means i=i+1 } Output:- 1 2 3 4 5 6 7 8 9 10 In the above program first of all the value of i is initialized by 1 then the condition is check in while loop the condition is if i<=10 at this time the value of i is equal to 1 so (1<=10) is true means condition is true so compiler enters in the loop and execute next statement which prints the value of i which is 1 and then go to i++ operator which means i=i+1 (i=1+1) now i=2 and again condition is checked in while loop it continuous until condition is not true. /t is used for tab space in printf statement.
1st Feb 2017, 3:51 AM
Avnish Tomar (CCS University)
Avnish Tomar (CCS University) - avatar