Thursday, January 19, 2017

Iterative statements in java..(for,while, do while loop)

3. Iterative Statements:

---------------------------------------------------------------------------------------------------

These statements are able to allow to execute a block of instructions repeatedly on the basis of a particular condition.

There are 3 type of iterative statement in java.


1. for 2. while          3. do-while


1. for:
----------------
Syntax:

for(Expr 1: Expr 2: Expr 3)
{
---Instructions---
}

EX 1:
for (int i = 0; i < 10 ; i++ )
{
System.out.println(i);
}

Expr 1: 1 time execution
Expr 2: 11 times execution
Expr 3: 10 times execution
Body : 10 times execution

EX 2:
int i = 0;
for ( ; i < 10 ; i++ )
{
System.out.println(i);
}

Status: No compilation error.

EX 3:
int i = 0;
for (System.out.println("Hello") ; i < 10 ; i++ )
{
System.out.print(i + " ");
}

Status: No compilation Error
OP:
Hello
0 1 2 3 4 5 6 7 8 9

Note: 
In for Loop, Expr1 is optional, we can write for loop without Expr1, we can provide any statement as Expr1 in for loop, but always it is suggestible to provided loop variable declaration and their initialization type of statements as Expr1.


EX 4:
for (int i = 0, float f = o.of; i<10 & f <10.0; i++ ,f++)
{
System.out.print(i + " " + f);
}

Status: Compilation Error

EX 5:
for (int i = 0, int j = o; i<10 & j <10; i++ ,j++)
{
System.out.print(i + " " + j);
}

Status: Compilation Error

EX 6:
for (int i = 0, j = 0; i<10 & j <10; i++ ,j++)
{
System.out.print(" " + i + " " + j);
}

Status: No Compilation Error
OP:  0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

Reason:
In for loops, Expr1 is able to allow utmost one declarative statement , if we provide more than one declarative statement then compiler will rise an error. In single declarative statement we can declare more than one variable.

(one keyword is one statement so if we use int&float or int&int then these are two keyword so it become two declarative statement, in EX 6, we use  only one keyword so it does not show any error)

EX 7:
for (int i = 0; ; i++ )
{
System.out.println(i);
}

Status: No Compilation Error

EX 8:

for (int i = 0; System.out.println("Hello"); i++ )
{
System.out.println(i);
}

Status: Compilation Error

Reason:
In for loop, Expr2 is optional, we can write for loop without Expr2, if we write for loop without Expr2 then for loop will take "true" value in place of Expr2 and it will make for loop as infinity loop. if we want to provide any statement as Expr2 then that statement must be Boolean statement, it must return either true value or false value.


EX 9:
System.out.println("Befor Loop");
for (int i = 0; i <=0 || i >= 0 ; i++ )
{
System.out.println("Inside loop");
}
System.out.println("After Loop");

Status: No compilation Error

EX 10: 
System.out.println("Befor Loop");
for (int i = 0; true ; i++ )
{
System.out.println("Inside loop");
}
System.out.println("After Loop");

Status: Compilation Error

EX 11: 
System.out.println("Befor Loop");
for (int i = 0;  ; i++ )
{
System.out.println("Inside loop");
}
System.out.println("After Loop");

Status: Compilation Error: Unreachable Statement

Note:
If we provide any statement immediately after infinity loop then that statement is called as "Unreachable Statement".

In java applications, when compiler identifiers the provided loop as an infinity loop and it is identified any following statement that infinity loop then compiler will provide an error like " Unreachable Statement". In for loop if we provide constant expression as conditional Expr & it
return "true" value then compiler will recognize that provided loop as infinite loop. if we provide variable expression as conditional Expr in  for loop as an infinite loop even it is really an infinite loop & compiler will not rise any "Unreachable Statement" like error even it  identify any statement immediately after the provided loop.

EX 12:
for (int i = 0; i < 10 ; )
{
System.out.println(i);
i = i + 1;
}
Status: No compilation Error

EX 13:
for (int i = 0; i < 10 ; System.out.println("Hello"))
{
System.out.println(i);
i = i + 1;
}
Status: No compilation Error

Reason:
In for loop, Expr3 is optional, we can write for loop without Expr3. we can write any statement as Expr3, but , it is suggestible to provide loop variable increment & decrements kind of statements as Expr3.

EX 14:
class  Test
{
public static void main(String[] args)
{
for ( ;  ; )
}
}
Status: Compilation Error

EX 15:
class  Test
{
public static void main(String[] args)
{
for ( ;  ; )
{
}
}
}

or

