Java If ... Else

JK JK 2038 
Created at
Updated at  
7,474 0 0

You already know that Java supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

if (condition) {
  // block of code to be executed if the condition is true
}

 

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text:

if (20 > 18) {
  System.out.println("20 is greater than 18");
}

 

We can also test variables:

int x = 20;
int y = 18;
if (x > y) {
  System.out.println("x is greater than y");
}

In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that "x is greater than y".


The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

 

Example

int time = 20;
if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."

In the example above, time (20) is greater than 18, so the condition is false. Because of this, we move on to the else condition and print to the screen "Good evening". If the time was less than 18, the program would print "Good day".


The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

 

Example

int time = 22;
if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."

In the example above, time (22) is greater than 10, so the first condition is false. The next condition, in the else if statement, is also false, so we move on to the else condition since condition1 and condition2 is both false - and print to the screen "Good evening".

However, if the time was 14, our program would print “Good day.”

Tags Java Java else Java if else if Facebook X
Comments 0
Similar posts
  1. Java Short Hand If...Else (Ternary Operator)
    7,646
  2. Java Switch Statements
    7,477
  3. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,983
  4. Java Math
    7,499
  5. Java Variables
    7,540
  6. Java Comments
    8,147
  7. Java Methods
    7,145
  8. Java Scope
    7,170
  9. Java Recursion
    7,237
  10. The Print() Method
    7,205
  11. Java Syntax
    7,523
  12. Java Classes and Objects
    7,162
  13. Java Getting Started
    7,469
  14. Java Packages
    7,221
  15. Java Inheritance (Subclass and Superclass)
    7,220
  16. Java Polymorphism
    7,099
  17. Java Inner Classes
    7,121
  18. What is Java?
    7,134
  19. Machine Learning Types and Programming Languages
    7,304
  20. Java Tutorials associated with AP Computer Science A
    7,726
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,228
  22. Java Servlet Example
    7,166
  23. Difference between Java and Javascript
    7,705
  24. Challenge: One Code Problem Per Day
    1,213
  1. How do I replace content that based on the HTML UI Template
    7,199
  2. Dataset of California Foodbanks
    7,214
  3. Java Abstract Classes and Methods
    7,825
  4. Java Arrays
    8,891
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    283
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    263
  3. Michael Jackson's Billie Jean
    311
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    159
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    206
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    238
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    251
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    294
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    885
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    433
  11. The Evolution and Production Reality of Agentic AI
    349
  12. How to Activate or Waive Your UIUC Student Health Insurance
    442
  13. Complete Guide to Building a Machine Learning Model
    460
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,549
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    491
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,206
  17. Clean Python Environments: The Power of venv vs. Docker
    1,011
  18. What is Docker? Why is Docker also useful in a development environment?
    740
  19. UIUC 2026-2027 Academic Calendar
    1,855
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    969
  21. Open-Source LLMs: The AI Revolution
    906
  22. Resume 2.0: Leveling Up for My First Software Gig
    2,631
  23. Not everyone will understand what this man just did
    2,192
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,751
  25. Unpacking IU's Shopper
    915
  26. Jackie Chan's Police Story: The Action Masterpiece
    787
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,097
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,311
  29. Open Databases for Sex Crime Occurrences in the U.S.
    865
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,054
  31. My First Day at University of Illinois-Urvana Champaign
    1,313
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,312
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,399
  34. Sipping the Stars: My Starbucks Adventure
    9,933
  35. Exciting explore at Sequoia National Park
    7,807
  36. My Life Shot at Death Valley
    1,919
  37. Ip Man fights with Muay Thai Master
    1,072
  38. Mad Clown - Don't Die
    1,184
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,395
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,030
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,271
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,102
  43. 10CM - To Reach You (너에게 닿기를)
    1,279
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,688
  45. BOYNEXTDOOR - If I Say I Love You
    1,303
  46. The Future of Software Engineer - AI Engineering
    1,036
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,036
  48. Lie - Legend song by BIGBANG
    7,938
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,000
  50. Reimbursement after Vaccination at McKinley Health Center
    1,141