Java

Flow Control Using Loops in Java

Programmingempire

In this article, I will explain Flow Control Using Loops in Java. In general, the statements of a program code execute sequentially. However, there are occasions when this sequential course of action needs to be altered. For instance, suppose we want to repeat certain statements till a particular condition remains satisfied. In that case, we can use loops. Basically, a loop is a construct in programming that allows ceratin statements to be repeatedly executed. Hence, a loop can control the flow of execution of statements in a program. In Java, we have the following types of loops available.

  • for loop
  • while loop
  • do…while loop

Apart from these loops, there is one more loop in Java – the foreach loop that we can use with a collection such as an array.

Using For Loop in Java

The for loop works as follows. At first, it initializes one or more loop control variables. Further, it checks whether the specified condition is true or not. If the condition is true, then the statements within the loop are executed. Finally, the loop control variable(s) are updated and the condition is checked again. Once, the condition becomes falls, the loop exits. Otherwise, the statements within the loop are executed again.

The following code shows an example of for loop. Here, the for loop is used to display integers in the range 1 to n, where n is an integer that the user enters.

package Loops;
import java.util.Scanner;
public class LoopsExamples {
	public static void main(String[] args) {		
                Scanner inp=new Scanner(System.in);
		System.out.println("Enter an integer n");
		int n=inp.nextInt();
		System.out.println("First "+n+"  integers....");
		//For loop to print integers from 1 to n and to find the sum of first n integers
		int sum=0;
		for(int i=1;i<=n;i++)
		{
			System.out.print(i+"  ");
			sum+=i;
		}
		System.out.println("nSum of first "+n+" integers = "+sum);
    }
}

Output

Enter an integer n
12
First 12 integers….
1 2 3 4 5 6 7 8 9 10 11 12
Sum of first 12 integers = 78

Using While Loop in Java

Another, loop in Java is the while loop. In contrast to the for loop, the while loop doesn’t initialize the loop control variables. Therefore, all loop control variables must be initialized before the loop starts. The while loop begins by checking a condition. If the condition is true, then statements within the loop will execute. Once the condition becomes false, the loop will exit. Otherwise, it will repeat the execution of all statements enclosed in the loop. Unlike, for loop, the loop control variables must be updated within the loop. Otherwise, it will become an infinite loop since the condition will always remain satisfied.

The following code shows an example of a while loop. as can be seen, the loop prints all integers from 1 to n, where n is also an integer that the user enters.

package Loops;
import java.util.Scanner;
public class LoopsExamples {
	public static void main(String[] args) {
		Scanner inp=new Scanner(System.in);
		System.out.println("Enter an integer n");
		int n=inp.nextInt();
		System.out.println("First "+n+"  integers....");
		int sum=0;
		int x=1;
		//While loop to print integers from 1 to n and to find the sum of first n integers
		while(x<=n)
		{
			System.out.print(x+"  ");
			sum+=x;
			x++;
		}
		System.out.println("nSum of first "+n+" integers = "+sum);
	}
}

Output

20
First 20 integers….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sum of first 20 integers = 210

Using Do…While Loop in Java

Thirdly, we also have a do…while loop in Java, that works in a similar way as the while loop. However, a do…while loop checks the condition after the statements within the loop are executed. Hence, in the case of a do…while loop, the statements within the loop execute at least once irrespective of whether the condition is true or false. The following code shows an example of the usage of the do…while loop.

package Loops;
import java.util.Scanner;
public class LoopsExamples {
	public static void main(String[] args) {
		Scanner inp=new Scanner(System.in);
		System.out.println("Enter an integer n");
		int n=inp.nextInt();
		System.out.println("First "+n+"  integers....");
		int sum=0;
		int p=1;
		//Do...While loop to print integers from 1 to n and to find the sum of first n integers
		do {
			System.out.print(p+"  ");
			sum+=p;
			p++;
		}while(p<=n);
		System.out.println("nSum of first "+n+" integers = "+sum);
	}
}

Output

Enter an integer n
14
First 14 integers….
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Sum of first 14 integers = 105

More Examples of Flow Control Using Loops in Java

As an illustration of the loops, the following example shows that a condition is always false that all of the three loops evaluate. However, only do…while loop executes the statements and produces an output as shown below.

package BasicExamples;
public class MyClass {

	public static void main(String[] args) {
		int n=10;
		System.out.println("Executing for loop...");
		for(int i=10;i<n;i++)
		{
			System.out.println(i+" ");
		}
		System.out.println("Executing while loop...");
		int i=10;
		while(i<10)
		{
			System.out.println(i+" ");
			i++;
		}
		System.out.println("Executing do...while loop...");
		int j=10;
		do
		{
			System.out.println(j+" ");
		}while(j<10);
	}
}

Output

Example of Flow Control Using Loops in Java
Example of Flow Control Using Loops in Java

The following example shows how the three loops display a series. As can be seen, the loop control variables are updated within the loop in case of a while and do…while loop. Therefore, these two loops produce an additional value as shown in the output.

package BasicExamples1;
public class MyClass {
	public static void main(String[] args) {
		int i, j;
		int n=100;
		System.out.println("Printing Series using For Loop... ");
		for(i=1, j=1;i<100;i=j+i,j+=5)
		{
			System.out.print(i+" ");
		}
		System.out.println("nPrinting Series using While Loop... ");
		i=1; j=1;
		while(i<100)
		{
			i=j+i;
			j+=5;
			System.out.print(i+" ");
		}
		System.out.println("nPrinting Series using Do..While Loop... ");
		i=1; j=1;
		do
		{
			i=j+i;
			j+=5;
			System.out.print(i+" ");
		}while(i<100);
		System.out.println("n");
	}
}

Output

Printing Series using For Loop…
1 2 8 19 35 56 82
Printing Series using While Loop…
2 8 19 35 56 82 113
Printing Series using Do..While Loop…
2 8 19 35 56 82 113

Comparing the Three Loops

While the for loop performs all three operations of initialization, condition checking, and update in the first line of loop, the other two loops use separate statements. Moreover the for loop, and the while loop perform condition checking in the beginning of the loop. In contrast, the do.. while loop performs condition checking in the end. Therefore, the do…while loop executes at least once. Because the condition is checked after the loop statements execute in case of do…while loop. Hence, it executes at least once even if the condition is false.


programmingempire

You may also like...