githubEdit

Day 05 - 04 Sep - Loop start πŸš€

for Loop

In Java, a for loop is a control structure that allows you to execute a block of code repeatedly based on a specified condition. It's commonly used when you know in advance how many times you want to repeat a certain task. Let's break down the parts of a for loop:

For Loop Flowchart

Here's what each part does:

  1. Initialization: This is where you initialize a control variable (usually an integer) to a starting value. It's executed only once when the loop begins.

  2. Condition: This is a boolean expression that's checked before each iteration of the loop. If the condition is true, the loop continues to execute. If it's false, the loop terminates.

  3. Update: This part is responsible for changing the value of the control variable after each iteration. It's executed at the end of each iteration, just before re-checking the condition.

Now, let's look at an example:

In this example:

  • Initialization: int i = 1 initializes i to 1.

  • Condition: i <= 5 checks if i is less than or equal to 5.

  • Update: i++ increments i by 1 after each iteration.

Output:

The loop starts with i equal to 1. It prints "Iteration 1," increments i to 2, and so on until i becomes 6, at which point the condition becomes false, and the loop terminates.

You can use for loops for a wide range of tasks, such as iterating over arrays, processing data, or performing calculations. They provide precise control over the number of iterations, making them a valuable tool in Java programming.

While and Do --- While Loops

Let's we learn about while and do-while loops in Java, providing detailed explanations and example programs. Both of these loops are essential for java developers.

while Loop

The while loop in Java is used to repeatedly execute a block of code as long as a specified condition

is true. Here's the basic structure of a while loop:

While Loop

Condition: The loop continues executing as long as the condition evaluates to true. If it becomes false, the loop terminates.

Example:

Let's write a simple program that uses a while loop to print numbers from 1 to 5:

In this program:

  • int i = 1 initializes i to 1.

  • i <= 5 checks if i is less than or equal to 5.

  • System.out.println("Number: " + i) prints the value of i.

  • i++ increments i by 1 after each iteration.

Output:

do --- while Loop

The do-while loop is similar to the while loop but with a crucial difference: it guarantees that the block of code is executed at least once, even if the condition is initially false. Here's the basic structure:

Do --- While Loop Flowchart

Condition: The condition is checked after the block of code executes. If it's true, the loop continues; if it's false, the loop terminates.

Let's write a program that uses a do --- while loop to take input from the user until they enter a positive number:

In this program:

  • We use a do --- while loop to repeatedly prompt the user for input.

  • The loop continues until the user enters a positive number (greater than 0).

The do --- while loop ensures that the input is collected at least once, making it suitable for situations where you want a guaranteed initial execution.

Both while and do --- while loops are fundamental for controlling program flow and iteration in Java. The choice between them depends on your specific requirements.

Foreach Loop

Let’s we learn about "enhanced for loop" or "for-each loop" in Java. This loop is used for iterating over collections like arrays and lists without the need for indices. It's quite beginner-friendly and simplifies iteration. Here's how it works:

For Each Loop Flowchart
  • datatype: The type of elements in the collection.

  • element: A variable to represent each element in the collection.

  • collection: The array or collection you want to iterate over.

Example: Let's say you have an array of integers and you want to print each element:

In this example:

  • int num is used to represent each element in the numbers array.

  • for (int num : numbers) iterates over each element in the array.

  • System.out.println("Number: " + num) prints each element.

The foreach loop is efficient and simplifies the code when you need to iterate over elements in collections, making it a great choice for beginners.

Last updated