What is looping?
Looping means to do a job in repetitive manner. If some statements are to be executed number of time then we can go for looping. In java programming language generally there are three types of loops are allowed
- While
- Do while
- For
For Each (It is mainly used to with array or collection type)
Benefits of Looping
Different benefits are available if you go for looping. The size of the program is reduced. That is why it takes less memory space. Because of this the burden on the developer is reduced. Also it does not take much time to execute a program
For a general loop following things are needed
1. Initialization – to give the initial value for the variable
2. Condition – to test the condition. The loop will be executed till the condition is true. If the condition is false it will not continue further.
3. Increment or Decrements – loop will continue till the condition is true. It should be false whenever required. That is why it should be incremented or decremented to be false, otherwise it will continue and continue. This will be called as indefinite looping.
Syntax of While
While (condition)
{
Statements;
increment or decrement;
}
Syntax of do while
Do
{
Statements;
increment or decrement;
}
While (condition);
Syntax of for
for (initialization; condition; increment or decrement)
{
Statements;
}
Syntax of For each
for (initialization: collection)
{
Statements;
}
Programming examples using while, do while and for
Print Your Name for 10 times using while loop
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
while(a<=10)
{
System.out.println(“Hi Pravuram”);
a+=1;
}
}
}
Output
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Print Your Name for 10 times using do while loop
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
do
{
System.out.println(“Hi Pravuram”);
a+=1;
}
while(a<=10);
}
}
Output
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Print Your Name for 10 times using for loop
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a;
Scanner sc=new Scanner (System.in);
for(a=0;a<=10;a++)
{
System.out.println(“Hi Pravuram”);
}
}
}
Output
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
Hi Pravuram
For loop follows the following steps
1. First initialize the variable
2. In second step check condition
3. In third step control goes inside loop body and execute.
4. At last increase the value of variable
5. Same process is repeated till condition is true.
For each loop example (will be discussed in arrays)
Programming Examples
Print the numbers from 1 to 10 (While Loop)
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
while(a<=10)
{
System.out.println(a);
a+=1;
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
Print the numbers from 1 to 10 (Do While Loop)
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
do
{
System.out.println(a);
a+=1;
}
while(a<=10);
}
}
Output
1
2
3
4
5
6
7
8
9
10
Print the numbers from 1 to 10 (For Loop)
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
for(a=1;a<=10;a++)
{
System.out.println(a);
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
Print the numbers from 10 to 1
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=10;
Scanner sc=new Scanner (System.in);
for(a=10;a>=1;a–)
{
System.out.println(a);
}
}
}
Output
10
9
8
7
6
5
4
3
2
1
Print the squares of numbers from 1 to 10
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a=1;
Scanner sc=new Scanner (System.in);
for(a=1;a<=10;a++)
{
System.out.println(a*a);
}
}
}
Output
1
4
9
16
25
36
49
64
81
100
Print the numbers from a to b
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner (System.in);
System.out.println(“Enter a number”);
a=sc.nextInt();
System.out.println(“Enter another number”);
b=sc.nextInt();
if(a<b)
{
for(c=a;c<=b;c++)
{
System.out.println(c);
}
}
else
System.out.println(“Wrong Input”);
}
}
Output
Enter a number
60
Enter another number
40
Wrong Input
Output
Enter a number
5
Enter another number
9
5
6
7
8
9
Print the numbers from a to b in reverse manner
import java.util.Scanner;
class student
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner (System.in);
System.out.println(“Enter a number”);
a=sc.nextInt();
System.out.println(“Enter another number”);
b=sc.nextInt();
if(a<b)
{
for(c=b;c>=a;c–)
{
System.out.println(c);
}
}
else
System.out.println(“Wrong Input”);
}
}
Output
Enter a number
60
Enter another number
40
Wrong Input
Output
Enter a number
5
Enter another number
9
9
8
7
6
5