digitalmars.D.learn - for loop
- RenatoL (15/15) Jan 22 2012 This works:
- Trass3r (1/2) Jan 22 2012 for (int x=0, y=0; ...)
- RenatoL (1/1) Jan 22 2012 Ops, tk u .. sometimes C# is a bad teacher :-)
- Max Klyga (4/23) Jan 22 2012 If you want to declare and initialize several variables in the for
- bearophile (8/12) Jan 22 2012 And if you need different types this sometimes is enough:
- Zachary Lund (13/25) Jan 22 2012 This is an ugly solution (and I'm not 100% sure it's valid D) but:
- Zachary Lund (3/32) Jan 22 2012 LOL, well... I missed the post that said the exact same thing I did. Oh
- Ellery Newcomer (6/18) Jan 23 2012 raise you.
- Trass3r (1/5) Jan 23 2012 wtf?
- bearophile (5/9) Jan 23 2012 I don't understand, is that a compiler bug?
- Mantis (3/12) Jan 23 2012 According to specs, this is BlockStatement that doesn't create scope, if...
- Timon Gehr (8/17) Jan 23 2012 It is not a bug.
- Jonathan M Davis (7/31) Jan 23 2012 That's a pretty cool feature actually, since it gives you much more flex...
This works: import std.stdio; void main() { int x = 0; int y = 0; for(; ((x < 5) && (y < 5)); x++, y ++) { writeln("x + y = ", x + y); } } The question is easy: is it possible to insert x and y internally for (int x = 0, int y = 0; .....) this doesn't work in D.
Jan 22 2012
for (int x = 0, int y = 0; .....)for (int x=0, y=0; ...)
Jan 22 2012
On 2012-01-22 16:23:36 +0300, RenatoL said:This works: import std.stdio; void main() { int x = 0; int y = 0; for(; ((x < 5) && (y < 5)); x++, y ++) { writeln("x + y = ", x + y); } } The question is easy: is it possible to insert x and y internally for (int x = 0, int y = 0; .....) this doesn't work in D.If you want to declare and initialize several variables in the for loop, you can do it if they are of the same type: for (int x = 0, y = 0; ...; .++x, ++y) { ... }
Jan 22 2012
Max Klyga:If you want to declare and initialize several variables in the for loop, you can do it if they are of the same type: for (int x = 0, y = 0; ...; .++x, ++y) { ... }And if you need different types this sometimes is enough: void main() { for (auto x = 0, y = 0.0; x < 10; x++, y++) { } } Bye, bearophile
Jan 22 2012
On 01/22/2012 11:08 AM, bearophile wrote:Max Klyga:This is an ugly solution (and I'm not 100% sure it's valid D) but: /+++++++++++++++++++++++++++++/ void main() { { short y = 0; int x = 0; for (; x < 10; ++x, ++y) { } } } /+++++++++++++++++++++++++++++/If you want to declare and initialize several variables in the for loop, you can do it if they are of the same type: for (int x = 0, y = 0; ...; .++x, ++y) { ... }And if you need different types this sometimes is enough: void main() { for (auto x = 0, y = 0.0; x< 10; x++, y++) { } } Bye, bearophile
Jan 22 2012
On 01/22/2012 11:37 AM, Zachary Lund wrote:On 01/22/2012 11:08 AM, bearophile wrote:LOL, well... I missed the post that said the exact same thing I did. Oh well...Max Klyga:This is an ugly solution (and I'm not 100% sure it's valid D) but: /+++++++++++++++++++++++++++++/ void main() { { short y = 0; int x = 0; for (; x < 10; ++x, ++y) { } } } /+++++++++++++++++++++++++++++/If you want to declare and initialize several variables in the for loop, you can do it if they are of the same type: for (int x = 0, y = 0; ...; .++x, ++y) { ... }And if you need different types this sometimes is enough: void main() { for (auto x = 0, y = 0.0; x< 10; x++, y++) { } } Bye, bearophile
Jan 22 2012
On 01/22/2012 11:37 AM, Zachary Lund wrote:This is an ugly solution (and I'm not 100% sure it's valid D) but: /+++++++++++++++++++++++++++++/ void main() { { short y = 0; int x = 0; for (; x < 10; ++x, ++y) { } } } /+++++++++++++++++++++++++++++/raise you. void main(){ for ({int x=0; short y=0;} x < 10; x++, y++){ } }
Jan 23 2012
void main(){ for ({int x=0; short y=0;} x < 10; x++, y++){ } }wtf?
Jan 23 2012
Ellery Newcomer:void main(){ for ({int x=0; short y=0;} x < 10; x++, y++){ } }I don't understand, is that a compiler bug? Aren't x and y in a sub-scope that ends before you use x and y? Bye, bearophile
Jan 23 2012
23.01.2012 20:06, bearophile ïèøåò:Ellery Newcomer:According to specs, this is BlockStatement that doesn't create scope, if I don't miss anything.void main(){ for ({int x=0; short y=0;} x< 10; x++, y++){ } }I don't understand, is that a compiler bug? Aren't x and y in a sub-scope that ends before you use x and y? Bye, bearophile
Jan 23 2012
On 01/23/2012 07:06 PM, bearophile wrote:Ellery Newcomer:It is not a bug. ForStatement: for (Initialize Testopt ; Incrementopt) ScopeStatement Initialize: ; NoScopeNonEmptyStatement Initialize is NoScope.void main(){ for ({int x=0; short y=0;} x< 10; x++, y++){ } }I don't understand, is that a compiler bug? Aren't x and y in a sub-scope that ends before you use x and y? Bye, bearophile
Jan 23 2012
On Monday, January 23, 2012 19:48:02 Timon Gehr wrote:On 01/23/2012 07:06 PM, bearophile wrote:That's a pretty cool feature actually, since it gives you much more flexibility with regards to the types of the variables that you declare in the beginning of the for loop (or other things that you might want to do to the variables prior to running the loop. My only concern with it would be programmers not understanding it, because they're not used to it. I may start using. - Jonathan M DavisEllery Newcomer:It is not a bug. ForStatement: for (Initialize Testopt ; Incrementopt) ScopeStatement Initialize: ; NoScopeNonEmptyStatement Initialize is NoScope.void main(){ for ({int x=0; short y=0;} x< 10; x++, y++){ } }I don't understand, is that a compiler bug? Aren't x and y in a sub-scope that ends before you use x and y? Bye, bearophile
Jan 23 2012