Java If ... Else

JK JK 2038 
Created at
Updated at  
7,533 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,722
  2. Java Switch Statements
    7,544
  3. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    8,097
  4. Java Math
    7,575
  5. Java Variables
    7,619
  6. Java Comments
    8,249
  7. Java Methods
    7,201
  8. Java Scope
    7,222
  9. Java Recursion
    7,298
  10. The Print() Method
    7,268
  11. Java Syntax
    7,600
  12. Java Classes and Objects
    7,230
  13. Java Getting Started
    7,532
  14. Java Packages
    7,291
  15. Java Inheritance (Subclass and Superclass)
    7,278
  16. Java Polymorphism
    7,158
  17. Java Inner Classes
    7,189
  18. What is Java?
    7,184
  19. Machine Learning Types and Programming Languages
    7,367
  20. Java Tutorials associated with AP Computer Science A
    7,810
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,288
  22. Java Servlet Example
    7,221
  23. Difference between Java and Javascript
    7,784
  24. Challenge: One Code Problem Per Day
    1,324
  1. How do I replace content that based on the HTML UI Template
    7,251
  2. Dataset of California Foodbanks
    7,270
  3. Java Abstract Classes and Methods
    7,957
  4. Java Arrays
    9,155
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    1,089
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    969
  3. Michael Jackson's Billie Jean
    451
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    429
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    372
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    414
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    409
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    446
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,486
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    590
  11. The Evolution and Production Reality of Agentic AI
    471
  12. How to Activate or Waive Your UIUC Student Health Insurance
    608
  13. Complete Guide to Building a Machine Learning Model
    616
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,660
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    623
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,408
  17. Clean Python Environments: The Power of venv vs. Docker
    1,152
  18. What is Docker? Why is Docker also useful in a development environment?
    853
  19. UIUC 2026-2027 Academic Calendar
    2,125
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,128
  21. Open-Source LLMs: The AI Revolution
    1,024
  22. Resume 2.0: Leveling Up for My First Software Gig
    3,142
  23. Not everyone will understand what this man just did
    2,578
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,914
  25. Unpacking IU's Shopper
    1,012
  26. Jackie Chan's Police Story: The Action Masterpiece
    908
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,259
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,431
  29. Open Databases for Sex Crime Occurrences in the U.S.
    1,021
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,550
  31. My First Day at University of Illinois-Urvana Champaign
    1,412
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,481
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,538
  34. Sipping the Stars: My Starbucks Adventure
    10,263
  35. Exciting explore at Sequoia National Park
    7,899
  36. My Life Shot at Death Valley
    2,126
  37. Ip Man fights with Muay Thai Master
    1,171
  38. Mad Clown - Don't Die
    1,299
  39. How to get Student Enrollment and Degree Verification at UIUC
    6,118
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,131
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,398
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,180
  43. 10CM - To Reach You (너에게 닿기를)
    1,365
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,776
  45. BOYNEXTDOOR - If I Say I Love You
    1,420
  46. The Future of Software Engineer - AI Engineering
    1,137
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,147
  48. Lie - Legend song by BIGBANG
    8,046
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,136
  50. Reimbursement after Vaccination at McKinley Health Center
    1,262