š Program Control Statements in Java
Control statements in Java allow you to dictate the flow of execution in your program. They can be broadly categorized into conditional, looping, and branching statements.
š§ Types of Control Statements
Conditional Statements
if
,else
,else if
: Used to make decisions in code.
int a = 10, b = 20; if (a > b) { System.out.println("a is greater"); } else { System.out.println("b is greater"); }
2. **Switch Statement**
* Used for multiple condition checks, simplifying `if-else if` chains.
```java
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid day");
}
Looping Statements
- for: Repeats code a set number of times.
- while: Executes code while a condition is true.
- do-while: Executes code at least once, then checks the condition.
// For Loop Example for (int i = 0; i < 5; i++) { System.out.println(i); }
Branching Statements
- break: Exits a loop or switch statement.
- continue: Skips the current iteration of a loop.
// Continue Example for (int i = 0; i < 5; i++) { if (i == 3) continue; // Skip the iteration when i is 3 System.out.println(i); }
š§Ŗ Example: Conditional and Looping Control
public class ControlStatementsExample {
public static void main(String[] args) {
int num = 5;
if (num > 0) {
System.out.println("Positive number");
}
// For Loop
for (int i = 1; i <= 3; i++) {
System.out.println("Iteration " + i);
}
}
}