Java: conditional initialization?

Ruby has conditional initialization. Apparently Java doesn't or doesn't it? I am trying to write more severely to limit the range as little 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);

        }
}

Mistakes

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 a declared value during a loop? YES code1
  • Is assignment in for-loop conditional? YES code2
  • conditional init NO
  • Can you assign different types of values ​​in a loop? YES in reply
  • Some rule for initializing 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 types of assignments and initializations is 100 lines of code in one layer

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");}}
+3
4

, .

public static void main(String[] args){
    String h = null;
    for(int i=7,k=999;i+((h="hello").size())<10;i++){}

    System.out.println("It should be: hello = "+h);
}

a = right , -

a = b = c = 4

(a = (b = (c = 4)))

c 4, b c ( 4) a b ( 4)

( )

File[] fs=null;                                                                            
File f= ??? ; //you never initialize f in the for loop, you need a starting value  
int t, size;                                                                               

for (t=0,size=(fs=((f=f.getParentFile()).listFiles(filt))).size();      
     fs==null;                                                                             
     t++) {
       if(t>maxDepth) {throw new Exception("No dir to read");}
     }
}

EDIT - ( , )

+1

, ​​; , . :

for(int i=7,k=999;i+((new String("hello")).length())<10;i++){}

:

public static void main(String[] args){
    String h = "hello";
    for(int i=7,k=999;(i+h.length())<10;i++){} // not sure what this is doing
    System.out.println("It should be: hello = "+h);
}

, , , for-loop, ( ).

+5

, , Java spec .

Java, , ? ? ( , Java , , ) ? . , , for - Java.

+2

Java:

variable = (condition ? expression1 : expression2);

:

int max = (faces.length>curves.length ? faces.length : curves.length);
0

Source: https://habr.com/ru/post/1743431/


All Articles