Loop control is one of java's language feature like any other programming language feature, which allows to execute written statements in loop body to execute sequentially those many time until loop condition not get satisfied.
Java programming language provides four types of loop controls or constructs.
Java programming language provides four types of loop controls or constructs.
- while loop
- do...while loop
- for loop
- for-each loop
we will see all those constructs with example one by one.
while loop
- while loop execute the statements or group of statements multiple times unless given loop condition not fail
- while loop checks for condition first then execute statements in loop body
- repeat above step till condition satiesfied
see, below code snippet explains while loop:
Output:
Output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
int index = 1; | |
while (index <= 5) { //check for condition first | |
System.out.println("Index:" + index); | |
index = index + 1; //increase index otherwise loop will never end | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index:1 | |
Index:2 | |
Index:3 | |
Index:4 | |
Index:5 | |
Process finished with exit code 0 |
do...while loop
- do...while loop checks for condition later executing body of loop
- unlike while loop it does not check condition first, this is one of major difference between do--while and while loop
- like while loop, it executes loop body until condition not failed
- in failure condition scenario, do...while loop executes at-least once.
see, below code snippet:
Output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
int index = 1; | |
do { | |
System.out.println("Index:" + index); | |
index = index + 1; //increase index otherwise loop will never end | |
} while (index <= 5);//check for condition later | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index:1 | |
Index:2 | |
Index:3 | |
Index:4 | |
Index:5 | |
Process finished with exit code 0 |
for loop
- for loop is useful when you know how many times the loop body to be executed.
- this loop construct allows you to compactly define or initialise loop condition then define how long condition will stay true and condition update statement
- unlike do...while loop for loop checks for condition after condition initialisation, then execute body and then execute condition update statement
- repeat above step until condition stays true.
see, below code snippet:
Output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
for(int i = 1 ; i <= 5 ; i++){ //condition <initialisation> ; <condition validation> ; <condition update> | |
System.out.println("Index:"+i); //execute body | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index:1 | |
Index:2 | |
Index:3 | |
Index:4 | |
Index:5 | |
Process finished with exit code 0 |
for each loop
- for each loop is advance loop in java.
- it introduced to improve code readability and beautify code.
- this loop iterates over array and Iterable type objects
- one can nest for each loop inside for each loop
- for each is not useful for filtering list or array.
- it travers list sequentially without index to access elements
see, below code snippet:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
int[] idArray = {1, 2, 3, 4, 5}; //traverse over array | |
for (int id : idArray) { //read it as: for each id in idArray | |
System.out.println("Id:" + id);// print id | |
} | |
//traverse over list of String objects | |
List<String> dictonary = Arrays.asList("John", "Ana", "George", "Sara"); | |
for (String name : dictonary) { //read it as: for each name in dictonary | |
System.out.println("Name:" + name); // print name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Id:1 | |
Id:2 | |
Id:3 | |
Id:4 | |
Id:5 | |
Name:John | |
Name:Ana | |
Name:George | |
Name:Sara | |
Process finished with exit code 0 |
Comments
Post a Comment