Java Control Statement

Controlled Statement in Java

In Java, controlled statements are used to control the flow of execution within a program based on certain conditions. These statements allow developers to execute specific code blocks depending on the result of a condition or to repeat actions multiple times. Controlled statements make programs more dynamic and adaptable by enabling decision-making and iterative actions.

There are several types of controlled statements in Java:

1. Conditional Statements:
    ○ These include if, if-else, if-else-if, and switch statements.
    ○ They allow the program to execute certain blocks of code only when specific conditions are met.

Example:

int age = 18;
if (age >= 18) {
    System.out.println(“Adult”);
} else {
    System.out.println(“Minor”);
}

2. Looping Statements:
    ○ These include for, while, and do-while loops.
    ○ Looping statements enable a block of code to be executed repeatedly as long as a condition is true, or for a
       specific number of times.

Example:

for (int i = 0; i < 5; i++) {
    System.out.println(“Iteration ” + i);
}

3. Jump Statements:
    ○ These include break, continue, and return statements.
    ○ They allow the program to jump out of a loop, skip a part of a loop, or exit a method.

Example:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exits the loop when i is 5
    }
    System.out.println(i);
    }

Controlled statements are essential for building logical and efficient Java applications, as they provide the ability to perform tasks conditionally and handle repetitive actions in a structured way.

Tasks:

1: Age Classification

Objective: Use conditional statements to classify a person’s age.
    ● Create a class named Person with properties name and age.
    ● Write an if-else statement to determine if the person is a “Child” (age < 13), “Teen” (age >= 13 and < 18), or
      “Adult” (age >= 18).
    ● Print the classification based on the person’s age.

Example Output:

Person person = new Person(“John”, 15);

System.out.println(“Classification: Teen”);

2: Odd or Even Checker with Loop

Objective: Use a looping statement and conditional check to find even numbers.
    ● Write a program that takes a number n and prints whether each number from 1 to n is odd or even.
    ● Use a for loop to iterate through numbers from 1 to n.
    ● Use an if-else condition within the loop to check if a number is odd or even.

Example Output for n = 5:

1 – Odd
2 – Even
3 – Odd
4 – Even
5 – Odd

3: Number Guessing Game

Objective: Use looping and jump statements to create a simple number guessing game.
    ● Generate a random number between 1 and 50.
    ● Allow the user to guess the number using a loop.
    ● If the guessed number is correct, print “Correct Guess!” and exit the loop using break.
    ● If the guess is too high, print “Too High”; if too low, print “Too Low.”
    ● End the loop after 5 guesses and print “Game Over” if the number is not guessed.

Example Output:

Guess 1: Too High
Guess 2: Too Low
Guess 3: Correct Guess!

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

The training covers Java control statements, including conditional statements (if, else, switch), loop statements (for, while, do-while), and jump statements (break, continue).

Yes, it includes examples demonstrating how to use various control statements to manage the flow of a Java program.

Yes, exercises involve implementing different control structures to solve common programming problems.

Yes, it’s designed for beginners with clear explanations and step-by-step examples

Yes, it covers how control statements allow for decision-making, looping, and branching in Java programs, enhancing their functionality.

Yes, the training is self-paced and accessible online anytime.

Yes, it explains the distinctions and appropriate use cases for different control statements in Java.

It typically takes a few hours to complete, depending on your learning pace.