Java For Loop Magic
Java is great programming Language To Do Web Applications.And also it contains many features. Many Developers don't know every features of Java. So as a Java Developer I try to share some of the Java Tips and Tricks that I know.
1) Many Developers using for loop for many purposes but many of them don't know what the way for loop actually performing. I think many of you know for loop syntax. I just recall that one.For Loop Syntax,
for(Initialization; Condition; Increment/Decrement){
//code to execute
}
But Did you know In the for loop how many arguments are required.* For your reference for loop arguments are:
1) Initialization
2) Condition
3) Increment/Decrement
Actually None of the above arguments are required.
Two semicolon is enough to run the for loop.
It will not Produce any Compile or Run Time Error.
Example:
for(;;){
System.out.println(" Java ");
}
You can use "for loop" without the Initialization part.
Example:
int i = 0;
for(; i<5; i++){
System.out.println("i = "+i);
}
You can use 'for loop' without the Initialization and Condition part.
Example:
int i = 0;
for(; ; i++){
System.out.println("i = "+i);
if(i>5){
break;
}
}
You can use 'for loop' without the Initialization, Condition and Increment/Decrement part.
Example:
int i = 0;
for(; ; ){
System.out.println("i = "+i);
if(i>5){
break;
}
i++;
}
I should point out, as I often do when I write about Java,
that I don't consider myself an expert in this area, so if you have
any corrections or supplementary information to contribute to this topic,
please do so in the comments. And by all means, do the necessary research
before you take my word as gold. I'm still learning, and I'm happy to
correct any mistaken views.
Nice Java Tips and Tricks
ReplyDelete