+ 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
7 Answers
+ 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
+ 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 :)
+ 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 đ
+ 2
@Ipang , ty for your response ... well adding a counter is a nice idea đ if only we could see the loop and find out đ
+ 2
Good luck on your project mate :)
+ 1
@Ipang ty đ