Mastering Java - Full Guide
If Statements for Conditional Logic
If Statements for Conditional LogicConditional statements let you control the flow of your program based on different conditions. Here’s how to use if statements to evaluate and act on various conditions in Java.
Equals To
To check if two values are equal, use the double equals sign ==. For all of these examples lets make an int called x and assign the value 50 to it.
int x = 50;
if (x == 50) {
System.out.println("x is equal to 50");
}Greater Than
Use > to check if a value is greater than another.
if (x > 50) {
System.out.println("x is greater than 50");
}Less Than
Use < to check if a value is less than another.
if (x < 50) {
System.out.println("x is less than 50");
}Greater or Equals To
Use >= to check if a value is either greater than or equal to another.
if (x >= 50) {
System.out.println("x is greater than or equal to 50");
}Members only
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 14 more lessons in Mastering Java - Full Guide.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.