Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: conditional initialization?
    primarykey
    data
    text
    <p>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.</p> <pre><code>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())&lt;10;i++){} System.out.println("It should be: hello = "+h); } } </code></pre> <p><strong>Errors</strong></p> <pre><code>Press ENTER or type command to continue InitFor.java:8: ')' expected for(int i=7,k=999;i+((String h="hello").size())&lt;10;i++){} ^ </code></pre> <p><strong>Puzzles</strong></p> <ol> <li>Is it possible to assign a value to an declared value in while-loop? <strong>YES</strong> code1</li> <li>Assignment in for-loop conditional? <strong>YES</strong> code2</li> <li>conditional init <strong>NO</strong> </li> <li>Can you assign different types of values in loops? <strong>YES in a reply</strong></li> <li>Some rule for initialization inside loops? Declare outside to access values later, what about init? <strong>(?)</strong></li> </ol> <p><strong>1. CODE</strong></p> <pre><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 } } </code></pre> <p><strong>2. CODE</strong></p> <pre><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! } } </code></pre> <p><strong>Part of the Original problem with many different kind of assignments and initializations -- 100lines of code in one-liner</strong></p> <pre><code>for(int t=0,size=(File[] fs=((File f=f.getParentFile()).listFiles(filt))).size();fs==null;t++){if(t&gt;maxDepth){throw new Exception("No dir to read");}} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload