Aha 3
Aha 3
Aha 3
geeksforgeeks
Courses 90% Refund
Tutorials
Java
Practice
Contests
Sign In
Java Arrays
Java Strings
Java OOPs
Java Collection
Java 8 Tutorial
Java Multithreading
Java Exception Handling
Java Programs
Java Project
Java Collections Interview
Java Interview Questions
Java MCQs
Spring
Spring MVC
Spring Boot
Hibernate
Syntax:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
Working of the if-else-if ladder:
import java.io.*;
class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i == 10)
System.out.println("i is 10\n");
// condition 2
else if (i == 15)
System.out.println("i is 15\n");
// condition 3
else if (i == 20)
System.out.println("i is 20\n");
else
System.out.println("i is not present\n");
System.out.println("Outside if-else-if");
}
}
Output:
i is 20
Outside if-else-if
Dry running
Example 1:
1. Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
5.a) "i is 20" gets printed.
6. "Outside if-else-if" gets printed.
7. Program ends.
Example 2:
class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i < 10)
System.out.println("i is less than 10\n");
// condition 2
else if (i < 15)
System.out.println("i is less than 15\n");
// condition 3
else if (i < 20)
System.out.println("i is less than 20\n");
else
System.out.println("i is greater than "
+ "or equal to 20\n");
System.out.println("Outside if-else-if");
}
}
Output:
i is greater than or equal to 20
Outside if-else-if
Related Articles:
Decision-Making in Java
Java if statement with Examples
Java if-else statement with Examples
Switch Statement in Java
Break statement in Java
return keyword in Java
Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course
fee refund in 90 days. Avail now!
Want to be a master in Backend Development with Java for building robust and
scalable applications? Enroll in Java Backend and Development Live Course by
GeeksforGeeks to get your hands dirty with Backend Programming. Master the key Java
concepts, server-side programming, database integration, and more through hands-on
experiences and live projects. Are you new to Backend development or want to be a
Java Pro? This course equips you with all you need for building high-performance,
heavy-loaded backend systems in Java. Ready to take your Java Backend skills to the
next level? Enroll now and take your development career to sky highs.
C
Chinmoy Lenka
56
Previous Article
Java if-else
Next Article
Loops in Java
Read More
Down Arrow
Similar Reads
Naveed Ahmed Janvekar – Geek on the top | "Success is like a ladder and no one has
ever climbed a ladder with their hands in their pockets"
Geek on the top is all about success stories of Geeks who are working hard to chase
their goals and are the inspiration for other geeks.Today we have brought to you
the story of Naveed Ahmed Janvekar who is currently working as a Senior Data
Scientist at Amazon at one of the world's leading technology companies - Amazon. He
leads the abuse preventi
6 min read
Program to print the Ladder Pattern
Given an integer N, the task is to print the ladder with N steps using '*'. The
ladder will be with the gap of 3 spaces between two side rails. Input: N = 3
Output: * * * * ***** * * * * ***** * * * * ***** * * * * Input: N = 4 Output: * *
* * ***** * * * * ***** * * * * ***** * * * * ***** * * * * Approach: Dividing the
pattern into two sub-patter
4 min read
Decision Making in Java (if, if-else, switch, break, continue, jump)
Decision Making in programming is similar to decision-making in real life. In
programming also face some situations where we want a certain block of code to be
executed when some condition is fulfilled. A programming language uses control
statements to control the flow of execution of a program based on certain
conditions. These are used to cause t
7 min read
Java if-else
Decision-making in Java helps to write decision-driven statements and execute a
particular set of code based on certain conditions. The if statement alone tells us
that if a condition is true it will execute a block of statements and if the
condition is false it won’t. In this article, we will learn about Java if-else. If-
Else in JavaIf- else toget
3 min read
Writing clean if else statements
Using if else chaining some time looks more complex, this can be avoided by writing
the code in small blocks. Use of conditional statement increases the code
readability and much more. One best practice should be handling error case first.
Below shown example shows how to handle error cases and simplify the if else
logic.Examples 1: updateCache()-
5 min read
switch vs if else
Prerequisite - Switch Statement, Decision making(if else) A switch statement is
usually more efficient than a set of nested ifs. Deciding whether to use if-then-
else statements or a switch statement is based on readability and the expression
that the statement is testing. Check the Testing Expression: An if-then-else
statement can test expressions
3 min read
Output of C programs | Set 65 (If-Else)
Prerequisite : Decision making in CQuestion 1 C/C++ Code
#include"stdio.h" #include"stdlib.h" void reverse(int i) { if
(i > 5) exit(0); printf("%d\n", i); return reverse(i++); } int main()
{ reverse(1); } OPTIONS: a)Segmentation fault b)Compilation error c)Print 1
Infinite time d)Both a & c OUTPUT: (d)Both a
2 min read
Rachit Jain - Geek on the top | "Stop starting, start finishing: Don't just give up
on things and then start doing something else"
Geek on the top is all about success stories of Geeks who are working hard to chase
their goals and are the inspiration for other geeks. Rachit Jain is an IIT
Roorkee Alumnus and a Software Engineer at Microsoft. Rachit has his programming
Youtube channel named "Algorithms With Rachit Jain". He believes that his YouTube
channel empowers other stu
7 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and
java.sql.Date in many instances. Whenever the java application interacts with the
database, we should use these instead of java.util.Date. The reason is JDBC i.e.
java database connectivity uses these to identify SQL Date and Timestamp. Here let
us see the differences
7 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from
ArrayDeque which satisfies a given predicate filter condition passed as a parameter
to the method. This method returns true if some element are removed from the
Vector. Java 8 has an important in-built functional interface which is Predicate.
Predicate, or a condition che
3 min read
Article Tags :
Java
Misc
School Programming
java-basics
Practice Tags :
Java
Misc
three90RightbarBannerImg
course-img
215k+ interested Geeks
Master Java Programming - Complete Beginner to Advanced
Avail 90% Refund
course-img
214k+ interested Geeks
JAVA Backend Development - Live
Avail 90% Refund
course-img
30k+ interested Geeks
Manual to Automation Testing: A QA Engineer's Guide
Avail 90% Refund
geeksforgeeks-footer-logo
Corporate & Communications Address:- A-143, 9th Floor, Sovereign Corporate Tower,
Sector- 136, Noida, Uttar Pradesh (201305) | Registered Address:- K 061, Tower K,
Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh,
201305
GFG App on Play Store
GFG App on App Store
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
GFG Corporate Solution
Placement Training Program
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
GfG Weekly Contest
Offline Classes (Delhi/NCR)
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming
Data Science & ML
Data Science With Python
Data Science For Beginner
Machine Learning Tutorial
ML Maths
Data Visualisation Tutorial
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning Tutorial
Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
DevOps Roadmap
System Design
High Level Design
Low Level Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
System Design Bootcamp
Interview Questions
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB
Preparation Corner
Company-Wise Recruitment Process
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
UPSC
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets
Recent Articles
Free Online Tools
Typing Test
Image Editor
Code Formatters
Code Converters
Currency Converter
Random Number Generator
Random Password Generator
Write & Earn
Write an Article
Improve an Article
Pick Topics to Write
Share your Experiences
Internships
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy
& Privacy Policy
Got It !
Lightbox