Loop Control Statements in Java Programming Language
Loop Control Statements in Java are used to execute a particular code several.Statements are executed sequentially, and once the condition tests false,the loop is exited.
Example:
if the above code we used break dd, execution of inner loop would stop but continue with the outer loop.
Example:
Output:
Output:
Types of Loop Control Statements in Java:
There are three types of Loop Control Statements in java.
- 1.Java for loop
- 2.Java for each loop also know as enhanced for loop.
- 3.Labeled for loop
Java for Loop
Java for loop is just like C++ for loop if you're familiar with the language
Syntax
for(variable initialization;condition to be tested;increment or decrement the variable)
{
//if condition is true,execute this code
}
Example:
public class JavaForLoop {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
for(int marks=0;marks<=10;marks++)
{
System.out.println("Mark is "+marks);
}
Output
Mark is 0
Mark is 1
Mark is 2
Mark is 3
Mark is 4
Mark is 5
Mark is 6
Mark is 7
Mark is 8
Mark is 9
Mark is 10
BUILD SUCCESSFUL (total time: 0 seconds)
Java for each loop
For each loop in java is used to iterate through an array or collections likes lists etc:Arrays elements are used instead of index.
Syntax:
for(certain variable:array/collection)
{
//execute this code block
}
Example:
public class ForeachLoop {
public static void main(String args[])
{
int marks[]={22,11,33,44,55,66,77,88,76,23,12,16};//initialize array
for(int a:marks)
{
System.out.println("Mark is "+a);
}
}
}
Output:
Mark is 22
Mark is 11
Mark is 33
Mark is 44
Mark is 55
Mark is 66
Mark is 77
Mark is 88
Mark is 76
Mark is 23
Mark is 12
Mark is 16
Labeled for Loop
Labeled for loop uses break and continue keywords to get out of the inner loop:
Syntax:
labeledname;
for(initialize variable;test condition;increment or decrement variable){
// executed this code if true
}
Example:
public class LabeledForLoopExample {
public static void main(String args[])
{
kk:
for(int i=1;i<=3;i++){
dd:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break kk;
}
System.out.println(i+" "+j);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
BUILD SUCCESSFUL (total time: 0 seconds)
if the above code we used break dd, execution of inner loop would stop but continue with the outer loop.
Example:
public class LabeledForLoopExample {
public static void main(String args[])
{
kk:
for(int i=1;i<=3;i++){
dd:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break dd;
}
System.out.println(i+" "+j);
}
}
}
}
Output:
run:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
BUILD SUCCESSFUL (total time: 0 seconds)
Infinitive For Loop
To create an infinitive for loop,we use two semicolons;;
Syntax:
for(;;){
//execution code
}
public class InfinitiveLoop {
public static void main(String args[])
{
for(;;)
{
System.out.println("Infinitive loop Example");
}
}
}
Output:
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop Example
Infinitive loop ExampleBUILD STOPPED (total time: 2 seconds)