Loading content...
Loading content...
Control statement is used to control the flow of execution depending on some specific condition.
Types of Control Statement
Conditional Control Statement
Un-Conditional Control Statement
Looping Control Statement
It is used to make decisions in code based on certain conditions.
Types of Conditional Control Statements
if statement :
if-else statement :
if-else-if ladder :
switch statement :
Used when you want to execute code only if a condition is true
java
if (condition) {
// code executes if condition is true
}java
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote");
}
// output : You are eligible to voteCondition must return boolean (true/false)
Executes only when condition = true
No action if condition is false
Used when you want one block if true, another if false
java
if (condition) {
// true block
} else {
// false block
}java
int number = 10;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
// output : Even numberOnly one block executes
else is optional but useful
Improves decision making
Used when you have multiple conditions
java
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else if (condition3) {
// block 3
} else {
// default block
}java
int marks = 75;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 70) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
// output : Grade BConditions checked top to bottom
First true condition executes
Remaining conditions are skipped
else is optional (default case)
Used when comparing one variable with multiple fixed values
javascript
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}javascript
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;
default:
System.out.println("Invalid day");
}
// output : WednesdaySince you're using Java 17
plaintext
int day = 2;
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
default -> System.out.println("Invalid");
}No need for break
Cleaner & modern
Used in real projects
Unconditional Control Statements are used to change the normal flow of execution in a program without checking any condition. These are not dependent on any boolean expressions — they just perform an action directly.
Types of Unconditional Control Statements:
break
continue
return
Looping control statements are used to repeat a block of code multiple times until a certain condition is met.
Types of Loops in Java
When you already know the number of iterations
Example: printing numbers from 1 to 10
java
for(initialization; condition; update) {
// code
}java
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}int i = 1 → starting point
i <= 5 → condition check
i++ → increment (increase value)
When the loop is condition-based
When you don’t know the exact number of iterations
java
while(condition) {
// code
}java
public class Main {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
}
}First, the condition is checked
If the condition is false, the loop will not run at all
When you want the loop to run at least once, no matter what
java
do {
// code
} while(condition);java
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);
}
}Print numbers from 1 to 10 using a for loop.
Print even numbers between 1 to 50.
Print odd numbers between 1 to 50.
Print the sum of numbers from 1 to N.
Find the sum of even and odd numbers separately from 1 to N.
*
**
***
****
==========================
*****
****
***
**
*
===========================
*
**
***
****
*****
===============================
*
***
*****
*******
*********
================================
*
***
*****
*******
*********
*******
*****
***
*