Loops
The
repeated execution of block of statements in a program is known as looping. The
block of statements can be executed any number of times from 0 to infinite,
depending upon the requirement of the program. The block of statements in a
loop in a program will be executed until some termination condition is
satisfied.
While
Loop
While
is entry controlled loop, test condition is checked in the beginning of the
loop and if condition is true then body of the loop is executed.
Syntax
Initialization
of variables;
while (check condition)
{ //
Body of the loop
Statement
a;
Statement b;
}
After executing all the
statements in the body of the loop condition is again checked, if condition is
true the loop is again executed and so on, until the condition becomes false.
Once condition is false execution comes out of the loop.
Example
public class WhileLoop {
public static void main(String[] args)
{
int sum=0, counter=1;
while(counter <= 10)
{
sum = sum + 1;
counter = counter + 1;
}
System.out.println(" SUM = " + sum );
}
}
The
body or the statements in the block of loop will be executed 10 times for counter = 1, 2, 3…..10. Initially value
of counter is 1 and value of sum is 0. When execution of line while(counter <= 10) starts the
execution will check the value of counter which is less than 10 so loop will be executed, in the body
of loop both sum and counter is incremented by 1. Control
again will go and check the condition, now value of counter is 2 which is less
than 10 so body of the loop will be executed. Similarly loop will continue
executing until value of counter becomes 11.
When value of counter becomes 11 control will check the condition which
is false because the value of counter is grater than 10, and execution will
jump out of the loop to the statement System.out.println(" SUM = "
+ sum ); and display the result. The result is
10.
Do
While Loop
In while loop we have
seen that condition is checked first and if the condition is true than only the
body of loop is executed. If condition is false in first attempt then loop will
never be executed. But some time it becomes necessary to execute the body of
loop at least once before checking the condition, for such conditions do loop
is used.
Syntax
Initialization of variables;
do
{ //
Body of the loop
Statement a;
Statement b;
}
while (check condition)
In do while loop once
body of loop will be executed without checking condition. Means condition will
be checked after execution of the loop. If condition is true loop will be
executed again and then condition will be checked and so on. Once condition
will become false execution of loop will be stopped and control will jump out
of the loop.
Example
public class DoWhileLoop {
public static void main(String[] args)
{
int sum=0, counter=2;
do
{
sum = sum + 1;
counter = counter + 1;
}
while(counter <= 1);
System.out.println(" SUM = " + sum );
}
}
Initially value of sum
= 0 and value of counter = 2. As control will go into do loop value of sum will
become 1 and value of counter becomes 3. The condition in while statement is while(counter <= 1); means loop
will be executed if value of counter is less than 1 or equal to 1. Condition
was not true even in the beginning of the loop even than it was executed onece.
The result
is: SUM = 1
For Loop
For loop is also entry controlled loop, it gives
more concise loop control structure.
Syntax
for (initialization of variable; condition;
increment)
{
Statement a;
Statement b;
}
There are three parts
of the for loop: initialization,
test condition and increment/decrement. The execution of for loop is as follows:
Initialization: first
of all initialization of control variable is done just like we declare and
initialize variables in a simple program, like: int i = 0. The variable i is called control variable.
Condition: The value of
the control variable (i) is checked
using the relational operators, such as:
i <= 10. If the value of condition is true then loop will be executed
otherwise loop will be terminated and execution of the statement just next to
loop will start.
Increment: When body of
the loop is executed control is shifted to for
statement and control variable is incremented by a assignment statement, like: i++ or i = i +1. Now new value of the variable is again checked by the
condition part of the for statement. If value is true, body of loop is again
executed and so on. This process continues until the value of variable becomes
false.
Example:
public class ForLoop {
public static void main(String[] args)
{
for(int i =0; i <= 10; i++)
{
System.out.println(i);
}
}
}
The output
of program displays numbers: 0...10.
We can do
initialization of the control variable out of for statement also:
int i;
for(i =0; i <= 10; i++)
{
System.out.println(i);
}
Initialization and
increment of more than one variable can be done in for statement:
int i,j;
for(i =0, j=2; i <= 10; i++, j+4)
{
System.out.println(i);
}
To check condition of more
than one relation is also possible using logical operators. To check condition
other then control variables can also be used.
int i, total = 0;
for(i =0; i <= 10 && total < 100 ; i++, j+4)
{
System.out.println(i);
}
Just like
increment of the control variable decrement is also possible:
int i;
for(i =10; i >= 0; i = i -1)
{
System.out.println(i);
}
Any same loop or
different loop can be used within the loop also:
int i,j;
for(i =0; i <= 3; i++)
{
for(j=0; j<=10; j++)
{
System.out.println("I = " +i);
System.out.println("J = " +j);
}
}
Jumps in Loops
A
loop executes set of operations repeatedly until the test condition becomes
false. But sometimes we need to break the loop in between or we want to skip
some steps in the loop.
Break Statement
Break
statement is used to exit from the loop before complete execution of the loop. Break
statement can be used within while, do while, for loops and switch statement. When
break statement is executed in the loop the loop is immediately exited and
control jumps to the statement next to the loop.
Example
public class LoopBreak {
public static void main(String[] args)
{
for(int i =0; i <= 10; i++)
{
System.out.println(i);
if(i==2)
{
break;
}
}
System.out.println("Break of loop !!!!!");
}
}
The output of the
program is:
0
1
2
Break of loop!!!!!
Loop executed three
times and then condition if(i==2) got satisfied and loop exited. Execution control jumped to statement
System.out.println("Break
of loop !!!!!");
Continue Statement
Continue statement is used to skip some part of the loop under
certain condition. Break statement terminates the loop but continue statement
jumps to the next iteration. Means when continue statement is executed the
execution control skips rest of the statements and jumps to the beginning of
the loop and checks test condition and so on.
Example
public class FirstJavaProgram {
public static void main(String[] args)
{
for(int i =0; i <= 3; i++)
{
if(i==2)
{
continue;
}
System.out.println(i);
}
}
}
The result of the program
is
0
1
3
After loop executing
2 times if(i==2) condition is satisfied and continue; is executed. Control jumped to beginning of the loop
without executing statement System.out.println(i); so 2 is not displayed in the output.
No comments:
Post a Comment