Control Statements in Java
What are Control Statements?
Control statements in Java are used to **control the flow of execution** in a program. These statements decide the sequence of execution based on conditions and loops.
Types of Control Statements
Java provides three types of control statements:
- Selection Statements (if, if-else, switch)
- Iteration Statements (for, while, do-while)
- Jump Statements (break, continue, return)
1. Selection Statements
These statements allow the program to make decisions and execute specific blocks of code.
1.1 if Statement
Executes a block of code only if the condition is **true**.
public class IfExample {
public static void main(String[] args) {
int num = 10;
if (num > 5) {
System.out.println("Number is greater than 5");
}
}
}
1.2 if-else Statement
Executes one block if the condition is **true** and another block if it's **false**.
public class IfElseExample {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
1.3 switch Statement
Used when multiple conditions are checked against a single variable.
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;
default: System.out.println("Other day");
}
}
}
2. Iteration Statements (Loops)
Loops are used to execute a block of code multiple times.
2.1 for Loop
Used when the number of iterations is known.
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
}
}
2.2 while Loop
Executes a block of code while the condition is **true**.
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Iteration: " + i);
i++;
}
}
}
2.3 do-while Loop
Executes the block at least once, even if the condition is **false**.
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Iteration: " + i);
i++;
} while (i <= 5);
}
}
3. Jump Statements
Used to change the flow of execution.
3.1 break Statement
Stops the execution of a loop or switch statement.
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
System.out.println(i);
}
}
}
3.2 continue Statement
Skips the current iteration and continues with the next iteration.
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
}
}
3.3 return Statement
Used to exit from a method and return a value.
public class ReturnExample {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 10);
System.out.println("Sum: " + result);
}
}
Conclusion
Control statements are essential for decision-making and looping in Java. They help in executing code efficiently based on conditions.
📌 Next Topic: Methods in Java