Java If ... Else

JK JK 2038 
Created at
Updated at  
7,495 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,666
  2. Java Switch Statements
    7,494
  3. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    8,021
  4. Java Math
    7,517
  5. Java Variables
    7,562
  6. Java Comments
    8,187
  7. Java Methods
    7,155
  8. Java Scope
    7,184
  9. Java Recursion
    7,249
  10. The Print() Method
    7,217
  11. Java Syntax
    7,541
  12. Java Classes and Objects
    7,182
  13. Java Getting Started
    7,488
  14. Java Packages
    7,240
  15. Java Inheritance (Subclass and Superclass)
    7,238
  16. Java Polymorphism
    7,122
  17. Java Inner Classes
    7,141
  18. What is Java?
    7,149
  19. Machine Learning Types and Programming Languages
    7,322
  20. Java Tutorials associated with AP Computer Science A
    7,753
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,245
  22. Java Servlet Example
    7,177
  23. Difference between Java and Javascript
    7,724
  24. Challenge: One Code Problem Per Day
    1,248
  1. How do I replace content that based on the HTML UI Template
    7,214
  2. Dataset of California Foodbanks
    7,229
  3. Java Abstract Classes and Methods
    7,860
  4. Java Arrays
    8,962
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    611
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    538
  3. Michael Jackson's Billie Jean
    351
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    261
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    267
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    304
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    316
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    345
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,060
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    496
  11. The Evolution and Production Reality of Agentic AI
    387
  12. How to Activate or Waive Your UIUC Student Health Insurance
    484
  13. Complete Guide to Building a Machine Learning Model
    507
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,577
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    526
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,286
  17. Clean Python Environments: The Power of venv vs. Docker
    1,058
  18. What is Docker? Why is Docker also useful in a development environment?
    777
  19. UIUC 2026-2027 Academic Calendar
    1,908
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,026
  21. Open-Source LLMs: The AI Revolution
    945
  22. Resume 2.0: Leveling Up for My First Software Gig
    2,791
  23. Not everyone will understand what this man just did
    2,308
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,794
  25. Unpacking IU's Shopper
    946
  26. Jackie Chan's Police Story: The Action Masterpiece
    824
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,143
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,351
  29. Open Databases for Sex Crime Occurrences in the U.S.
    917
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,197
  31. My First Day at University of Illinois-Urvana Champaign
    1,348
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,359
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,439
  34. Sipping the Stars: My Starbucks Adventure
    10,019
  35. Exciting explore at Sequoia National Park
    7,836
  36. My Life Shot at Death Valley
    1,979
  37. Ip Man fights with Muay Thai Master
    1,102
  38. Mad Clown - Don't Die
    1,218
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,585
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,060
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,314
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,125
  43. 10CM - To Reach You (너에게 닿기를)
    1,309
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,716
  45. BOYNEXTDOOR - If I Say I Love You
    1,341
  46. The Future of Software Engineer - AI Engineering
    1,068
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,069
  48. Lie - Legend song by BIGBANG
    7,965
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,045
  50. Reimbursement after Vaccination at McKinley Health Center
    1,170