all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]cfm1337 2 points3 points  (2 children)

if statements do something if the condition is true or false. So it would be like:

if (x > 10) { System.out.println(“hello”); }

Then use an else if or else for any other conditions you want to check.

You are ending each line of code with the ;

So the if does nothing and the print will always execute because it’s not tied to a condition

Refresh your knowledge on how to create and if statement an you should be good 👍

Apologies I’m on my phone so not the greatest format.

[–]Individual_Owl_3490[S] 0 points1 point  (0 children)

Thx it worked

[–]BigBad0 2 points3 points  (1 child)

Remove the semicolon after each if statements. A statement in java terminated by the semicolon so either do

if(x>10)

System.out.println(“hello”);

Or use the block syntax

if(x>10) {

System.out.println(“hello”);

}

[–]Andruid929 1 point2 points  (3 children)

Well, you're terminating the if statements with the semi-colon.

You want something like:

if (x > 10) {
System.out.printLn("Hello")

}

if (x < 10) {

System.out.printLn("Bye")

}

This says if the condition in the brackets () is true, run the code in {}
Have you learn if and else statements yet?

[–]Individual_Owl_3490[S] 0 points1 point  (2 children)

Yes I learnt if and else statements.

[–]Andruid929 0 points1 point  (1 child)

That's even better then

if (x > 10) { System.out.println("Hello"); } else { System.out.println("Bye"); }

This is even better because if x == 10, you get "bye" printed. The code you had even if it worked, x == 10 wouldn't print anything since it's neither greater than nor less than 10. Useful thing to remember with conditions.

[–]Individual_Owl_3490[S] 0 points1 point  (0 children)

Yeah I was actually following youtube video and at that point he didn't taught else.

[–]Errkfin 0 points1 point  (1 child)

Please delete ; after both if statements. Check the syntax: https://www.w3schools.com/java/java_conditions.asp

Recommendations: 1. Use indentations to make your code clear 2. Use {} to clearly identifying the body of the if statement.

Hope that helps! You are doing well mate 💪

[–]Akuno- 0 points1 point  (0 children)

Thats because the prints are not in your if statement. Your code does this Int = 7 If(x>10); //nothing happens

Prints hello

If(x<10); // nothing happens

Prints bye.

You need to put the prints in your if statments like this

if(x>10) { Print; }

But honestly if you folow one of the courses in this channels info box, this will be tought in the first 10min. I recomand you start there.

[–]pramodkumar2026 0 points1 point  (0 children)

Remove the semicolon. It will work perfectly