• Register
2 votes
36 views

Problem:

Hello

I am a new learner of programming. I’ve started with Java programming language. I am learning step by step. I stuck somewhere in an if-else statement. I’ve written a basic program for practice purpose but it’s not running.

I am unable to figure out the problem. Could you please find and fix the error in the if-else statement.?

Thanks. Find the code snippet below:

import java.io.*; 
public class MyClass { 
    public static void main(String[] args){ 
        int a = 20; 
        if (a == 10) 
            System.out.println("a is 10\n"); 
        else if (a == 15) 
            System.out.println("a is 15\n"); 
        else if (a == 20) 
            System.out.println("a is 20\n"); 
        else (a==0)
            System.out.println("a is not present\n"); 
        System.out.println("It is outside of the if-else-if"); 
    } 
} 

 

12 7 7
15,250 points

1 Answer

2 votes
 
Best answer

Solution:

You need to understand the basic of an if-else statement firstly. As long as you don’t know the functionality you can’t write the program in the right way.

The if-else statement is a boolean type operator. It works like Either/Or in a program. At least one statement has to be correct.

if (condition)
    First statement;
else if (condition)
    Second statement;
.
.
else
    Another statement;

As you can see it doesn’t require condition in the last case. So you need to remove the last condition from your code. I am doing this for you.

import java.io.*; 
public class MyClass { 
    public static void main(String[] args){ 
        int a = 20; 
        if (a == 10) 
            System.out.println("a is 10"); 
        else if (a == 15) 
            System.out.println("a is 15"); 
        else if (a == 20) 
            System.out.println("a is 20"); 
        else
            System.out.println("a is not present\n"); 
        System.out.println("It is outside of the if-else-if"); 
    } 
} 

Now your program looks good. Keep discovering new things. Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 9 views
9 views
Problem: I understand the basic flow of an if-else statement very well. Today I was reading somewhere that it is possible to set a ternary condition using the if-else statement. Let&rsquo;s say, I have a set of conditions like below: if i > 100: x = 2 elif i < 100: x = 1 else: x = 0 How can I put all the conditions and statements within a single line? Could you please help me out?
asked May 31 adamSw 11.3k points
0 votes
1 answer 9 views
9 views
Problem: Hello guys, I am new to python language and just complete the list concept in python and wanted to learn the list comprehension and have learned about it but when i tried to implement one of the program of it, I am not able to implement it.below is the code so this is getting me mad because i come from c language background so need help from someone.
asked Jul 4 Gavin 15.3k points
1 vote
1 answer 13 views
13 views
Problem: I am new in python programming an I have been learning for this some days. As a newbee I am still learning and and I am cofused about some things. My question is &ldquo;In an if/else statement, if the boolean expression is always false?&rdquo;
asked Apr 3 LizzyM 6.1k points
1 vote
1 answer 27 views
27 views
Problem: Let me put it as sim as I can. I have an if statement in my code and it returns a value. Since IF is a boolean statement it should return value whether it&rsquo;s true or false. So, in my program when a method tests an argument and returns a true or ... as a parameter in a different method inside an IF statement. How should I do that? Any kind of help is appreciated by my side. Thanks.
asked Mar 17 Gavin 15.3k points
0 votes
1 answer 21 views