Q.Why use loops in java?
Advantage of loops in java
- Reduce the length of the code
- Saving time
- Take less memory space
There are some loops in java
- for loop
- while loop
- do while loop
- for each loop
for loop in java
Syntax:
for(Initialization; condition; increment/decrements)
{
//Statements ;
}
Initialization: initialization step execute first and this step is execute on only once when we are entering into the loop first time. Initialization step allows to declare and initialize any loop control variables.
Condition: condition is the second step after initialization step. If condition of second steps is true, the body of the loop is executed again and again, if it is false then the body of the loop does not execute and flows of loop control goes outside of the for loop.
Increment or Decrements: when the initialization and condition steps is execute then increment or decrements step is execute. This statement allows to update any loop control variables.
Initialization: initialization step execute first and this step is execute on only once when we are entering into the loop first time. Initialization step allows to declare and initialize any loop control variables.
Condition: condition is the second step after initialization step. If condition of second steps is true, the body of the loop is executed again and again, if it is false then the body of the loop does not execute and flows of loop control goes outside of the for loop.
Increment or Decrements: when the initialization and condition steps is execute then increment or decrements step is execute. This statement allows to update any loop control variables.
For example:
class ForLoop
{
public static void main(String...s)
{
for(int i = 1; i<=10; i++)
{
System.out.println(i);
}
}
}
output:1
2
3
4
5
6
7
8
9
10
output:1
2
3
4
5
6
7
8
9
10
while loop in java
It is also used to repeat a task number of time but where the number of repeatitions are not fixed. The Statements inside the loop are executed while certain condition is true.
Syntax:
while(condition)
{
//Statements ;
}
For example:
class WhileLoop
{
public static void main(String...s)
{
int a =1;
while(a<=5)
{
System.out.println(i);
i++;
}
}
}
output: 1
2
3
4
5
do while loop in java
Similar to the while loop except that the statements are executed at least once because condition is checked after loop body.
Syntax:
do
{
//Statements;
}
while(condition);
For example:
class DowhileLoop
{
public static void main(String...s)
{
int a = 10;
do
{
System.out.println(a);
a++;
}
while(a < =20);
}
}
output: 10
11
12
13
14
15
16
17
18
19
20
for-each or enhanced for loop in java
Syntax:
for(type variable : array)
{
//Statements;
}
For example:
class ForEachLoop
{
public static void main(String...s)
{
int i[]={1,2,3,4,5};
for(int a : i)
{
System.out.println(a);
}
}
}
infinite for loop in java
Syntax:
for(;;)
{
//Statements;
}
For example:
class InfiniteForLoop
{
public static void main(String []args)
{
for(;;)
{
System.out.println("This is infinite for loop");
}
}
}
output: This is infinite for loop
This is infinite for loop
This is infinite for loop
Note: for Stop this loop press Ctrl + c on your command prompt.
0 comments:
Post a Comment