blog posts

Learn Loop For In Java (In Quite Simple Language)

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 is used in programming to repeat a specific block of code until certain conditions are met (condition is false).

Rings are what make computers interesting machines. Imagine for a second you were transposed into the karmic driven world of Earl. Well, you can do this using the print command 50 times (without using a loops).

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

This is just a simple example. You will learn to use the circle to write some interesting programs in this tutorial.

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- Initialization is performed only once.

2. Then the condition (here testExpression) is evaluated which is 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 body of the loop is executed and the update statement is executed.
  • This process continues until the condition is evaluated incorrectly.

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

Ring flowchart for

C: \ Users \ Mr \ Desktop \ java-for-loop-flowchart_0.jpg

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 it is first given a value of 1.

The condition i <= 10 is then evaluated. Because, right, the body of the loop runs, 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 value of i 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

Ring for infinity

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:

  1. for (;;) {
  2. }