How to use Loops in Java? for, while, do-while example tutorial

Hello guys, if you are learning Java and don't know programming then it can be really hard. In order to learn Java, learning programming basics like loops, conditional statement and control statement is very important. If you are looking for content to understand about Java Loops to have deep knowledge of java basics then you will definitely learn from this article. We are again here with new article that is on Java Loops will give all basics about the topic to our viewers or readers. we will be covering all questions like what, why, when, Benifits etc.


What is loop in Java

Loops in Java are an important part or module to understand the concept of writing code once to execute the same task again. In Java, we use loops to iterate our code multiple times using some conditions, statements, and other terms like initialization and increment/decrement. We also called them "control statements" because they control the flow of execution.

4 Types of Loops in Java

There are four types of loops in Java.
1. For loop
2. For-each loop
3. While loop
4. Do-while loop


1. For loop in Java

In java, we use for loop when we know how many times we have to run our loop or iterate our condition.
We also called it an entry control check loop.


Syntax:
for (initialization condition; testing condition;increment/decrement)
{
statement(s)
}







Main Idea of For loop:



↓←←←←←←←←←←←←←↑

↓ ↑

Start → Initialization → Condition check →if true →Main statement



False →end




Example:

class Demo {

public static void main (String[] args) {

for (int i=0;i<=100;i++)

{

System.out.println(i);

}

System.out.println(“we printed number from 0 to 100 using for loop”);

}

}

In order to perform nesting, we can place one loop inside another, but this adds to the time complexity.



2. For-each loop in Java

Java's for-each loop is used to iterate through an array or collection.Because we don't have to use condition or increment value,it is simpler to use than a simple for loop.



Syntax:
for(data_type variable : array_name){
//body of code
}





Example:

class Demo {

public static void main (String[] args) {

Int[] arr={2,4,6,8,10};

for (int i:arr)

{

System.out.println(i);

}

System.out.println(“we printed number from 0 to 100 using for-each loop”);

}

}





3. While loop in Java

This is also an entry control check loop. In the while loop, we iterate our loop till particular condition is not satisfied. So we can say we don’t know how many time our loop will execute.



Syntax :

while (boolean condition)

{

Body code

}





Main Idea of While Loop:




↓←←←←←←←←←←←←←↑

↓ ↑

Start →→ Condition check →if true →Main statement



False →end






Example:

class Demo {

public static void main (String[] args) {

int i=2;

while (i<=10 && i%2==0)

{

System.out.println(i);

i+=2;

}

System.out.println(We used while loop to print table of 2);

}

}





4. Do while loop

This is an exit control check loop because the condition is checked at the end of loop. Like while loop, we iterate do while loop till a particular condition is not satisfied but it also executes a statement one time even if the condition is false.



Syntax:

do

{

Body code

}

while (condition);





Main Idea of Do-while Loop:

if true

↓←←←←←←←←←↑

↓ ↑

Start →→ Main statement →Condition check



False →end



Example:

class Demo {

public static void main (String[] args) {

int i=100;

do

{

System.out.println(i);

i- -;

}while(i<100 && i>0);

System.out.println(we are also printing number 100 even are condition is only to print number less than 100 and greater than 0);

}

}





Difference between for loop, while loop and Do while loop in Java

Here are the key difference between these loops in Java

ComparisonFor LoopWhile LoopDo-While Loop
AboutA control flow statement that repeatedly iterates through a section of a program is the Java for loop.The while loop in Java is a control flow statement that repeatedly runs a section of the program depending on a given boolean condition.The Java do-while loop is a control flow statement that runs a section of code at least once and determines whether to run it again based on a given boolean condition.
Infinite Loop Syntaxfor(;;){ //code }while(true){ //code }do{ //code }while(true);
When to ApplyUse of a for loop is advised if the number of iterations is fixed.Use of the while loop is advised if the number of iterations is not fixed.It is advised to use a do-while loop if the number of iterations is not fixed and you must run the loop at least once.



That's all about what are loops in Java and how to use them. This article describes about what is loop, types of loops in Java using examples and various implementations.  understanding and effectively utilizing loops in Java is an essential skill for any programmer. Loops, including the for loop, while loop, and do-while loop, provide powerful tools for automating repetitive tasks and iterating through data structures.

In this tutorial, we've explored the syntax and application of each type of loop, offering real-world examples to illustrate their usage. By mastering these loop constructs, you can enhance your programming capabilities and write more efficient and organized code.

Remember that choosing the right type of loop depends on the specific requirements of your program. Use the for loop when you have a fixed number of iterations, the while loop when the iteration count is determined by a condition, and the do-while loop when you need to ensure at least one execution.

As you continue your journey in Java programming, practice and experimentation will be your greatest allies. With loops at your disposal, you have a versatile toolset for tackling a wide range of programming challenges. Whether you're processing data, searching for elements, or simply automating repetitive tasks, loops are your reliable companions on the road to becoming a proficient Java developer.

I hope reading it taught you something new.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.