DEV Community

Elayaraj C
Elayaraj C

Posted on

Day-1 🌟 Understanding Java Statements: Normal, Conditional, and Control Statements

🧱 1. Normal Statements

A normal statement is any line of code that executes in a straightforward, sequential manner. These are the basic building blocks of a Java program.
✍️ Example:

int a = 10;
System.out.println("The value of a is: " + a);

The first line stores 10 in variable a.

The second line prints the value.
Enter fullscreen mode Exit fullscreen mode

These lines run one after the otherβ€”no decisions, no loopsβ€”just straight execution.
πŸ”€ 2. Conditional Statements

Conditional statements allow your program to make decisions. They help the program choose between different paths based on certain conditions.
βœ… if Statement

int number = 5;
if (number > 0) {
System.out.println("The number is positive.");
}

This checks if the number is greater than 0. If true, it prints a message.
πŸ”„ if-else Statement

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

Either the if or the else block will execute, not both.
πŸ“š else if Ladder

if (number < 0) {
System.out.println("Negative number.");
} else if (number == 0) {
System.out.println("Zero.");
} else {
System.out.println("Positive number.");
}

Multiple conditions, one result.
πŸ”˜ switch Statement

Used when you have multiple specific values to check.

`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");
}`

πŸ” 3. Control (Looping) Statements

Control statements are used to repeat a block of code as long as a certain condition is true.
πŸ”‚ for Loop

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

Runs the loop 5 times, printing values from 1 to 5.
♻️ while Loop

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

Similar to for, but separates initialization and update.
πŸ” do-while Loop

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

This loop runs at least once, even if the condition is false initially.
🧭 Bonus: Jump Statements (break, continue)

break – exits a loop or switch.

continue – skips the current iteration and moves to the next one.
Enter fullscreen mode Exit fullscreen mode

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

Output: 1, 2, 4, 5 (skips 3)
🧠 Final Thoughts

  • Normal statements execute in order.
  • Conditional statements let you make choices.
  • Control statements let you repeat actions.

Top comments (0)