Any looping statement is comprises of there stages i.e
1> Initialization 2> Conditional 3> Updation
Initialization : It is the first step while executing any loop and it will be executed once while running the loop.if we declare the variable in the initialization part then the memory will be allocated once and reused many time while looping.
Conditional : It is the 2nd step in looping statement and it will be executed multiple time till the condition false.
Updation : It is the last step which is used after executing loop each time and increment and decrements operators are used for the updation.
Syntax of " for " statement :
for ( initialization ; condition ; updation ) { }
Note: Any variable declared inside the for block will be treated as local variable and the scope will be within that block only. If we try to print out side the block it will give compilation error " cannot find symbol ".
Why error is like " cannot find symbol " ?
It is because the memory for the local variable get deallocated at the end of the looping block and again re allocated while executing the looping block again. for ex-
for ( int i =0 ; i <= 0 ; i++ ) {
int x = 0 ; // It is a local variable, memory will be created.
} // After this memory will be de-allocated.
System.out.println("X ="+x) ; // Give compilation error "cannot find symbol".
Note : We cannot declare multiple variable of different type in the initialization part but it can be of same type. for ex-
for ( int i =0 ,float f = 20.34f ; i < 0 ; i++ ) {} // Invalid
for ( int i =0 , j = 10, x= 20, ch= 'A' ; i < 0 ; i++ , x-- , ch *=2 ) {} // valid
But declaring multiple variable of different type can be achieved by declaring all the different typed variable before the looping statement . for ex-
int i ;
float f1 ;
char ch ;
String s ;
for ( i =0 , f1 = 10.2f , ch= 'A', s="java" ; i < 0 ; i++ , f1+=100 , ch *=2,s+=ch ) {}
Some of the situation where you will get compilation error :-
final boolean b = true ;
for ( ; b ; ) { }Here any statement written after for loop will be unreachable because final boolean variable cannot be modified because of constant property and hence compiler will be sure about infinite loop and give the compilation error as " unreachable statement ".
for ( ; true ; ) { } // Here also true true indicates that it will be a infinite loop and
statement after for loop will be unreachable.
for ( ; ; ) { } // Here also condition is by default true means compilation error as
" unreachable ".
compilation error as " unreachable ".
Same situation as above with little modification to avoid compilation error :-
boolean b = true ;
for ( ; b ; ) { } // No compilation error as b can be modified as false within for block.for ( ; true ; ) { break; } // Here compiler is sure about infinite loop but still loop can be terminated at any point of time by the use of break statement
and hence control will be at the statement after for block.
and hence control will be at the statement after for block.
Same situation looks like infinite but in real it is a finite statement:-
int x = 56789 ;
for ( ; x > 0 ; x++ ) { }
It looks like infinite loop but in java there is a concept of wrapping around that means after reaching the maximum value if int type it will wrap to tits minimum value(65535) i.e negative value and hence condition will be false at that time.
for ( int i=10 ; i > 10 ; i-- ) ; // This statement is same as for ( int i=10 ; i > 10 ; i-- ) { }
The above statement is terminated with semi-colon that means it will execute till the condition get false.
for (int i=10 ; i > 10 ; i--); {
System.out.println ("Not a part of for loop");
}
Here in the above code don't think that print statement is a part of for loop because ' for' loop is terminated with semi-colon that means it will self executed till condition get false and this print will execute only once as not a part of ' for ' loop.
Like this there are many condition give will make you surprised so keep experimenting with your code.
EmoticonEmoticon