Java If ... Else

JK JK 2022 
Created at
Updated at  
7,400 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,584
  2. Java Switch Statements
    7,414
  3. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,872
  4. Java Math
    7,441
  5. Java Variables
    7,473
  6. Java Comments
    8,016
  7. Java Methods
    7,092
  8. Java Scope
    7,108
  9. Java Recursion
    7,203
  10. The Print() Method
    7,159
  11. Java Syntax
    7,447
  12. Java Classes and Objects
    7,092
  13. Java Getting Started
    7,414
  14. Java Packages
    7,179
  15. Java Inheritance (Subclass and Superclass)
    7,184
  16. Java Polymorphism
    7,060
  17. Java Inner Classes
    7,060
  18. What is Java?
    7,094
  19. Machine Learning Types and Programming Languages
    7,238
  20. Java Tutorials associated with AP Computer Science A
    7,622
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,173
  22. Java Servlet Example
    7,125
  23. Difference between Java and Javascript
    7,626
  24. Challenge: One Code Problem Per Day
    1,106
  1. How do I replace content that based on the HTML UI Template
    7,159
  2. Dataset of California Foodbanks
    7,176
  3. Java Abstract Classes and Methods
    7,744
  4. Java Arrays
    8,719
Recently updated
  1. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    64
  2. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    52
  3. Watch This Dazzling Dancer’s Mind-Blowing Moves
    104
  4. Michael Jackson's Billie Jean
    168
  5. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    162
  6. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    514
  7. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    264
  8. The Evolution and Production Reality of Agentic AI
    229
  9. How to Activate or Waive Your UIUC Student Health Insurance
    316
  10. Complete Guide to Building a Machine Learning Model
    348
  11. My life cuts at Las Vegas during Thanksgiving day holiday
    7,431
  12. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    376
  13. Harness vs. OpenClaw: Two Very Different "Agents"
    1,008
  14. Clean Python Environments: The Power of venv vs. Docker
    848
  15. What is Docker? Why is Docker also useful in a development environment?
    641
  16. UIUC 2026-2027 Academic Calendar
    1,639
  17. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    827
  18. Open-Source LLMs: The AI Revolution
    779
  19. Resume 2.0: Leveling Up for My First Software Gig
    2,283
  20. Not everyone will understand what this man just did
    1,928
  21. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,619
  22. Unpacking IU's Shopper
    783
  23. Jackie Chan's Police Story: The Action Masterpiece
    676
  24. The IVE Story: Identity, 'I AM' Charts, and Influence
    979
  25. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,197
  26. Open Databases for Sex Crime Occurrences in the U.S.
    731
  27. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,706
  28. My First Day at University of Illinois-Urvana Champaign
    1,214
  29. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,156
  30. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,237
  31. Sipping the Stars: My Starbucks Adventure
    9,695
  32. Exciting explore at Sequoia National Park
    7,679
  33. My Life Shot at Death Valley
    1,737
  34. Ip Man fights with Muay Thai Master
    958
  35. Mad Clown - Don't Die
    1,049
  36. How to get Student Enrollment and Degree Verification at UIUC
    4,975
  37. LAX Thanksgiving Rush: A Joyful Reunion
    938
  38. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,147
  39. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,001
  40. 10CM - To Reach You (너에게 닿기를)
    1,173
  41. Feeling weak? Transform yourself at the UIUC ARC!
    1,585
  42. BOYNEXTDOOR - If I Say I Love You
    1,198
  43. The Future of Software Engineer - AI Engineering
    940
  44. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    932
  45. Lie - Legend song by BIGBANG
    7,834
  46. Why ROLLBACK is useful when you work with Google Gemini CLI?
    858
  47. Reimbursement after Vaccination at McKinley Health Center
    1,038
  48. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    977
  49. Common Questions from UIUC school life in terms of CS Program
    1,144
  50. UIUC Immunization Compliance
    1,231