class  Test
{
public static void main(String[] args)
{
for ( ;  ; ) ;
}
}
Status: No Compilation Error
Output: No output but JVM is in infinite loop.

EX 16:
class  Test
{
public static void main(String[] args)
{
for ( ;  ; )
;
}
}
Status: No Compilation Error

Reason:
In for loop, if we want to provide single statement in body then curly braces {} are optional, if we don't want to provide any statement in for loop then we must provide either curly braces or  ' ; ' .

In Java Application, we are able to use "for" loop when we aware the number of iteration in advance before writing the loop.

To retrieve elements from array, we are going to use for loop only, because, we can identify number of iterations before the loop, that is, size of array represented in form of "length " variable.

EX 17:
class  Test
{
public static void main(String[] args)
{
int[] a = {1,2,3,4,5};
for ( int i = 0; i < a.length ; i++ )
{
System.out.println(a[i]);
}
}
}

To retrive elements from arrays if we use the above "for" loop approach then we are able to get following drawbacks:

1. We have to manage a separate loop variable.
2. At each and every iteration, we have to execute conditional expression.
3. At each & every iteration, we have to perform either increment or decrement operation over the loop variable.
4. We have to retrive elements from array on the basis of index values, if the provided index value is not proper then JVM will rise an Exception
like java.lang.ArrayIndexOutOfBoundsException

To overcome the above drawbacks, we have to use "for-Each" loop provided by JDK5.0 version.

Syntax:
for(Array_Data_Type var: Array_Ref_Var)
{
  ---Instructions--
}

when JVM Encounters the above for-each loop, JVM will perform iteration over the provided array right from first element to last element, At each & every
iteration, JVM will take element from array & JVM will assign that elements to the specified variables in for-each loop.

EX 18:
 int[] a = {1,2,3,4,5};
for ( int i : a )
{
System.out.println(i);
}


EX 19:
class  Test
{
public static void main(String[] args)
{
String[] str = {"AAA","BBB","CCC","DDD"};
for ( int i = 0; i < str.length ; i++ )
{
System.out.println(str[i]);
}

System.out.println();

for (String i : str)
{
System.out.println(i);
}
}
}
OP:
AAA
BBB
CCC
DDD

AAA
BBB
CCC
DDD

-------------------------------------------------------------------------------------------------------------------------

2. While Loop:
------------------------------
In java application, we will use "while" loop when we are not aware about number of loop iteration in advance before writing loop.

Syntax:
while(Condition)
{
 --- instrucion---
}

EX 1:
int i = 0;
while(i < 10)
{
System.out.print(i + "  ");
i = i + 1;
}
OP:
0 1 2 3 4 5 6 7 8 9

EX 2:
int i = 0;
while( )
{
System.out.print(i + "  ");
i = i + 1;
}
Status: Compilation Error
Reason: Conditional Expression is mandatory in while loop.

EX 3:
System.out.println("Befor Loop");
int i = 0;
while( i <=0 || i >= 0 )
{
System.out.println("Inside loop");
i = i + 1;
}
System.out.println("After Loop");

Status: No compilation Error

EX 4:
System.out.println("Befor Loop");
int i = 0;
while( true )
{
System.out.println("Inside loop");
i = i + 1;
}
System.out.println("After Loop");

Status: compilation error: Unreachable statement

-------------------------------------------------------------------------------------------------------------

3. Do-while loop:
------------------------------------

Q.) what are the difference between while loop & do-while loop?
Ans:
1. while loop will not give guarntee to execute loop body minimum one time.
    do-while loop will give guarantee to execute loop body minimum one time.

2. In case of while loop, first conditional expression will be executed then loop body will be                   executed.
   In case of do-while loop, first loop body will be executed then conditional expression will be               executed.

3. In case of while loop, conditional expression will be executed to perform current iteration.
   In case of do-while loop, conditional expression will be executed to perform next iteration.

Syntax:
while(condition)
{
 --- instruction --
}


do
{
--- instruction ---
} while(condition);


EX 1: 
int i = 0;
do
{
System.out.print(i + "  ");
i = i + 1;
}while(i < 10);
OP:
0 1 2 3 4 5 6 7 8 9

EX 2:
int i = 0;
do
{
System.out.print(i + "  ");
i = i + 1;
}while( );
Status: Compilation Error


EX 3:
System.out.println("Befor Loop");
int i = 0;
do
{
System.out.println("Inside loop");
i = i + 1;
}while( true );
System.out.println("After Loop");

Status: compilation error: Unreachable statement

You may also like: --> if-else condition in java

No comments:

Post a Comment