0% found this document useful (0 votes)
2 views5 pages

Loops in Java

The document provides an overview of loops in Java, including the for loop, for-each loop, labeled for loop, while loop, and do-while loop. It explains the syntax and usage of each loop type, along with examples demonstrating their functionality. The document emphasizes the appropriate use cases for each loop based on whether the number of iterations is fixed or variable.

Uploaded by

Scz Mcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Loops in Java

The document provides an overview of loops in Java, including the for loop, for-each loop, labeled for loop, while loop, and do-while loop. It explains the syntax and usage of each loop type, along with examples demonstrating their functionality. The document emphasizes the appropriate use cases for each loop based on whether the number of iterations is fixed or variable.

Uploaded by

Scz Mcb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loops in Java

For loop in java:-

Java Simple for Loop


1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the
variable, or we can use an already initialized variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues
execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
4. Statement: The statement of the loop is executed each time until the second condition is false.

Syntax:

1. for(initialization; condition; increment/decrement){


2. //statement or code to be executed
3. }
The 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.
Here are some example of for loop:- 1  If we have a for loop inside the another loop, it
 Iterating over an array: 2 is known as nested for loop. The inner loop
3 executes completely whenever outer loop
int[] numbers = {1, 2, 3, 4, 5}; 4 executes.
5 {
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);  Nested loops:
}
for (int i = 1; i <= 3; i++) {
 Iterating over a collection (e.g., ArrayList): for (int j = 1; j <= 3; j++) {
System.out.print(i * j + " ");
ArrayList<String> fruits = new ArrayList<>(); }
fruits.add("Apple"); Output: System.out.println();
fruits.add("Banana"); - }
fruits.add("Orange"); Output:-
Apple
for (String fruit : fruits) { 123
Banana
System.out.println(fruit); 246
Orange
} 369

 Count-controlled loop: Ex:-2


Iteration: 0
Iteration: 1
for (int i = 0; i < 5; i++) {
Iteration: 2 1. public class NestedForExample {
System.out.println("Iteration: " + i);
}
Iteration: 3 2. public static void main(String[] args) {
Iteration: 4
3. //loop of i outpu
 Looping through a range of numbers: 4. for(int i=1;i<=3;i++){ t
11
5. //loop of j 12
for (int i = 1; i <= 10; i++) {
1 2 3 4 5 6 7 8 9 10 6. for(int j=1;j<=3;j++){ 13
System.out.print(i + " ");
21
} 7. System.out.println(i+" "+j); 22
23
8. }//end of i
31
9. }//end of j 32
33
10. }
Pyramid Example 1: 2.11.public static void main(String[] args) {
PyramidExample.java }
3. for(int i=1;i<=5;i++){
4. for(int j=1;j<=i;j++){
1. public class PyramidExample { 5. System.out.print("* ");
6. } 2. public static void main(String[] args) {
7. System.out.println();//new line 3. int term=6;
8. } 4. for(int i=1;i<=term;i++){
9. } 5. for(int j=term;j>=i;j--){
10. } 6. System.out.print("* ");
7. }
Output: 8. System.out.println();//new line
9. }
*
10. }
** 11. }
***
Output:
****
***** ******
*****
****
Pyramid Example 2: ***
PyramidExample2.java
**
*
1. public class PyramidExample2 {

Java for-each Loop


The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don't
need to increment value and use subscript notation. It works on the basis of elements and not the index. It returns element
one by one in the defined variable.

Syntax:

for(data_type variable : array_name){


//code to be executed
}

ForEachExample.java

//Java For-each loop example which prints the

//elements of the array output


public class ForEachExample {
public static void main(String[] args) { 12
23
//Declaring an array
44
int arr[]={12,23,44,56,78}; 56
//Printing array using for-each loop 78
for(int i:arr){
System.out.println(i);
}
}
}

Java Labeled For Loop


We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the
nested for loop as we can break/continue specific for loop.

Note: The break and continue keywords breaks or continues the innermost for loop respectively.
Syntax:
labelname:
for(initialization; condition; increment/decrement){
//code to be executed
}
 LabeledForExample.jav
a  LabeledForExample2.java
//A Java program to demonstrate the use of labeled for loop
1. public class LabeledForExample2 {
1. public class LabeledForExample {
2. public static void main(String[] args) {
2. public static void main(String[] args) {
3. //Using Label for outer and for loop
3. aa:

4. aa: 4. for(int i=1;i<=3;i++){


5. for(int i=1;i<=3;i++){ 5. bb:
6. bb: 6. for(int j=1;j<=3;j++){
7. for(int j=1;j<=3;j++){ 7. if(i==2&&j==2){
8. if(i==2&&j==2){
8. break bb;
9. break aa;
10. }
9. }

11. System.out.println(i+" "+j); 10. System.out.println(i+" "+j);


12. } 11. }
13. } 12. }
14. } 13. }
15. }
14. }

Output: Output:-
11
11
12
12
13
13 21
21 31
32
33

If you use break bb;, it will break inner loop only which is the default behaviour of any loop.

Java While Loop


The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As
soon as the Boolean condition becomes false, the loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to
use the while loop.

Syntax:
while (condition){
//code to be executed
I ncrement / decrement statement
}

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to
update expression. When the condition becomes false, we exit the while loop.
Example:
i <=100
2. Update expression: Every time the loop body is executed, this expression increments or decrements loop variable.
Example:
i++;

In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment
the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.

WhileExample.java

1. public class WhileExample {


2. public static void main(String[] args) {
3. int i=1;
4. while(i<=10){
5. System.out.println(i);
6. i++;
7. }
8. }
9. }
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java do-while Loop


The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of
iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop.

Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the do-while check the condition at
the end of loop body. The Java do-while loop is executed at least once because condition is checked after loop body.
Syntax:

do{
//code to be executed / loop body
//update statement
}while (condition);

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update
expression. As soon as the condition becomes false, loop breaks automatically.
Example:
i <=100
2. Update expression: Every time the loop body is executed, the this expression increments or decrements loop variable.
Example:
i++;

Note: The do block is executed at least once, even if the condition is false.

Example:

In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to
initialize and increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.

DoWhileExample.java

1. public class DoWhileExample {


2. public static void main(String[] args) {
3. int i=1;
4. do{
5. System.out.println(i);
6. i++;
7. }while(i<=10);
8. }
9. }

Output:
1
2
3
4
5
6
7
8
9
10

You might also like