blog posts

Loop

Learn Loop For In Java

Loops are used in programming to repeat a specific block of code. In this tutorial, you can create a for loop in Java programming. 

Loops are used in programming to repeat a specific block of code until certain conditions are met (the condition is false).

Rings are what make computers interesting machines. Imagine, for a moment, that you were transposed into Earl’s karmically driven world. You can do this using the print Command 50 times (without a loop).

How do you want to print a sentence a million times? So you have to use the rings.

What Are Loops in Java?
Loops in Java are a fundamental feature that allows a specific section of a program to be executed repeatedly as long as a given condition remains true. While the core purpose of all loop types is similar, they differ significantly in syntax and usage.

For Loops in Java
The for loop in Java is designed to iterate over a block of code a predetermined number of times, making it ideal for tasks like processing arrays or performing calculations. Mastering for loops is essential for efficient iteration and data handling. To dive deeper into for loops and other Java programming constructs, consider enrolling in a Java course.

Comparing For, While, and Do-While Loops
Java offers three main types of loops—for, while, and do-while—each with distinct characteristics in terms of syntax, condition checking, and optimal use cases. Below is a breakdown of their differences:

AspectFor LoopWhile LoopDo-While Loop
IntroductionIterates a set of statements a specific number of times.Executes statements until a boolean condition becomes false.Runs statements at least once, then repeats until a boolean condition is false.
Best Use CaseUse when the exact number of iterations is known (e.g., array processing).Use when the number of iterations is unknown.Use when the loop must execute at least once, even if the condition is unknown.
Syntaxfor (init; condition; icr/dcr) { // statements }while (condition) { // statements }do { // statements } while (condition);
Examplefor (int x = 0; x <= 5; x++) { System.out.println(x); }int x = 0; while (x <= 5) { System.out.println(x); x++; }int x = 0; do { System.out.println(x); x++; } while (x <= 5);

This article will delve into the for loop in Java in greater detail, but understanding the distinctions between these loop types is crucial for selecting the right one for your programming needs.

Overview of For Loops
A for loop in Java is used to repeatedly execute a block of code a fixed number of times, making it ideal when the number of iterations is known in advance. Let’s break down its syntax and functionality to understand how it works.

Syntax of a For Loop

for (init; condition; incr/decr) { // statements to be executed or body }
  • init: This expression initializes a variable (e.g., int x = 0) and is executed only once at the start of the loop.
  • condition: This boolean expression (e.g., x <= 5) is evaluated before each iteration. If true, the loop body executes; if false, the loop terminates.
  • incr/decr: This statement (e.g., x++) updates the initialized variable after each iteration to eventually make the condition false.

Flow of a For LoopFlow of a For Loop

  1. The init expression is executed once to set the starting value.
  2. The condition is checked; if true, the loop body runs.
  3. After each iteration, the incr/decr statement updates the variable.
  4. The condition is re-evaluated, and the loop continues until the condition becomes false.

Example of a For Loop
Here’s a simple example to illustrate how a for loop operates in Java:

public class ForExample { public static void main(String[] args) { for (int x = 0; x <= 5; x++) { System.out.println(x); } } }

OutputFlow of a For Loop

In this example, the loop starts with x = 0, checks the condition x <= 5, prints the value of x, and increments x by 1 in each iteration until the condition becomes false.

Ring for

The loop structure in Java is as follows:

for (initialization; testExpression; update)

{

// codes inside for loop's body

}

How does the for loop work?

1- The initialization is performed only once.

2. Then the condition is evaluated as a Boolean expression.

3- If the condition is evaluated correctly,

  • The codes inside the body of the loop are executed.
  • The Update statement is then executed.
  • Again, the condition is evaluated.
  • If the condition is correct, the code inside the loop’s body and the Update statement are executed.
  • This process continues until the condition is evaluated incorrectly.

4- If the condition is evaluated incorrectly, the for loop ends.

Ring flowchart forLoop

Example 1: ring for

  1. // Program to print a sentence 10 times
  2. class Loop {
  3. public static void main (String [] args) {
  4. for (int i = 1; i <= 10; ++ i) {
  5. System.out.println (“Line” + i);
  6. }
  7. }
  8. }

Output

Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Here, the variable i is defined and given a value of 1.

The condition i = 10 is then evaluated. The body of the loop runs right, which prints Line 1 on the screen.

Then, the expression ++ i is executed. The value of i has now been increased to 2. Again, the condition i = 10 is evaluated to be correct, and the loop body is executed, which prints Line 2 on the screen.

This iteration process continues until i = 11. When i equals 11, the condition i <= 10 is false, and the loop ends.

Example 2: ring for

  1. // Program to find the sum of natural numbers from 1 to 1000.
  2. class Number {
  3. public static void main (String [] args) {
  4. int sum = 0;
  5. for (int i = 1; i <= 1000; ++ i) {
  6. sum + = i; // sum = sum + i
  7. }
  8. System.out.println (“Sum =” + sum);
  9. }
  10. }

Output

Sum = 500500

Here, the value of the sum variable starts at 0. Then, each time the loop is repeated, the sum variable equals sum + i, and the i value increases until it is greater than 1000.

1st iteration: sum = 0 + 1 = 1

2nd iteration: sum = 1 + 2 = 3

3rd iteration: sum = 3 + 3 = 6

4th iteration: sum = 6 + 4 = 10

… ..…

999th iteration: sum = 498501 + 999 = 499500

1000th iteration: sum = 499500 + 1000 = 500500

Java for Loop

Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is:

for (initialExpression; testExpression; updateExpression) { // body of the loop }

Here,

  1. The initialExpression initializes and/or declares variables and executes only once.
  2. The condition is evaluated. If the condition is true, the body of the for loop is executed.
  3. The updateExpression updates the value of initialExpression.
  4. The condition is evaluated again. The process continues until the condition is false.

Ring for infinityLoop

If the condition is always true, the loop will be executed forever and is called an infinite loop. For example:

  1. // Infinite for Loop
  2. class Infinite {
  3. public static void main (String [] args) {
  4. int sum = 0;
  5. for (int i = 1; i <= 10; –i) {
  6. System.out.println (“Hello”);
  7. }
  8. }
  9. }

Here, the condition i <= 10 is never false, and Hello is printed many times (at least in theory).

The initialization of the variable, the variable update, and the condition used in the for loop are optional. Here is another example of an infinite loop:

for (;;) {
}