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");} } }

21st May 2021, 12:30 PM
Amrit Sahni
Amrit Sahni - avatar
4 Respuestas
+ 4
Replace if(salary >6000); with if(salary >6000) There should not be semicolon.
21st May 2021, 12:32 PM
Sandesh Aryal
Sandesh Aryal - avatar
+ 3
Remove the semicolon after if statement
21st May 2021, 12:33 PM
Atul [Inactive]
+ 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
21st May 2021, 12:33 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 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"); } } }
21st May 2021, 12:43 PM
Benjamin Jürgens
Benjamin Jürgens - avatar