CodeWithAbdessamad

Control Flow

Control Flow: The Engine of Your Java Applications

Control flow is the backbone of any program — it dictates the order in which statements are executed. Without it, your code would be a static collection of instructions, unable to respond to changing conditions or user input. 🚀

In this section, we’ll dive into the two most fundamental control flow structures in Java: if/else statements and loops. By the end, you’ll be able to write conditional logic and repetitive operations with confidence.

If/Else Statements

If/else statements allow your program to make decisions based on conditions. They are the building blocks of logical reasoning in code.

Here’s a simple example:

<code class="language-java">int age = 20;
<p>if (age >= 18) {</p>
<p>    System.out.println("You are an adult.");</p>
<p>} else {</p>
<p>    System.out.println("You are a minor.");</p>
<p>}</code>

In this example, the program checks if the age variable is at least 18. If true, it prints “You are an adult.”; otherwise, it prints “You are a minor.”.

You can also chain multiple conditions using else if:

<code class="language-java">int score = 85;
<p>if (score >= 90) {</p>
<p>    System.out.println("A");</p>
<p>} else if (score >= 80) {</p>
<p>    System.out.println("B");</p>
<p>} else {</p>
<p>    System.out.println("C");</p>
<p>}</code>

This example categorizes a student’s score into letter grades.

Remember: only one branch will execute in an if/else chain. Once a condition is met, the rest are skipped.

Loops

Loops are essential for repeating actions. Java offers three primary loop structures:

  1. for loops
  2. while loops
  3. do-while loops

We’ll compare them in the table below and then dive into each.

Loop Type Syntax Example When to Use Key Notes
for for (int i = 0; i < 10; i++) When you know the number of iterations in advance Ideal for counting from start to end
while while (condition) When iterations are unknown Condition checked before each iteration
do-while do { ... } while (condition) When you need to run at least once Condition checked after the body

For Loop

The for loop is perfect when you know exactly how many times you want to run the loop. It combines initialization, condition, and update in one line.

Example:

<code class="language-java">for (int i = 0; i < 5; i++) {
<p>    System.out.println("Iteration " + i);</p>
<p>}</code>

This loop prints numbers from 0 to 4.

While Loop

The while loop runs as long as a condition is true. It’s ideal when the number of iterations isn’t fixed.

Example:

<code class="language-java">int count = 0;
<p>while (count < 3) {</p>
<p>    System.out.println("Count: " + count);</p>
<p>    count++;</p>
<p>}</code>

This loop prints "Count: 0", "Count: 1", and "Count: 2".

Do-While Loop

The do-while loop is similar to the while loop, but it runs at least once before checking the condition.

Example:

<code class="language-java">int num = 5;
<p>do {</p>
<p>    System.out.println("Number: " + num);</p>
<p>    num--;</p>
<p>} while (num > 0);</code>

This loop prints numbers from 5 down to 1.

Break and Continue

In loops, you can use break and continue to control flow:

  • break: Exits the loop immediately.
  • continue: Skips the rest of the current iteration.

Example of break:

<code class="language-java">for (int i = 0; i < 5; i++) {
<p>    if (i == 3) {</p>
<p>        break; // exits the loop when i is 3</p>
<p>    }</p>
<p>    System.out.println("Current index: " + i);</p>
<p>}</code>

This loop prints "Current index: 0", "Current index: 1", and "Current index: 2".

Example of continue:

<code class="language-java">for (int i = 0; i < 5; i++) {
<p>    if (i == 2) {</p>
<p>        continue; // skips index 2</p>
<p>    }</p>
<p>    System.out.println("Current index: " + i);</p>
<p>}</code>

This loop prints "Current index: 0", "Current index: 1", and "Current index: 3", "Current index: 4".

Summary

In this section, we explored the two most fundamental control flow structures in Java: if/else statements and loops. You now understand:

  • How to use if/else to make decisions based on conditions.
  • How to use loops (for, while, do-while) to repeat actions, along with break and continue to control flow.

Mastering these concepts is essential for building responsive and efficient Java applications. 🚀