Control Statements
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
1. Conditional 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 :
1. if Statement
Used when you want to execute code only if a condition is true
if (condition) {
// code executes if condition is true
}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
2. if-else Statement
Used when you want one block if true, another if false
if (condition) {
// true block
} else {
// false block
}int number = 10;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
// output : Even numberOnly one block executes
elseis optional but usefulImproves decision making
3. if-else-if Ladder
Used when you have multiple conditions
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else if (condition3) {
// block 3
} else {
// default block
}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
elseis optional (default case)
4. SWITCH Statement
Used when comparing one variable with multiple fixed values
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}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 : WednesdayJava 17 Modern Switch
Since you're using Java 17
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
breakCleaner & modern
Used in real projects
2. Un-Conditional Control Statement
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
3. Looping Control Statement
Looping control statements are used to repeat a block of code multiple times until a certain condition is met.
Types of Loops in Java
for loop
When you already know the number of iterations
Example: printing numbers from 1 to 10
for(initialization; condition; update) {
// code
}Example:
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}🔍 Explanation:
int i = 1→ starting pointi <= 5→ condition checki++→ increment (increase value)
while loop
When the loop is condition-based
When you don’t know the exact number of iterations
while(condition) {
// code
}public class Main {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
}
}🔍 Explanation:
First, the condition is checked
If the condition is false, the loop will not run at all
do-while loop
When you want the loop to run at least once, no matter what
do {
// code
} while(condition);public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);
}
}Basic Loop Questions
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.
Pattern Questions
*
**
***
****
==========================
*****
****
***
**
*
===========================
*
**
***
****
*****
===============================
*
***
*****
*******
*********
================================
*
***
*****
*******
*********
*******
*****
***
*