+ 2

Its resulting in an infinte loop...why so? can we use ternary operator in increment part?

public class Program { public static void main(String[] args) { for(int x=0; x<=10; x=(x<5)?x+2:x++) { System.out.println(x); } } }

24th Mar 2020, 10:22 PM
Ana
2 Respostas
+ 4
yes, you can use ternary operator there replace 'x++' by '++x' there to make it a finite loop as after crossing 5, x=x++ which is equivalent to x=x only resulting in infinite loop.
24th Mar 2020, 10:26 PM
Gaurav Agrawal
Gaurav Agrawal - avatar