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 second you were transposed into Earl’s karmic-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.

This is just a simple example. In this tutorial, you will learn to write interesting programs using the circle.

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 (;;) {
}