Table of Contents

Repeated loops are used to execute a statement repeatedly, until a certain condition is satisfied.”What is a loop in Java?” The answer can be said, when we want to do a particular task by JavaScript and when i want to run that job until a certain condition is met, it is called a loop.

Generally, statements are executed sequentially. For example: the first statement of a function is executed first, then goes down and the second statement then follows the second statement, and after the third step, the control re-evaluates the condition. This way it goes.

A “specific operation” and “condition” is inside the body of the loop. First verify that condition is true or false. If true, he enters the loop and executes that code. If false, do not enter the loop and starts verifying the next statement.

 With an example. For example: when given homework from school, that page would have to be written the same line again and again until the homework was completed. Here “write a page” this is a condition and the sentence that had to be written is the statement.

What kinds of Loops are there in Java?

Any kind of condition in programming is done with a repeating loop.

Here are three types of loops in Java:

  1. For Loop.
  2. While Loop.
  3. Do-While Loop.

Java for Loop vs. while Loop vs. do-while Loop :-

Differencefor loopwhile loopdo-while loop
1.Definition Java “for loop” is a control flow statement, which until certain conditions are met repeats a portion of the program multiple times and after performing the code each time the quality is decreased / increased.Java “while loop” is a control flow statement that repeatedly executes a part of the program based on the given Boolean condition and decreases/increase the value each time the code is executed.Java “do while loop” is a control flow statement that executes a part of the programs at least once (Whether the condition is true or false) and the further execution depends upon the given Boolean condition.
2.Use This is used if the number of iterations is fixed.This is used if the number of iterations is not fixed.If the number of iterations is not fixed and you must execute the loop at least once, it is used.
3.Syntax for(init; condition; incr( ) / decr(–)){
// code goes here…
}
while(condition){
// code goes here…
incr( ) / decr(--)
}
do{
// code goes here…
incr( ) / decr(--)
}while(condition);

1.“For” Loop:

        
   It’s (For Loop) used to repeat a part of the program. In my opinion, the for loop is the most common of the three types of loops. “What is loop in Java?” We first learned about this. If the number of iterations is fixed, it is recommended to use “for loop”. A “for loop” is the same as C/C++.

The code flow will be as follows:

1. The loop will first check if the condition is true.

2. If the condition is true then it goes inside the block and executes the code block.

3. Each time the code block is executed it increases / decreases the value of the variable.

4. Repeat step 1.

Syntax:
Captrtdfggrure.PNG
Flow Diagram:
Captretgaertgure.PNG

It is made up of four parts – “initialize value of the variable, check condition, increment / decrement value and statement“.

A brief way to write the loop structure of “for loop” is provided:

Initialization:  Sets or initialize a variable before the loop starts (int i = 0). Here we can input the value of the variable outside the loop, or we can use the initial variable inside. This is an optional condition.

Check condition:   Next, the loop defines the condition to run. If the condition is true then the loop will start, if it is false then the loop will not start. Again if the condition is true then it continues to execute until the condition is false. Then it will go to the next statement.

Statement:   The statement of the loop is executed each time until the condition is false.

Increment/Decrement:   Each time the code block in the loop is executed it increased / decreased a value of the variable (i ++ / i–).

What is the loop in Java?” We already know about it first.

A statement is executed repeatedly until certain conditions are met, it’s called loop.

 

  Example:

var i;

for(i=1; i<=30; i++){

          if(i%2==1){

                   document.write(“The value of i is:  “,i,”<br>”);     

               }

}

The output of this program is:

The value of i is: 1
The value of i is: 3
The value of i is: 5
The value of i is: 7
The value of i is: 9
The value of i is: 11
The value of i is: 13
The value of i is: 15
The value of i is: 17
The value of i is: 19
The value of i is: 21
The value of i is: 23
The value of i is: 25
The value of i is: 27
The value of i is: 29

Visit to learn more about “for loop”- w3schools.com. This is a great site for web designers and developers.

2.“While” loop:

Java “while loop” is a control flow statement that repeatedly executes a part of the program based on the given Boolean condition and decreases/increase the value each time the code is executed. “while loop” works just like “for loop“.  There is no significant difference between the two. Structurally, differences can be seen between them. Their work is very similar.

The code flow will be as follows:

1. The loop will check if the condition is true.

2. If the condition is true then the code block executes.

3. Each time the code block in the loop is executed it increased / decreased a value of the variable.

4. Repeat step 1

Syntax:
Capedfarture.PNG
Flow Diagram:
CapAfdeAWFgrure.PNG

When the loop starts checking the condition. If the condition is evaluated to true, then the body statements in the loop are executed and the loop increased /decreased a value of the variable each time.

Otherwise the loop executes the next statement. That’s why it’s also called an “entry control loop“.

Frequently asked to be reminded. “What is the loop in Java?” We already know about it first. A statement is executed repeatedly until certain conditions are met, it’s called loop.

Example:

var i=20;

while(i>=1){

         document.write(“The value of i is:  “,i,”<br>”);

          i–;

}

The output of this program is:

The value of i is: 20
The value of i is: 19
The value of i is: 18
The value of i is: 17
The value of i is: 16
The value of i is: 15
The value of i is: 14
The value of i is: 13
The value of i is: 12
The value of i is: 11
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2
The value of i is: 1

Visit to learn more about “while loop”- Study.com

3.“ Do-While ” Loop:

Java “do while loop” is a control flow statement that executes a part of the programs at least once (Whether the condition is true or false) and the further execution depends upon the given Boolean condition.

“Do-while loop” “while loop” is the same. Yet some differences can be noted. Such as, when we use the ” while loop”, it checks the condition first and then executes the code block. On the other hand “Do-While” loop will always execute the code in your block at least once.

The code flow will be as follows:

1. The “do well” loop executes code block once before checking if the condition is true.

2. Then it verifies the condition.

3. As long as the condition is true it will repeat the loop. If false, it will not repeat the loop.

Syntax:
Captutrhysrthre.PNG

Flow Diagram:
CaptuefAWEfre.PNG

Example:

var i=20;

do {

          document.write(“The value of i is:  “,i,”<br>”);

          i–;

} while (i<1);

The output of this program is:

The value of i is: 20

Visit to learn more about “while loop”- tutorialspoint.com.

Learn more about the details- visit here.

It turns look out, the code block executes once, even if the condition is false.

I tried to give a complete idea about the loop.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on whatsapp
WhatsApp
Share on email
Email
Md. Nurnobi Hosen
Md. Nurnobi Hosen

Student of Computer Science and Engineering at Daffodil International University,Main Campus ,Dhanmondi.
Web and App Developer also expart of wordpress customization

All Blog Posts »
Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
oprol evorter
oprol evorter
December 11, 2019 5:41 AM

Oh my goodness! a tremendous article dude. Thank you However I’m experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting similar rss downside? Anyone who is aware of kindly respond. Thnkx

trackback
Places and Guideline: “Learn JavaScript” Online - Nurnobi Shanto
December 12, 2019 1:47 PM

[…] An important element in JavaScript is Loop. See here about loop. […]

2
0
Would love your thoughts, please comment.x
()
x