+ 1

any easier way of understanding of WHILE loops in Java ? 😥

hey fellow sololearners ! I just wanted to know is there an easier way on how to calculate how many times a WHILE loop will execute ? they do climb on my nerves ... - ph4nt0m

22nd Oct 2017, 4:08 AM
ph4n70m
ph4n70m - avatar
7 odpowiedzi
+ 3
The while loop repeats its instructions as long as the specified condition remains true, and will stop when the condition evaluates to false. e.g. int n = 1; while(n <= 100) { n++; } The above example of while loop will repeat incrementing value of <n> *while* <n> is still less than or equal to 100. Hope that can clear the doubts a bit. Hth, cmiiw
22nd Oct 2017, 4:39 AM
Ipang
+ 3
@ph4n70m, oops, I missed your actual point in the question, sorry 8) Anyways, would adding a counter variable in the loop seem viable? that's what I used to do if I found doubts on loop count, I just put in a counter variable in the loop, increase its value on each repetition, and evaluate the final value after loop completed, lame as it maybe, it helps a bit I suppose :)
22nd Oct 2017, 6:32 AM
Ipang
+ 2
ty @Ipang , ik how incrementing/decrementing works ... but when debugging long programs or work with others on *big* projects , while loops are often complex ... so is there no easy way to quickly recognize how many times it'll iterate ? anyways ty for your answer 😄
22nd Oct 2017, 4:42 AM
ph4n70m
ph4n70m - avatar
+ 2
@Ipang , ty for your response ... well adding a counter is a nice idea 😄 if only we could see the loop and find out 😅
22nd Oct 2017, 6:43 AM
ph4n70m
ph4n70m - avatar
+ 2
Good luck on your project mate :)
22nd Oct 2017, 6:52 AM
Ipang
+ 1
@Ipang ty 😄
22nd Oct 2017, 6:53 AM
ph4n70m
ph4n70m - avatar