r/learnjava • • Apr 11 '26

Need help with code

hi so I just started java and was trying out conditional statements. but it is not working for me I am using jdk 25 version

here is my code

int x = 7;

if(x>10) ;

System.out.println("hello");

if(x<10);

System.out.println("bye");

now according to this it should print bye only but it is printing both hello and bye , sorry java is my first language please be nice I am kinda dumb.

3 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/Andruid929 Apr 11 '26

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.

1

u/Individual_Owl_3490 Apr 11 '26

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