Chapter N0 4
Control Statements
4.1. Introduction and Control Statement Types
4.2. Conditional Statements
4.3. Loops
4.4. Break / Continue
4.1. Control Statement Types:
- Control statements are used in programming to control the flow of a program. There are primarily
three types:
1. Selection Statements:These are used to make decisions in the code. Common selection statements
include `if`, `else if`, and `else`.
2. Iteration Statements (Loops):These statements are used for repetitive tasks. Common iteration
statements include `for`, `while`, `do-while`, and `foreach`.
3. Jump Statements: These are used to alter the normal flow of the program. Common jump
statements include `break` and `continue`.
4.2. Expression Statements:
- Expression statements are used to perform a task or evaluate an expression. They don’t control the
flow of a program but can be part of control statements.
- Example: `x = 5;` where `x` is assigned the value 5.
4.3. Compound Statements:
- Compound statements, also known as code blocks, group multiple statements into a single unit.
- They are often used in control statements to execute multiple statements together.
- Common examples include code blocks enclosed in curly braces `{ ... }`.
4.4. If/Else Statements:
- `if` statements are used to make decisions in code based on a condition. If the condition is true, a
block of code is executed.
- `else` can be used to specify what to do if the condition is false.
- `else if` allows for multiple conditions to be checked in sequence.
- Example:
Public class Main {
Public static void main(String[] args) {
Int percentage = 85;
If (percentage > 90) {
System.out.println(“Grade A”);
} else if (percentage > 75) {
System.out.println(“Grade B”); } else if (percentage > 65) {
System.out.println(“Grade C”);
} else {
System.out.println(“Grade D”);
} } }
4.5. Switch Statement:
- The `switch` statement is used to select one of many code blocks to be executed based on the value of an expression.
- It’s a more efficient way of handling multiple conditions than a series of `if` statements.
- Example:
Public class DayOfWeek {
Public static void main(String[] args) {
Int dayNumber = 3; StringdayOfWeek=getDayOfWeek(dayNumber);
System.out.println(“Day: “ + DayOfWeek); }
Private static String getDayOfWeek(int dayNumber) {
String dayOfWeek;
Switch (dayNumber) {
Case 1:
dayOfWeek = “Sunday”;
break;
case 2:
dayOfWeek = “Monday”;
break;
default:
dayOfWeek = “Invalid day number”;
break; }
Return dayOfWeek;
} }
4.6. Loops (DO, WHILE, FOR, FOREACH):
- Loops are used for repetitive execution of a block of code until a specific condition is met.
- Common loop types:
1. for loop:
Iterates a fixed number of times.
Public class Main {
Public static void main(String[] args) {
For (int i = 0; i < 5; i++) {
// Code to execute in each iteration
} } }
2. `while` loop:
Repeats as long as a condition is true.
Public class Main {
Public static void main(String[] args) {
Int i = 0;
While (i < 5) {
I++;
} } }
3. `do-while` loop:
Similar to a `while` loop but guaranteed to execute at least once.
Public class Main {
Public static void main(String[] args) { Int i = 0;
Do {
I++;
} while (i < 5);
} }
4. `foreach` (or equivalent) loop:
Used for iterating over elements in collections or arrays.
Public class Main {
Public static void main(String[] args) {
Int[] array = {0, 1, 2, 3, 4};
For (int i : array) {
// Code to execute in each iteration
} } }
4.7. BREAK/CONTINUE Statements:
- `break` is used to exit a loop prematurely, breaking out of the loop’s execution.
- `continue` is used to skip the current iteration and move to the next iteration in a loop.
- These statements are especially useful for fine-grained control in loops.
- Example:
Public class Main {
Public static void main(String[] args) {
For (int i = 0; i < 10; i++) {
If (i == 5) {
Break; // Exit the loop when i equals 5
}
// Code here won’t execute when i equals 5 } }
Comments
Post a Comment