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 for
Example 1: ring for
- // Program to print a sentence 10 times
- class Loop {
- public static void main (String [] args) {
- for (int i = 1; i <= 10; ++ i) {
- System.out.println (“Line” + i);
- }
- }
- }
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
- // Program to find the sum of natural numbers from 1 to 1000.
- class Number {
- public static void main (String [] args) {
- int sum = 0;
- for (int i = 1; i <= 1000; ++ i) {
- sum + = i; // sum = sum + i
- }
- System.out.println (“Sum =” + sum);
- }
- }
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 offor
loop is:for (initialExpression; testExpression; updateExpression) { // body of the loop }Here,
- The initialExpression initializes and/or declares variables and executes only once.
- The
condition
is evaluated. If thecondition
istrue
, the body of thefor
loop is executed.- The
updateExpression
updates the value ofinitialExpression
.- The condition is evaluated again. The process continues until the condition is
false
.
Ring for infinity
If the condition is always true, the loop will be executed forever and is called an infinite loop. For example:
- // Infinite for Loop
- class Infinite {
- public static void main (String [] args) {
- int sum = 0;
- for (int i = 1; i <= 10; –i) {
- System.out.println (“Hello”);
- }
- }
- }
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 (;;) { }