Object Oriented Programming with Java

⌘K
  1. Home
  2. Docs
  3. Object Oriented Programmi...
  4. Fundamental Programming S...
  5. Control Statements in Java

Control Statements in Java

Control statements in Java are used to control the flow of execution of the program based on certain conditions.

  • Based on specific conditions, you can decide which block of code should be executed, repeated, or skipped.
  • Conditional Statements
  • Looping Statements
  • Jump Statements

Conditional statements, also known as Selection statements, are control statements that allows a program to make decisions and execute certain blocks of code only when specific conditions are met.

  • Conditional statements evaluate expressions (conditions) that return either true or false and perform actions based on these evaluations.

Types of Conditional Statements in Java:

  • The if Statement
  • The if-else statement
  • The if-else if-else statement
  • The switch Statement

a.) The if Statement:

  • The if statement is used to execute a block of code if a condition is true. If the condition is false, it is skipped.

Syntax:

if (condition) {
    // Code to execute if condition is true
}

Example:

int age = 18;
if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

b.) The if-else statement:

  • The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.

Syntax:

if (condition) {
    // Code if true
} else {
    // Code if false
}

Example:

int number = 5;
if (number % 2 == 0) {
    System.out.println("Even number");
} else {
    System.out.println("Odd number");
}

c.) The if-else if-else Statement:

The if-else if-else statement is used when you have multiple conditions to check. It allows you to test multiple conditions and execute code based on the first true condition.

Syntax:

if (condition1) {
    // Code
} else if (condition2) {
    // Code
} else {
    // Default code
}

Example:

int score = 75;
if (score >= 90) {
    System.out.println("Grade A");
} else if (score >= 75) {
    System.out.println("Grade B");
} else {
    System.out.println("Grade C");
}

d.) The switch Statement

The switch statement is used for scenarios where multiple conditions need to be checked.

Syntax:

switch (expression) {
    case value1:
        // Code
        break;
    case value2:
        // Code
        break;
    default:
        // Default code
}

Example:

int day = 3;
switch (day) {
    case 1: System.out.println("Sunday"); break;
    case 2: System.out.println("Monday"); break;
    case 3: System.out.println("Tuesday"); break;
    default: System.out.println("Invalid day");
}

Looping statements are used to execute a block of code repeatedly, either for a specific number of times or until a condition is met.

  • A loop continues to execute as long as its condition remains true.
  • Loops help in situations where repetitive tasks are required.

Types of Looping Statement in Java:

  • for loop
  • while loop
  • do-while
  • for-each

a.) for loop

The for loop is used when you know beforehand how many times you want to execute a statement or a block of statements.

Syntax:

for (initialization; condition; update) {
    // code block to be executed
}

Example:

for (int i = 1; i <= 5; i++) {
    System.out.println("Count: " + i);
}

b.) while loop

The while loop is used to execute a block of code as long as the specified condition is true. It checks the condition before executing the loop.

Syntax:

while (condition) {
    // code block to be executed
}

Example:

int i = 1;
while (i <= 5) {
    System.out.println("Count: " + i);
    i++;
}

c.) do-while Loop:

The do-while loop is used to executes the code block at least once before checking the condition.

Syntax:

do {
    // code block to be executed
} while (condition);

Example:

int i = 1;
do {
    System.out.println("Count: " + i);
    i++;
} while (i <= 5);

d.) for-each loop:

It is best for looping through arrays or collections when index is not needed.

Syntax:

for (dataType item : arrayName) {
    // code block to be executed
}

Example:

int[] numbers = {10, 20, 30};
for (int num : numbers) {
    System.out.println(num);
}

Jumping statements are used to alter the normal flow of program execution in loops or conditional blocks.

  • They help manage the execution flow by skipping iterations, exiting loops, or stopping the program.

Java provides three primary jumping statements:

  • break Statement
  • continue Statement
  • return Statement

a.) break Statement

The break statement is used to exit from a loop or a switch statement prematurely. It stops the execution of the loop or switch and transfers control to the next statement outside of the loop.

Example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break; // exits loop when i is 3
    }
    System.out.println("i = " + i);
}

b.) continue Statement

The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It does not terminate the loop; it just skips the code below it for the current iteration.

Example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // skips printing when i is 3
    }
    System.out.println("i = " + i);
}

c.) return Statement

The return statement exits from the current method and optionally returns a value.

Example:

public class ReturnExample {
    public static void main(String[] args) {
        System.out.println("Start");
        test();
        System.out.println("End");
    }

    static void test() {
        System.out.println("Inside test");
        return;
        // code here won't run
    }
}

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *