Looping Statement in Java
Looping statements allow the programmer to execute some group of statement repetitively multiple
times.
Control
goes inside the body of the loop if the condition is true otherwise goes
outside of looping block. The main difference between decision making
statements and looping statement is decision making statement execute once
alike looping statements executes several times.
There
are two types of loop
1. Entry control loop (while
loop and for loop)
2. Exit control loop (do …
while loop)
Entry
control loops
are the loop in which condition is tasted at the beginning of
the loop.
Exit
control loops
are the loop which condition is tasted at the end of the loop.
Do
… while is an exit control loop. For and while are entry control loop.
Note
: Exit control loops are executed at least once in their lifetime.
Advantages
of looping :
- Reduce the memory consumption.
- Reduce the length of the code.
- No need to write same code again to execute several
times
Following
are the types of looping statements
- while loop
- do … while loop
- for loop
- nested loops
1. While loop
While
loop is an entry control looping statement that allows code to be executed
until the boolean condition is true.
You
can also call while loop as a repeating if statement.
Syntax
:
...
//Assignment of counter variable
while(condition)
{
//body of loop
//increment or decrement of variable
}
...
Flowchart
:
...
//Assignment of counter variable
while(condition)
{
//body of loop
//increment or decrement of variable
}
...
while loop |
while loop
Example
:
class WhileLoop{
public
static void main(String args[]){
int
i=0; // Assignment of counter variable
while(i<10){
//This
loop will be executed 10 times.
System.out.println(i);
i++;
//increment of counter variable
}
}
}
class WhileLoop{
public
static void main(String args[]){
int
i=0; // Assignment of counter variable
while(i<10){
//This
loop will be executed 10 times.
System.out.println(i);
i++;
//increment of counter variable
}
}
}
Output
:
C:\workspace>notepad WhileLoop.java
C:\workspace>javac WhileLoop.java
C:\workspace>java WhileLoop
0
1
2
3
4
5
6
7
8
9
C:\workspace>
C:\workspace>notepad WhileLoop.java
C:\workspace>javac WhileLoop.java
C:\workspace>java WhileLoop
0
1
2
3
4
5
6
7
8
9
C:\workspace>
1. Do … while loop
Do
… while is an exit control looping statement which is also used to execute a
block of instructions several times. This loop is executed at least once in its
lifetime whether the condition is true or false.
Syntax
:
//Assignment of counter variable
do{
//body of statement
//increment or decrement of counter variable
}
while(condition); //You must put semicolon
here
//Assignment of counter variable
do{
//body of statement
//increment or decrement of counter variable
}
while(condition); //You must put semicolon
here
Flowchart
:
do while
loop
Example
:
class DoWhileLoop{
public
static void main(String args[]){
int
i=0; // Assignment of counter variable
do{
//this
body will be executed at least once
System.out.println(i);
i++;
//increment of counter variable
}
while(i<10);
}
}
Output
:
C:\workspace>javac DoWhileLoop.java
C:\workspace>java DoWhileLoop
0
1
2
3
4
5
6
7
8
9
C:\workspace>
Note
: You need to put a semicolon at the end of the do … while statement.
1. For loop
For
loop is an entry control loop which works same as the other two loop but its
syntax is different. In other two loop counter variable is declared outside the
body of the loop.
Here,
the counter variable is declared within for loop itself but the counter
variable is declared and assigned by initial value only once.
Increment
and condition checks will be done multiple times.
Syntax
:
for(init; condition; increment or decrement){
//body of for loop
}
Flowchart
:
java lopping statement |
Example
:
class ForLoop{
public
static void main(String args[]){
for(int
i=0;i<10;i++)
{
System.out.println(i);
}
}
}
class ForLoop{
public
static void main(String args[]){
for(int
i=0;i<10;i++)
{
System.out.println(i);
}
}
}
Output
:
C:\workspace>javac ForLoop.java
C:\workspace>java ForLoop
0
1
2
3
4
5
6
7
8
9
C:\workspace>
C:\workspace>javac ForLoop.java
C:\workspace>java ForLoop
0
1
2
3
4
5
6
7
8
9
C:\workspace>
1. Nested loops
The
concept of a nested loop is as same as nested decision-making statement. One
loop can contain another looping statement.
Example
:
class NestedLoop{
public
static void main(String args[]){
int
j;
for(int
i=0;i<5;i++)
{
j=1;
while(j<=i)
{
System.out.print("*");
j++;
}
System.out.println();
}
}
}
class NestedLoop{
public
static void main(String args[]){
int
j;
for(int
i=0;i<5;i++)
{
j=1;
while(j<=i)
{
System.out.print("*");
j++;
}
System.out.println();
}
}
}
Output
:
C:\workspace>javac NestedLoop.java
C:\workspace>java NestedLoop
*
**
***
****
C:\workspace>
C:\workspace>javac NestedLoop.java
C:\workspace>java NestedLoop
*
**
***
****
C:\workspace>
No comments:
Post a Comment