While loop doubt | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.....

14th Jan 2019, 7:00 AM
KAAMIL AHAMADH S
KAAMIL AHAMADH S - avatar
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; }
14th Jan 2019, 7:25 AM
Danijel Ivanović
Danijel Ivanović - avatar
14th Jan 2019, 8:52 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
😊 You are welcome! 👍
14th Jan 2019, 9:06 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
14th Jan 2019, 8:27 AM
notqueued
notqueued - avatar
+ 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...
14th Jan 2019, 7:37 AM
KAAMIL AHAMADH S
KAAMIL AHAMADH S - avatar
+ 1
14th Jan 2019, 9:03 AM
KAAMIL AHAMADH S
KAAMIL AHAMADH S - avatar