why is Java the way it is?

This is a discussion on why is Java the way it is? within the Java forums in Programming Languages category; Why is this illegal? while ( int place = s.indexOf( x ) >= 0 ) { ... } you have to write it: int place; while ( place = s.indexOf( x ) >= 0 ) { ... } If for can do it, why not while? -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 02:02 AM
Roedy Green
Guest
 
Default why is Java the way it is?

Why is this illegal?

while ( int place = s.indexOf( x ) >= 0 )
{
...
}
you have to write it:

int place;
while ( place = s.indexOf( x ) >= 0 )
{
...
}


If for can do it, why not while?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Reply With Quote
  #2  
Old 08-27-2008, 02:28 AM
EJP
Guest
 
Default Re: why is Java the way it is?

Roedy Green wrote:
> If for can do it, why not while?


Good question. The content of an 'if' statement is essentially either
(<statement>; <expression>; <statement>)
or
(<declaration>; <expression>; <statement>).

The content of a while-condition is just <expression>.

Allowing a <declaration> in a while-condition would be +less+
symmetrical with 'for' than <expression> is: it would imply
re-initialization of the item every time around the loop, which is
asymmetric with 'for', where it only happens once.
Reply With Quote
  #3  
Old 08-27-2008, 02:57 AM
Lee Fesperman
Guest
 
Default Re: why is Java the way it is?

Roedy Green wrote:
> Why is this illegal?
>
> while ( int place = s.indexOf( x ) >= 0 )
> {
> ..
> }


I think you must be a little bleary-eyed, tonight.

A for has three sub-clauses. Only the first can contain a variable
declaration. The second must be a boolean expression like while.

You also can't write:

int place = s.indexOf( x ) >= 0 ;

> you have to write it:
>
> int place;
> while ( place = s.indexOf( x ) >= 0 )
> {
> ..
> }


Even this won't work. It would need to be:

int place;
while ( (place = s.indexOf( x )) >= 0 )
{
...
}

Notice the extra parentheses. I'm assuming you are changing x inside
the loop.

> If for can do it, why not while?


Apples and oranges :^)

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
Reply With Quote
  #4  
Old 08-27-2008, 02:58 AM
Roedy Green
Guest
 
Default Re: why is Java the way it is?

On Wed, 27 Aug 2008 06:28:53 GMT, EJP
<esmond.not.pitt@not.bigpond.com> wrote, quoted or indirectly quoted
someone who said :

>Allowing a <declaration> in a while-condition would be +less+
>symmetrical with 'for' than <expression> is: it would imply
>re-initialization of the item every time around the loop, which is
>asymmetric with 'for', where it only happens once.


That makes sense logically. Pragmatically it forces you to expose
variables to wider scope than they should have.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Reply With Quote
  #5  
Old 08-27-2008, 03:00 AM
Dirk Michaelsen
Guest
 
Default Re: why is Java the way it is?

Roedy Green <see_website@mindprod.com.invalid> wrote:

>Why is this illegal?


I don't know the reason but...

>int place;
>while ( place = s.indexOf( x ) >= 0 )
>{
>..
>}


....this code does not compile. you have to write

