Ruby has conditional initialization. Apparently, Java does not or does it? I try to write more succintly, to limit the range as small as possible.
import java.io.*;
import java.util.*;
public class InitFor{
public static void main(String[] args){
for(int i=7,k=999;i+((String h="hello").size())<10;i++){}
System.out.println("It should be: hello = "+h);
}
}
Errors
Press ENTER or type command to continue
InitFor.java:8: ')' expected
for(int i=7,k=999;i+((String h="hello").size())<10;i++){}
^
Puzzles
- Is it possible to assign a value to an declared value in while-loop? YES code1
- Assignment in for-loop conditional? YES code2
- conditional init NO
- Can you assign different types of values in loops? YES in a reply
- Some rule for initialization inside loops? Declare outside to access values later, what about init? (?)
1. CODE
import java.io.*;
import java.util.*;
public class InitFor{
public static void main(String[] args){
int k=5;
while((k=(k%3)+1)!=1){
System.out.println(k);
}
//PRINTs only 3
}
}
2. CODE
import java.io.*;
import java.util.*;
public class InitFor{
public static void main(String[] args){
int k=5;
for(;(k=(k%3)+1)!=1;){
System.out.println(k);
}
//PRINTs only 3
System.out.println(k);
// WHY DOES IT PRINT 1? Assign in for-loop!
}
}
Part of the Original problem with many different kind of assignments and initializations -- 100lines of code in one-liner
for(int t=0,size=(File[] fs=((File f=f.getParentFile()).listFiles(filt))).size();fs==null;t++){if(t>maxDepth){throw new Exception("No dir to read");}}