0
Can anybody please tell me that why this program is showing an error?
public class Program { public static void main(String[] args) { int salary = 5000; if(salary >6000); {System.out.println("ok"); } else { System.out.println("great");} } }
4 Respuestas
+ 4
Replace
if(salary >6000);
with
if(salary >6000)
There should not be semicolon.
+ 3
Remove the semicolon after if statement
+ 2
Remove the semicolon from rhe line starting with if
Explanation: a semicolon ends a statement, so that's an empty statement. if(...) executes the next statement only if the condition is true. But the next statement is the empty statement. So ok is always printed. else has to directly follow an if, which is not the case because of the empty statement
+ 2
Your code properly indented to show the actual structure:
public class Program {
public static void main(String[] args) {
int salary = 5000;
if(salary > 6000)
;
{ System.out.println("ok"); }
else
{ System.out.println("great"); }
}
}