![]() |
How To Use Loops in Java Explain Briefly Details |
How To Use Loops in Java Explain Briefly Details
Loops in Java: A java programming language loops are used to execute a set of code functions repeatedly. When some conditions become true.
It java loop is a part of control flow statements. To run the particular block of code continuously until a required condition is fulfilled is called a java loop.
There are Three Types of java loops
1. While loop
2. For loop
3. Do-while loop
Some Others Languages Loops
👉C++ Loops
👉C Loops
👉Python Loops
While Loop: While loop when program execution starts before condition check then statement execution. while loop is the most useful loop .it is called the while loop.
Syntax of while loop in java
while(condition) { //statement Increment/decrement } |
How To Use while loop in java?
class icoderweb { Public static void main(String[] args) { int i=1; while(i<=10) { System.out.print(i); i++; } } } *****OUTPUT***** 1 2 3 4 5 6 7 8 9 10 |
How To Use while loop user input in java?
import java util.Scanner; class icoderweb { public static void main(String[] args) { Scanner x=new Scanner(System.in); int n,f=1,i=1; System.out.println(“Enter any Number:”); n=x.nextInt(); while(i<=n) { f=f*i; i++; } System.out.println(“Factorisl is:”+f); } } *****OUTPUT***** Enter any number:6 Factorial is:720 |
Do-while loop: A do-while loop is used to iterate a part of the program several times.
if the number of iteration is not fixed and you must have to execute the loop at least one time executed.
The do-while loop is executed at least one time because the condition is checked after the loop statement.
Syntax of the do-while loop in java
do { //statement while(condition); } |
How To Use do-while loop in java?
class icoderweb { public static void main(String[] args) { int x=1; do { System.out.print(x); x++; } while(x<=10); } } *****OUTPUT***** 1 2 3 4 5 6 7 8 9 10 |
For Loop: A java for loop is used to iterate a part of the program several times.if the number of iteration is fixed.it is recommended to use for loop.
There are Three Types of java for Loop
1. Simple for Loop
2. For each Loop
3. Labeled Loop
Java Simple Loop: Java simple loop is the same c language. we can initialize the variable, condition check than increment value.
An initialization part is executed only one time and then a condition check. for loop is very easy to understand loop because three-parts only one line. it is called the for a loop.
Syntax of simple for loop in java
for(initialization;condition;increment/decrement) for(int i=1;i<=10;i++) { //Statement; } |
How To Use simple for loop in java?
class icoderweb { public static void main(String[] args) { for(int i=1;i<=10;i++) { System.out.print(i); } } } *****OUTPUT***** 1 2 3 4 5 6 7 8 9 10 |
For each loop: A for each loop is used to traverse array or collection in java.
it is easy to use and simple for each loop because we do not use increment value using the subscript notation.
Each loop is worked on element not index. for each loop return element one by one in the defined variables.
Syntax of for each loop in java
for(type var:array) { //statement } |
How To Use for each loop in java?
class icoderweb { public static void main(String[] args) { int a[]={1,20,30,40,50}; for(int i:a) { System.out.print(i); } } } *****OUTPUT***** 10 20 30 40 50 |
Labeled for loop: A labeled loop is used label before the for a loop.it is useful if we have nested for loop so that we can break /continue specific label for a loop.
Syntax of for each loop in java
labelname: for(initialization;condition;incre/decre) { //statement } |
How To Use Label for loop in java?
class icoderweb { public static void main(String[] args) { a: for(int i=1;i<=3;i++) b: for(int j=1;j<=3;j++) { if(i==2&&j==2) { break a: } System.out.print(i+” ”+j); } } } } *****OUTPUT***** 1 1 1 2 1 3 2 1 3 1 |