Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 766251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:01:04+00:00 2026-05-14T17:01:04+00:00

Ruby has conditional initialization. Apparently, Java does not or does it? I try to

  • 0

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

  1. Is it possible to assign a value to an declared value in while-loop? YES code1
  2. Assignment in for-loop conditional? YES code2
  3. conditional init NO
  4. Can you assign different types of values in loops? YES in a reply
  5. 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");}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:01:05+00:00Added an answer on May 14, 2026 at 5:01 pm

    I think you are confusing assignment with declaration. You could do

    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);
    }
    

    the =’s operator is right associative and sets the first argument to the second argument and evaluates to the first argument, so something like

    a = b = c = 4
    

    is the same as

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

    which sets c to 4, b to c (now 4) and a to b (now also 4)

    your one line of code could be (reformatted for clarity)

    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 – (though that’s really ugly code and if you checked it in on my project I’d be telling you to rewrite it ASAP)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby has a wealth of conditional constructs, including if / unless , while /
Ruby has a select method that takes an array and returns a subarray consisting
Ruby has been around for a while now so I was wondering if there
I recently installed jruby on a machine that also has ruby installed on it.
I have a RoR application running on box1...it obviously has Ruby, RubyGems, and Rails
Ruby on Rails has become a new competitive face in the server programming industry,
Ruby on Rails has a screencast presentation they use to promote their framework that
Ruby 1.9 has a few new syntax elements, such as the {key: value} hash
I'm writing a ruby program that has 2 threads. One that listens on an
I am currently developing a ruby application that has a large number of different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.