Skip to main content

Control Flow Statements in Java – Loops & Conditional Statements

 

Control Flow Statements in Java – Loops & Conditional Statements

Control flow statements in Java allow you to control the execution of your code based on conditions and loops. In this post, we will explore:

  1. Conditional Statementsif, if-else, if-else-if, switch
  2. Looping Statementsfor, while, do-while
  3. Jump Statementsbreak, continue, return

Let’s break them down with examples.


1. Conditional Statements in Java

1.1 if Statement

The if statement executes a block of code if a condition is true.

Syntax:

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

Example:

public class IfExample {
public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println("You are eligible to vote."); } } }

1.2 if-else Statement

Executes one block of code if the condition is true, otherwise executes another block.

Example:

public class IfElseExample {
public static void main(String[] args) { int num = 10; if (num % 2 == 0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }

1.3 if-else-if Ladder

Used to test multiple conditions.

Example:

public class IfElseIfExample {
public static void main(String[] args) { int marks = 85; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else if (marks >= 50) { System.out.println("Grade: C"); } else { System.out.println("Fail"); } } }

1.4 switch Statement

Used when multiple conditions depend on a single variable.

Example:

public class SwitchExample {
public static void main(String[] args) { 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("Invalid day"); } } }

2. Looping Statements in Java

Loops execute a block of code multiple times based on a condition.

2.1 for Loop

Used when the number of iterations is known.

Example:

public class ForLoopExample {
public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Iteration: " + i); } } }

2.2 while Loop

Executes the block as long as the condition is true.

Example:

public class WhileExample {
public static void main(String[] args) { int i = 1; while (i <= 5) { System.out.println("Count: " + i); i++; } } }

2.3 do-while Loop

Executes the block at least once, even if the condition is false.

Example:

public class DoWhileExample {
public static void main(String[] args) { int i = 1; do { System.out.println("Number: " + i); i++; } while (i <= 5); } }

3. Jump Statements in Java

3.1 break Statement

Used to exit a loop or switch case.

Example:

public class BreakExample {
public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { break; } System.out.println(i); } } }

3.2 continue Statement

Skips the current iteration and continues to the next.

Example:

public class ContinueExample {
public static void main(String[] args) { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.println(i); } } }

3.3 return Statement

Exits from a method and returns a value.

Example:

public class ReturnExample {
public static void main(String[] args) { System.out.println("Sum: " + add(5, 10)); } public static int add(int a, int b) { return a + b; } }

Conclusion

Control flow statements help in making decisions and repeating actions. They are essential for writing efficient Java programs. In the next post, we will discuss Java Methods – Creating & Using Functions.

Comments