2. If Statement
The if statement is the
most simple decision-
making statement. The if
statement allows us to
execute a block of code
only when specified
condition is true otherwise
it will not execute.
4. If Statement Example
public class IfStatementExample {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote!");
}
}
}
5. If-else Statement
The “if-else” statement in Java is
another decision-making
statement that allows you to
execute different blocks of
code based on the condition’s
result. It provides an alternative
statement to execute when
the condition is not true.
7. Example
public class IfElse {
public static void main(String[] args) {
int n = 10;
if (n > 5) {
System.out.println("The number is greater than 5.");
}
else {
System.out.println("The number is 5 or less.");
}
}
}
8. Else - if ladder
The Java if-else-if ladder is
used to evaluate multiple
conditions sequentially. It
allows a program to check
several conditions and
execute the block of code
associated with the first true
condition. If none of the
conditions are true then else
block is executed.
9. Else - if ladder Syntax
if (condition1) {
// Code to execute if condition1 is true
}
else if (condition2) {
// Code to execute if condition1 is false AND condition2 is true
}
else {
// Code to execute if all conditions are false
}
10. Else - if ladder Example
public class ElseIfLadderExample {
public static void main(String[] args) {
int marks = 78;
if (marks >= 90) {
System.out.println("Grade: A");
}
else if (marks >= 75) {
System.out.println("Grade: B");
}
else {
System.out.println("Grade: D");
}
}
}
11. Switch Statement
The switch statement
in Java is a multi-way
branch statement.
The Java switch
statement executes
one statement from
multiple conditions.
13. Switch Statement Example
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
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");
}
}
}
14. While Statement
The while statement in Java repeatedly executes a block of
code as long as a specified condition remains true. The
condition is evaluated before each iteration of the loop. If the
condition is initially false, the loop body will not execute at all.
Syntax :
while (condition) {
// code block to be executed
}
15. Example
public class WhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
}
}
16. For Statement
For loop in java is a control flow statement used to execute a block
of code repeatedly for a specific number of times , based on
condition.
For loop initializes a variable , checks a condition before each
iteration and update the variable after every iteration .
Syntax :
for (initialization; condition; increment) {
// code to be executed
}
17. Example
public class SumExample {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum of first 10 natural numbers is: " + sum);
}
}