0

Can you run this program and explain me why get that kind of result?

class MyClass { public static void main(String[] args) { int a = 10; int b = 1; int i = 0; while (b <= a || i < 10){ System.out.println(b); b++; if(b == 5) b = 1; i++; } } }

22nd Aug 2020, 12:43 PM
Phyo Htet Aung
Phyo Htet Aung - avatar
1 Respuesta
+ 3
It is infinite loop, because of your condition Look, b=1 a=10 b<=a is true, b printed, (1 to 5 repeatedly) b++ when b becomes 5, if statement get executed and so i++ but b is set to 1 again.. Even i<10 becomes false but b values repeated from 1 to 5 again and again and b<=a is always true.. So infinite loop... Edit : what is your expected output?
22nd Aug 2020, 12:52 PM
Jayakrishna 🇮🇳