JAVA Iteration


ITERATION
STATEMENTS
The iteration statements allow a set of instructions to execute repeatedly
until a termination condition is reached.  
A Loop consists of the following parts:-
  1. INITIALIZATION EXPRESSION
Here the initialization of the loop control variable takes place. The first
value is assigned to the variable in this expression.
  1. TEST EXPRESSION
The truth value of the test expression decides whether the loop body will
be further executed or not.
  1. UPDATE EXPRESSION 
This expression alters the value of the loop control variable as mentioned
by the user. This statement is executed is executed at the end of the loop body.
  1. BODY OF THE LOOP
The statements that are repeatedly executed as long as the test expression
is true.


ENTRY CONTROLLED LOOPS
They consist of two types:-
  1. for loop
  2. while loop




for -LOOP
SYNTAX:
for(initialization expression(s);test expression; update expression(s) )
{
Body of the loop;


EXAMPLE: PRINT NUMBERS FROM 1 TO 10
class Number
{
public void Nos()
{
int i=0;
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}
}


WHILE LOOP
SYNTAX:


initialization expression;
while(condition)
{
Body of the loop;
update expression;
}



EXAMPLE: PRINT NUMBERS FROM 1 TO 10


class Number
{
public void Nos()
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}


EXIT CONTROLLED LOOP
DO WHILE LOOP
SYNTAX:


initialization expression;
do
{
Body of the loop;
Update expression;
}while(test expression);


EXAMPLE: PRINT NUMBERS FROM 1 TO 10
class Number
{
public void Nos()
{
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}

No comments:

Post a Comment