Read Also: [Fixed] Variable might not have been initialized
[Fixed] error: 'else' without 'if'
Example 1: Producing the error by ending if statement with a semi-colon
public class ElseWithoutIf {
public static void main(String args[]) {
int input = 79;if(input > 100);{
System.out.println("input is greater than 100");
}
else
{
System.out.println("input is less than or equal to 100");
}
}
}
Output:
/ElseWithoutIf.java:8: error: 'else' without 'if'
else {
^
1 error
Explanation:
The statement if(input > 100); is equivalent to writingif (input > 100)
{
}
i.e. input is greater than 100 then execute an empty block. There is no else block for this. As a result, the next line else block does not have a corresponding if block, thus, produces the error: else without if.
Solution:
The above compilation error can be resolved by removing the semicolon.public class ElseWithoutIf {
public static void main(String args[]) {
int input = 79;if(input > 100){
System.out.println("input is greater than 100");
}
else
{
System.out.println("input is less than or equal to 100");
}
}
}
Output:
input is less than or equal to 100
Example 2: Producing the error by missing the closing bracket in if condition
Just like the above example, we will produce the error first before moving on to the solution.public class ElseWithoutIf2 {
public static void main(String args[]) {
int input = 89;
if(input > 100)
{
System.out.println("inside if");else
{
System.out.println("inside else");
}
}
}
Output:
/ElseWithoutIf2.java:8: error: 'else' without 'if'
else
^
Explanation:
In the above example, if condition closing bracket is missing, hence, we are getting the else without if error.Solution:
The above compilation error can be resolved by inserting the missing closing bracket as shown below.public class ElseWithoutIf2 {
public static void main(String args[]) {
int input = 89;
if(input > 100)
{
System.out.println("inside if");}else
{
System.out.println("inside else");
}
}
}
Output:
inside else
Example 3: Producing the error by having more than one else clause for an if statement
public class ElseWithoutIf3 {
public static void main(String args[]) {
int input = 99;
if(input > 100)
{
System.out.println("input is greater than 100");
}else{
System.out.println("input is less than 100");
}
else
{
System.out.println("100");
}
}
}
Output:
/ElseWithoutIf3.java:12: error: 'else' without 'if'
else
^
1 error
Explanation:
In the above scenario, you are chaining the two elses together that is not correct syntax, hence, resulting in the error. The correct syntax is given below:if(condition)
{
//do A;
}
else if(condition)
{
//do B;
}
else
{
//do C;
}
Solution:
The above compilation error can be resolved by providing correct if-elseif-else syntax as shown below.public class ElseWithoutIf3 {
public static void main(String args[]) {
int input = 99;
if(input > 100)
{
System.out.println("input is greater than 100");
}else if (input < 100){
System.out.println("input is less than 100");
}
else
{
System.out.println("100");
}
}
}
Output:
input is less than 100
The best way to deal with error: 'else' without 'if' is always to use curly braces to show what is executed after if. Learn to indent the code too as it helps in reading and understanding.
0 Commentaires