while ( (place = s.indexOf( x )) >= 0 ) {

>If for can do it, why not while?


why then you don't use for?

for (int place; (place = s.indexOf(x)) >= 0 {

and if you prefer using while and want to redefine variable place
later again you can put the code block into braces:

{
int place;
while ( place = s.indexOf( x ) >= 0 )
{
..
}
}

int place = 42;

Dirk
Reply With Quote
  #6  
Old 08-27-2008, 03:22 AM
Ian Semmel
Guest
 
Default Re: why is Java the way it is?



Roedy Green wrote:
> Why is this illegal?
>
> while ( int place = s.indexOf( x ) >= 0 )
> {
> ..
> }
> you have to write it:
>
> int place;
> while ( place = s.indexOf( x ) >= 0 )
> {
> ..
> }
>
>
> If for can do it, why not while?


Well, none of these statements compile on my java but

Probably because they are different statements

while ( booleanExpression) statement;

for (ForInit;Expression;ForUpdate) statement
where
ForInit is StatementExpressionList OR LocalVariableDefinition

You can't define a variable in a boolean expression

This is ok
int place;
boolean b = ( ( place = s.indexOf ( 'a' ) ) >= 0 );
while ( b ) {}

This is not
boolean b = ( ( int place = s.indexOf ( 'a' ) ) >= 0 );
Reply With Quote
  #7  
Old 08-27-2008, 03:39 AM
Motosauro
Guest
 
Default Re: why is Java the way it is?

Ian Semmel ha scritto:
>
>
> Roedy Green wrote:
>> Why is this illegal?
>>
>> while ( int place = s.indexOf( x ) >= 0 )
>> {
>> ..
>> }
>> you have to write it:
>>
>> int place;
>> while ( place = s.indexOf( x ) >= 0 )
>> {
>> ..
>> }
>>
>>
>> If for can do it, why not while?

>
> Well, none of these statements compile on my java but
>
> Probably because they are different statements
>
> while ( booleanExpression) statement;
>
> for (ForInit;Expression;ForUpdate) statement
> where
> ForInit is StatementExpressionList OR LocalVariableDefinition
>
> You can't define a variable in a boolean expression
>
> This is ok
> int place;
> boolean b = ( ( place = s.indexOf ( 'a' ) ) >= 0 );
> while ( b ) {}
>
> This is not
> boolean b = ( ( int place = s.indexOf ( 'a' ) ) >= 0 );


Ok, but in the first case b gets evaluated only the first time:
if it evaluates to true it would be like writing
while(true){}

You should put the evaluation inside the while loop, or change it into a
do-while loop, in order to assign the 'place' variable before evaluating it

What I would do though would be :
int place = 0;
while(s.indexOf(x)>=0){
place = s.indexOf(x);
/* do something with place*/
x++; /* or whatever counter would be appropriate */
}
Reply With Quote
  #8  
Old 08-27-2008, 07:39 AM
mv1945
Guest
 
Default Re: why is Java the way it is?

Roedy Green escribió:
> Why is this illegal?
>
> while ( int place = s.indexOf( x ) >= 0 )
> {
> ..
> }
> you have to write it:
>


First al all: place must be boolean



> int place;
> while ( place = s.indexOf( x ) >= 0 )
> {
> ..
> }
>
>
>

There are no sense to redefine place in each iteraction

> If for can do it, why not while?
>

In a for sentence eg:
for ( int i=0; i<10;i++)
int i=0 initilize the variable but it the same al time.

Regards
Reply With Quote
  #9  
Old 08-27-2008, 08:18 AM
Daniele Futtorovic
Guest
 
Default Re: why is Java the way it is?

On 27/08/2008 13:39, Tom Anderson allegedly wrote:
> The for loop is an aberration, in that it has a very complicated
> parenthesised bit.
>
> I do agree that this is sort of annoying, though. Being able to declare
> a variable in the loop condition of a while would be tidier than having
> to declare it before the loop.


Given that you can use a 'for' over a 'while' any time and, AFAIAA,
without any penalties, I don't find that annoying at all. I prefer the
'for' anyway.

--
DF.
Reply With Quote
  #10  
Old 08-27-2008, 08:19 AM
Eric Sosman
Guest
 
Default Re: why is Java the way it is?

Roedy Green wrote:
> Why is this illegal?
>
> while ( int place = s.indexOf( x ) >= 0 )
> {
> ..
> }
> you have to write it:
>
> int place;
> while ( place = s.indexOf( x ) >= 0 )
> {
> ..
> }
>
>
> If for can do it, why not while?


If while can't do it, why not for?

for (int place; (place = s.indexOf(x) >= 0; )
{ ... }

(The deeper "why" seems impossible to answer with certainty,
being akin to "Why aren't whales called whilligs?" Best, I
think, to take it as read and move on.)

--
Eric Sosman
esosman@ieee-dot-org.invalid
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:02 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.