Decision Making in Java (If, If-Else, Switch, Break, Continue, Jump) - GeeksforGeeks
Decision Making in Java (If, If-Else, Switch, Break, Continue, Jump) - GeeksforGeeks
CoursesTutorialsJavaPracticeContests
if
if-else
nested-if
if-else-if
switch-case
jump – break, continue, return
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 Got It !
Policy
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 1/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. if statement
accepts boolean values – if the value is true then it will execute the block of
statements under it.
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by
default if statement will consider the immediate one statement to be inside
its block. For example,
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
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 2/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Example:
Java
class IfDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("Inside If block"); // part of if block(immediat
System.out.println("10 is less than 15"); //always executes as it i
// This statement will be executed
// as if considers one statement by default again below statement is ou
System.out.println("I am Not in if");
}
}
Output
Inside If block
10 is less than 15
I am Not in if
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 3/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
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
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 4/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Example:
Java
class IfElseDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output
i is smaller than 15
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
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
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 5/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Example:
Java
class NestedIfDemo {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i<15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
} else{
System.out.println("i is greater than 15");
}
}
}
Output
We use cookies to ensure you have the best browsing experience on our website. By using
our site,iyou
is smaller than 15
acknowledge that you have read and understood our Cookie Policy & Privacy
i is smaller than 12 too Policy
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 6/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:
Java
class ifelseifDemo {
We use cookies to ensure
public you have
static the best
void browsing experience
main(String args[])on our website. By using
our site, you{acknowledge that you have read and understood our Cookie Policy & Privacy
int i = 20; Policy
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 7/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output
i is 20
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
We use cookies to ensure you have the best browsing experience on our website. By using
Java
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 8/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
import java.io.*;
class GFG {
public static void main (String[] args) {
int num=20;
switch(num){
case 5 : System.out.println("It is 5");
break;
case 10 : System.out.println("It is 10");
break;
case 15 : System.out.println("It is 15");
break;
case 20 : System.out.println("It is 20");
break;
default: System.out.println("Not present");
}
}
}
Output
It is 20
6. jump: Java supports three jump statements: break, continue and return.
These three statements transfer control to another part of the program.
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
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 9/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Example:
Java
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 10/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
class ContinueDemo {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is even
// skip and continue
if (i % 2 == 0)
continue;
Output
1 3 5 7 9
JavaExample:
Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exceptio
Java
if (t)
return;
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 11/16
4/22/24, 8:22 PM Decision Making in Java (if, if-else, switch, break, continue, jump) - GeeksforGeeks
Output
Feeling lost in the vast world of Backend Development? It's time for a
change! Join our Java Backend Development - Live Course and embark on an
exciting journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Previous Next
Similar Reads
Break and Continue statement in Java Loops and Control Statements
(continue, break and pass) in Python
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
switch vs if else Policy Continue Statement in Java
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/ 12/16