Online-Academy

Look, Read, Understand, Apply

Menu

Loops

Loops

Loops are programming constructs to perform tasks which needs to be done repeatedly. Generally, found loops in most of the programming languages are: for, do..while, and while; but in addition to these three Java has foreach,.. loops also.

Syntax of loops

  • for loop: for loop has three parts: initialization of variable, condition checking and iterating and body of the loop. Statement/s to execute repeatedly are written within the body of the loop; those statements will be executed till the condition is true.
    for(initialization; condition; iteration){
    	//body of for loop
    }
    
  • while loop: In the while loop, condition is evaluated first, if the condition is true then the statements within the body of while loop is executed; the statements are executed until the condition of the loop is true.
    while(condition){
    	//body of while loop
    }
    
  • do..while: In the do..while loop, statement within the body of do..while loop is executed first then only condition is evaluated; if the condition is true statements in the loop body are executed again and again until the condition is true.
    do{
    	//body of do while loop
    }while(condition);
    

Examples

Given program uses for loop to display numbers 1 to 10; The statement to display number is repeated for 10 times. In the for-loop variable i is initialized to 1; then the value of i is compared with 10 to see if i is less than or equal to 10 or not; if this condition is true, statement within the for loop is executed, then value of i is incremented by 1. The statement , printing value of i, is executed until the value of i is less than or equal to 10.

class loops_I{
	public static void main(String[] opp){
		int i;
		System.out.println("Displaying numbers between 1 and 10 inclusive.");
		for(i=1;i<=10;i++){
			System.out.print(i+ ", ");
		}
	}
}

Output:

Displaying numbers between 1 and 10 inclusive. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

Following program display product table of nine.

class loops{
	public static void main(String[] opp){
		int i;
		System.out.println("Displaying product of 9.");
		for(i=1;i<=10;i++){
			System.out.println(i+ " * 9 = "+ (i*9));
		}
	}
}

Output:

Displaying product of 9. 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 10 * 9 = 90

Following program displays all even numbers between 99 and 199.

class loops_II{
	public static void main(String[] opp){
		int i;
		System.out.println("Displaying all even numbers  between 99 and 199.");
		for(i=99;i<=199;i++){
			if(i%2 == 0)
			System.out.print(i+", ");
		}
	}
}

Output:

Displaying all even numbers between 99 and 199. 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198,

Given program displays sum of numbers entered by the user. User enters number till s/he likes; while loop is used to take integers from the user; this loop will run until the users enters 0. Each entered value is added to variable sum, sum is initially initialized with 0. As user enters 0, loop will terminate and sum of the entered values is displayed.

import java.util.*;
class loops_III{
	public static void main(String[] opp){
		System.out.println("Enter numbers and get sum of those numbers until users enters 0");
		int sum = 0;
		int num=99;//initializing num is dummy value 
		Scanner sc = new Scanner(System.in);
		while(num != 0){
			System.out.print("Enter a number: ");
			num= sc.nextInt();	
			sum += num;
		}
		System.out.print("Sum of entered numbers is :"+sum);
	}
}

Output:

First Run: Enter numbers and get sum of those numbers until users enters 0 Enter a number: 22 Enter a number: 33 Enter a number: 44 Enter a number: 55 Enter a number: 0 Sum of entered numbers is :154 Second Run: Enter numbers and get sum of those numbers until users enters 0 Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: 5 Enter a number: 0 Sum of entered numbers is :15

The following program shows use of do..while loop. This program displays all the numbers between two numbers provided as input by the user and are divisible by 17. The first value entered is assigned to i. Value of i is increased by 1 in the loop. This loop executes until the value of i is less than equal to second entered value which is assigned to another integer b.

class loops_IV{
	public static void main(String[] opp){
		int a, b,i;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number: ");
		a = sc.nextInt();
		System.out.print("Enter a number: ");
		b = sc.nextInt();
		System.out.println("Displaying all numbers divisible by 17 between "+a+" and "+b+" inclusive.");
		i = a;
		do{
			if(i%17 == 0)
			System.out.print(i+", ");
			i++;
		}while(i<=b);
	}
}

Output:

Enter a number: 101 Enter a number: 201 Displaying all numbers divisible by 17 between 101 and 201 inclusive. 102, 119, 136, 153, 170, 187,

Program below is similar to the previous one, but difference is first used do..while loop and second has while loop.

class loops_V{
	public static void main(String[] opp){
		int a, b,i;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number: ");
		a = sc.nextInt();
		System.out.print("Enter a number: ");
		b = sc.nextInt();
		System.out.println("Displaying all numbers divisible by 9 between "+a+" and "+b+" inclusive.");
		i = a;
		while(i<=b){
			if(i%9 == 0)
			System.out.print(i+", ");
			i++;
		}
	}
}

Output:

Enter a number: 91 Enter a number: 200 Displaying all numbers divisible by 9 between 91 and 200 inclusive. 99, 108, 117, 126, 135, 144, 153, 162, 171, 180, 189, 198,

Program given below uses for loop to displays all the numbers that are divisible by 9 and between the two numbers entered by the user.

class loops_VI{
	public static void main(String[] opp){
		int a, b,i;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number: ");
		a = sc.nextInt();
		System.out.print("Enter a number: ");
		b = sc.nextInt();
		System.out.println("Displaying all numbers divisible by 9 between "+a+" and "+b+" inclusive.");
		for(i=a;i<=b;i++){
			if(i%9 == 0)
			System.out.print(i+", ");
		}
	}
}

Output:

Displaying all numbers divisible by 9 between 9 and 81 inclusive. 9, 18, 27, 36, 45, 54, 63, 72, 81,