+ 1
While loop doubt
Why while loop ended in 30 instead of 25 Since i used <=25 (in line 8) https://code.sololearn.com/cFb9OTmLOzt4/?ref=app Any help will be appreciated.....
6 Answers
+ 11
Kaamil Ahamadh S
If you want the loop finishing with 25, then write like this:
while (num <= 25){
System.out.print(num +" ");
num = num + 5;
}
+ 10
+ 8
đ You are welcome! đ
+ 5
when it iterates through the while loop it does something like this:
iteration 1: num = 0; num <= 25: true -> num = 0 + 5;
iteration 2: num = 5; num <= 25: true -> num = 5 + 5;
iteration 3: num = 10; num <= 25: true -> num = 10 + 5;
iteration 4: num = 15; num <= 25: true -> num = 15 + 5;
iteration 5: num = 20; num <= 25: true -> num = 20 + 5;
attention to the next iteration
iteration 6: num = 25; num <= 25: true -> num = 25 + 5;
iteration 7: num = 30; num <= 25: false -> stop the loop
+ 1
Danijel IvanoviÄ
Sir i have written same code only (what ur saying ) but while loop ends in 30 not 25...
if i change the code to
while (num < 25){
System.out.print(num +" ");
num = num + 5;
}
then it ends in 25...
so my doubt is when i used <=25 why it ends in 30 instead of 25...
+ 1
Danijel IvanoviÄ thanks....