0

why dose this code run forever and doesn't execute the while loop ??

package primenum; import java.util.*; public class PrimeNum { public static void main(String[] args) { Scanner NUM = new Scanner(System.in); System.out.println("enter the min number: "); double a = NUM.nextDouble(); System.out.println("enter the max number: "); double b = NUM.nextDouble(); while(a <= b) { if ((!(a%2==0))||(a==2.0)) {System.out.println(a); a++ ;}} }}

8th Jun 2021, 4:08 PM
Sarah
Sarah - avatar
3 Respuestas
+ 1
If `a` is even and not equal to 2, the if-condition would be false, and the value of `a` would not be changed. The loop will repeat again with the same value of `a`, hence the same thing will happen again, and keep happening forever.
8th Jun 2021, 4:30 PM
XXX
XXX - avatar
+ 1
Sarah Because you have written a++ inside if condition so if I takes 10 and 20 then condition is not satisfying and 'a' is not incrementing. If 'a' will not increment then while loop condition will remain constant and loop will run forever. So write a++ outside the if condition.
8th Jun 2021, 5:30 PM
A͢J
A͢J - avatar
0
how so, i have written after the statement a++ so it'll increase by 1 every time
8th Jun 2021, 4:45 PM
Sarah
Sarah - avatar