• Register
2 votes
44 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

Please log in or register to answer this question.

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

0 votes
1 answer 3 views
3 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 12 views
12 views
Problem: Can any one explain php if else statement with multiple conditions to me?
asked Feb 23 jyoti goyal 2.1k points
0 votes
1 answer 210 views
210 views
Problem Hey, I have built a calculator with a code of addition and subtraction. Then after some time of using it i felt something incomplete then i decided to add some more lines of code for adding multiplying and dividing. During compilation my code was showing errors. So provide me a code of complete calculator and ...               }                 }}}     system("PAUSE");     return 0; }
asked Dec 19, 2020 chris jordan 2.4k points
1 vote
1 answer 14 views
14 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, 2020 adamSw 11.3k points
0 votes
1 answer 14 views
14 views
Problem: What is the difference between switch statement and if else statment ? can anyone explain with examples.
asked Dec 23, 2020 prog_learner 2.5k points
0 votes
1 answer 13 views
13 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, 2020 Gavin 15.3k points
0 votes
1 answer 1 view
1 view
Problem How i write multiple condition in if statment at c++
asked Mar 30 Subhendu 4.5k points
1 vote
1 answer 25 views
25 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, 2020 LizzyM 6.1k points
0 votes
1 answer 3 views
3 views
Problem: Please help me to solve it... complete the if-else statement to print 'lol means laughing out loud' if user_tweet contains 'lol'.
asked Mar 31 Ifra 24.4k points
0 votes
1 answer 3 views
3 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 24 rubelkhan 20.4k points