I can’t understand what’s wrong with this program. It’s not stopping at i<=number
public class AddNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 10;
for (int i = 1; i <= number; ++i) {
number +=i;
}
System.out.println(number);
}
Hi,
The condition to get out of the loop is i <= number.
Every loop increment i by 1.
Every loop increment number by i.
So basically number grows faster than i.
You’ll never meet the condition to exit the loop.
Regards.
EDIT: I did a fiddle to check and in practice it stops at some time probably because of the type boundaries.