Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > why is Java the way it is?

Reply
Thread Tools

why is Java the way it is?

 
 
Roedy Green
Guest
Posts: n/a
 
      08-27-2008
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
 
 
 
 
EJP
Guest
Posts: n/a
 
      08-27-2008
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
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-27-2008
On Wed, 27 Aug 2008 06:28:53 GMT, EJP
<(E-Mail Removed)> 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
 
Dirk Michaelsen
Guest
Posts: n/a
 
      08-27-2008
Roedy Green <(E-Mail Removed)> 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
 
Ian Semmel
Guest
Posts: n/a
 
      08-27-2008


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
 
Motosauro
Guest
Posts: n/a
 
      08-27-2008
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
 
mv1945
Guest
Posts: n/a
 
      08-27-2008
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
 
Tom Anderson
Guest
Posts: n/a
 
      08-27-2008
On Wed, 27 Aug 2008, Ian Semmel wrote:

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

>
> while ( booleanExpression) statement;
>
> for (ForInit;Expression;ForUpdate) statement
> where
> ForInit is StatementExpressionList OR LocalVariableDefinition
>
> You can't define a variable in a boolean expression


Or indeed, any expression. This:

int x = (int y = 1) + 1;

Isn't legal. This:

int y ;
int x = (y = 1) + 1;

Is. That's the crux of it.

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.

tom

--
It not infrequently happens that something about the earth, about the sky,
about other elements of this world, about the motion and rotation or even
the magnitude and distances of the stars, about definite eclipses of the
sun and moon, about the passage of years and seasons, about the nature
of animals, of fruits, of stones, and of other such things, may be known
with the greatest certainty by reasoning or by experience. -- St Augustine
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      08-27-2008
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
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-27-2008
> Roedy Green escribió:
>> Why is this illegal?
>> while ( int place = s.indexOf( x ) >= 0 )


It reminds me of my casual wish to just save away some
interims-result of a formula, and have the compiler
just "dup" it on the stack for immediately subsequent
use, rather than "store" it into a local variable and
(immediately subsequently) load it back from there.

But usually, I then remember the dogma about "the jit
does all the kinky magic for me - it knows *much* better
than me" and I then just save the value into a local
variable.

And then I'm wondering, why (or whether)
String all=""; for(/*some long loop*/) all += next_part;
is still my responsibility to rewrite for using explicit
StringBuilder.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hey i am just started java,, can anyone tell me the use ,application, why java , importance of java.. manish sahu Java 3 02-14-2008 12:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM



Advertisments