| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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. |
|
#3
| |||
| |||
| 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) |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 ); |
|
#7
| |||
| |||
| 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 */ } |
|
#8
| |||
| |||
| 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 |
|
#9
| |||
| |||
| 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. |
|
#10
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.