Thursday, April 2, 2009

java-for-foreach-while-dowhile

Iteration statement :
while:-if we don’t know the number of iteration in advance then we should go for while loop. The valid argument for the while loop is Boolean
{ } are optional , without { } only one statement is allow, that statement never be declarative statement .
Syntax:- while(true) {
System.out.println (“hello”);
}
System.out.println (“hi”); //Compile time error unreachable statement ex:-while(false)
{
System.out.println (“hello”) ;//CE Unreachable statement
}
System.out.println(“hi”)
Ex:- int a=10;
int b=20;
while (b>a)
{
System.out.println(“hello”); hello hello hello …........
}
System.out.println (“hi”);
ex:- final int a=10;
final int b =20;
while (b>a)
{
System.out.println (“hi”) ;
}
System.out.println (“hello”);//CE saying unreachable statement

do..while statement:-
If the loop body as to execute at least once ,then we should go for while loop
syntax:-
do{
………………..
}
while (b);// mandatory .
without { } we should give exactly one statement between do and while . that may be empty statement .
ex:- do
;
while (b); // valid
without { } only one statement is allowed but it should not be declarative statement
ex:- do
;
while (b); // valid
ex:-
do
{
System.out.println(“hi”);
} while (true);
System.out.println(“hello”); // compile time error unreachable statement
Ex: int x = 10;
do
while(x >0);
System.out.println("hai"); // compile time error saying since after do there is no loop In the while continue statement will shift the transfer conditional checking.
Eg:- int a=10;
int b=20;
do{
System.out.println(“hi”);
}
while(a<b)
{
System.out.println(”hello”);
}
Eg:-final int a=10;
final int b=20;
do {
System.out.println(“hi”);
}
while (a<b)
{
System.out.println(”hello”);
}//CE saying unreachable statement because here a and b are constants

for loop:The most commonly used loop, if we know the iteration in advance, then we should go for for loop
syntax:-
for(initialization part; conditional check; inc/ dec)
{
//loop body
}
initialization part:- Here more than one variable of different data type are not allowed to declare. i.e we have to declare all the variable of the same type and the data type must be mention only one time.
i.e int i=0,j=0; right
int i=0,int j=0; wrong
int i=0,float f=10.0; wrong
More than one data type declaration is not allowed.
As the initialization part we are allowed to take any valid java statement including sop also. initialization part is optional
conditional expression:- As the conditional expression we can take any expression ,but it should boolean value.
The default value for condition expression is true if we are not providing. But default values are not applicable for while and do. .while loops.
Inc/ dec part:- Here we are allowed to any valid java statement including sop also .
All the 3 parts of for loop are optional i.e for(;;); is valid
Ex:-for (int i=0;true; i++){
System.out.println(i);
}
System.out.println(“hello”);//CE unreachable statement
Ex:- :-for (int i=0;false; i++)
{
System.out.println(i); //CE unreachable statement
}
System.out.println(“hello”);
Ex:-
for(int i=0;a<b; i++){
System.out.println(i);
}
System.out.println(“hello”);
Output:-1 2 3 4 5 6 7 8…….
Eg:-
final int a=10;
final int b=20;
for(int i=0;a<b;i++)
{
System.out.println(i);
}System.out.println(“hello”);// invalid

For-each loop:- introduce 1.5 version. More convenient loop for iterating the elements of arrays and collections.
for Example:-int [] a={10,20,30,40};
for(i=0;i<=a.length;i++)
{
System.out.println(a[i]); // 10,20,30,40
}
foreach example:-int [] a={10,20,30,40};
for(int x: a)
{
System.out.println(x); // 10, 20,30,40
}
The main limitation of for-each loop is we can’t use in general and it is applicable only for arrays and collections.
Write the code to display the two dimensional array use for-each loop.
int[] [] a={{10,2030,40},{50,60}}
for(int x : a)
{
for(int y: a){
for(int y : x){
System.out.println(y);{
}

Transfer statement:
Break statement:- we can use the break statement in the following cases .
(i) with in the ioops
(ii) in the switch statement
(iii) in the lable blocks
if we can use any where else we will get a compile time error saying break out side switch (or) loop.
continue statement:-
We can use continue statement only in loops to skip the current iteration and continue for the next iteration.
int i=10;
do{i++;
System.out.println(i);
if(i<=16)
continue;
System.out.println(i);
}
while(++i<20);
In the while continue statement will shift the transfer conditional checking.
Ex:-class sample {
Public static void main(string args[])
{
int x=10;
if(x>10)
continue; //CE saying continue out side of loop
Labeled break and labeled continue:-In the nested loop, if you want to break or continue a particular loop then we should go for labeled break and continue statements.
Ex:-for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(i==j)
break;
System.out.println(i+”……”+j);
}
}

0 comments:

Post a Comment