In programming, there are situations where the set of instructions is to be represented multiple times. Let us take a real-life situation. You are asked by your gym trainer, to do a push-up. It is inconvenient for him as well as you, if he/she instructs after every pushup. Instead, he/she instructs you in a single line, do push up 50 times. As a result, you repeat the pushup fifty times and then stop.
Such statements that allow executing a group of instructions to occur multiple times and abort upon negating a condition is called loop statement.
There are 3 variations of the loop statement.
It is used to repeat a part of the program multiple times. It is recommended to use this version if the number of iteration is fixed.
for(initialize the counter variable; condition of repeat; manipulate the counter variable)
{
//block of code to be repeated
}
public class MyLoops{
public static void main(String []args){
//Counter is the initial variable
//It is set to value 1
//The condition is checked every time. The loop repeats only till this condition is true
//The counter is incremented here using ++ operator
for(int counter=1;counter<=10;counter++)
{
System.out.print(counter+" ");
}
}
}
0 1 2 3 4 5 6 7 8 9 10
public class MyLoops{
public static void main(String []args){
//Counter is the initial variable
//It is set to value 1
//The condition is checked every time. The loop repeats only till this condition is true
//The counter is incremented here using -- operator
for(int counter=15;counter>0;counter--)
{
System.out.print(counter+" ");
}
}
}
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
There is another version of for loop which is known as for-each loop/enhanced for loop. This variant of for loop was introduced to deal with the collection of objects i.e Arrays, ArrayList, Set, etc.
for(unitDataType var:DataSet)
{
//Set of Instructions
}
public class MyLoops{
public static void main(String []args){
int[] myIntArray = {1,2,3,4,5,6,7,8,9};
System.out.println("\nThe traditional for loop");
for(int counter=0;counter<myIntArray.length;counter++)
{
System.out.print(myIntArray[counter]+" ");
}
System.out.println("\nThe enhanced for loop");
for(int data:myIntArray)
{
System.out.print(data+" ");
}
}
}
The traditional for loop 1 2 3 4 5 6 7 8 9 The enhanced for loop 1 2 3 4 5 6 7 8 9
It is used to repeat a part of the program multiple times. It is recommended to use this version if the number of iteration is not known.
while(condition of repeat)
{
//block of code to be repeated
}
public class MyLoops{
public static void main(String []args){
int myVal=1;
while(myVal<=15)
{
System.out.print(myVal+" ");
myVal++;
}
}
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
It is used to repeat a part of the program multiple times. It is recommended to use this version if the number of iteration is not known and the loop is required to be executed at least once.
do
{
//block of code to be repeated
}
while(condition of repeat );
public class MyLoops{
public static void main(String []args){
int myVal=1;
do
{
System.out.print(myVal+" ");
myVal++;
}
while(myVal<=15);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class MyLoops{
public static void main(String []args){
int myVal=19;
do
{
System.out.print(myVal+" ");
myVal++;
}
while(myVal<=15);
}
}
Output:
19
There are two keywords (break, continue
) which is used to change the flow of execution of a loop. Let us understand the significance and their utility in detail.
This keyword was used in our previous lecture as well(in the switch
statement). Whenever break
is encountered, the block of code that is currently in action gets terminated and the instructions followed by is executed.
public class MyLoops{
public static void main(String []args){
int[] myIntArray = {1,2,3,4,5,6,7,8,9};
for(int data:myIntArray)
{
if(data==5)
break;
System.out.println("I am less than 5, I am "+data);
if(data>6)
System.out.println("I will never be printed. Block terminates at value 5");
}
System.out.println(“I am out of block,so I will be printed!!”);
}
}
I am less than 5, I am 1 I am less than 5, I am 2 I am less than 5, I am 3 I am less than 5, I am 4 I am out of block,so I will be printed!!
Whenever continue
is encountered, the set of instructions followed by continue
inside the block is skipped and it simply goes for the condition check, to decide its re-iteration status.
public class MyLoops{
public static void main(String []args){
int[] myIntArray = {1,2,3,4,5,6,7,8,9};
for(int data:myIntArray)
{
if(data==5)
continue;
System.out.println("I am not 5, I am "+data);
}
}
}
I am not 5, I am 1
I am not 5, I am 2
I am not 5, I am 3
I am not 5, I am 4
I am not 5, I am 6
I am not 5, I am 7
I am not 5, I am 8
I am not 5, I am 9
This was an overview of Java loops. We will be discussing Java Class in our next lecture.