if
: Runs code if a condition is true.
if-else
: Runs one block of code if true, and another block if false.
if-else if
ladder: Checks multiple conditions one by one and runs the block of code for the first true condition.
switch
: Chooses a block of code to run based on specific cases.
if
statement in Java evaluates a boolean
condition.
true
, the block of code inside the if statement is executed.
if(condition)
{
// this block will be executed if the condition is true
}
public class IfExample
{
public static void main(String[] args)
{
int number = 10;
// Check if the number is positive
if (number > 0)
{
System.out.println("The number is positive.");
}
}
}
The number is positive.
if
block, then curly braces {}
are optional.
if(condition)
statement;
public class IfExample
{
public static void main(String[] args)
{
int number = 10;
if (number > 0)
System.out.println("The number is positive.");
}
}
if-else
statement in Java evaluates a boolean
condition.
true
, the block of code inside the if
is executed; otherwise, the code inside the else
block runs.
if(condition)
{
// this block will be executed if the condition is true
}
else
{
// this block will be executed if condition is false
}
public class IfElseExample
{
public static void main(String[] args)
{
int number = -5;
// Check if the number is positive or negative
if (number > 0)
{
System.out.println("The number is positive.");
}
else
{
System.out.println("The number is negative.");
}
}
}
The number is negative.
if-else
block, then curly braces {}
are optional.
if(condition)
statement;
else
statement;
if-else if
ladder in Java evaluates multiple boolean
conditions in sequence.
true
, the block of code associated with that condition is executed; if none of the conditions are true
, the optional else
block runs.
if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition2 is true
}
// ---- more else-if blocks as needed ----
else
{
// Code to execute if none of the above conditions are true
}
public class IfElseIfLadderExample
{
public static void main(String[] args)
{
int marks = 75;
// Determine the grade based on marks
if (marks >= 90)
{
System.out.println("Grade: A");
}
else if (marks >= 75)
{
System.out.println("Grade: B");
}
else if (marks >= 50)
{
System.out.println("Grade: C");
}
else
{
System.out.println("Grade: F");
}
}
}
Grade: B
switch
statement in Java runs one block of code based on matching a condition.
default
block runs.
switch (expression)
{
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// ---- more cases as needed ----
default:
// Code to execute if no case matches (optional)
break;
}
if-else
statements, we can use the switch
statement for simpler code.
public class SwitchExample
{
public static void main(String[] args)
{
int day = 3;
// Determine the day of the week
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Wednesday
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.