www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - for loop

reply RenatoL <rexlen gmail.com> writes:
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
in the for header? that is something like C#

for (int x = 0, int y = 0; .....)

this doesn't work in D.
Jan 22 2012
next sibling parent reply Trass3r <un known.com> writes:
 for (int x = 0, int y = 0; .....)

for (int x=0, y=0; ...)
Jan 22 2012
parent RenatoL <rexlen gmail.com> writes:
Ops, tk u .. sometimes C# is a bad teacher :-)
Jan 22 2012
prev sibling parent reply Max Klyga <max.klyga gmail.com> writes:
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
 in the for header? that is something like C#
 
 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
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply Zachary Lund <admin computerquip.com> writes:
On 01/22/2012 11:08 AM, bearophile wrote:
 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

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) { } } } /+++++++++++++++++++++++++++++/
Jan 22 2012
next sibling parent Zachary Lund <admin computerquip.com> writes:
On 01/22/2012 11:37 AM, Zachary Lund wrote:
 On 01/22/2012 11:08 AM, bearophile wrote:
 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

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) { } } } /+++++++++++++++++++++++++++++/

LOL, well... I missed the post that said the exact same thing I did. Oh well...
Jan 22 2012
prev sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/23/2012 07:06 PM, bearophile wrote:
 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

It is not a bug. ForStatement: for (Initialize Testopt ; Incrementopt) ScopeStatement Initialize: ; NoScopeNonEmptyStatement Initialize is NoScope.
Jan 23 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, January 23, 2012 19:48:02 Timon Gehr wrote:
 On 01/23/2012 07:06 PM, bearophile wrote:
 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

It is not a bug. ForStatement: for (Initialize Testopt ; Incrementopt) ScopeStatement Initialize: ; NoScopeNonEmptyStatement Initialize is NoScope.

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 Davis
Jan 23 2012
prev sibling next sibling parent "Trass3r" <un known.com> writes:
 void main(){
        for ({int x=0; short y=0;} x < 10; x++, y++){
        }
 }

wtf?
Jan 23 2012
prev sibling parent Mantis <mail.mantis.88 gmail.com> writes:
23.01.2012 20:06, bearophile пишет:
 Ellery Newcomer:

 void main(){
           for ({int x=0; short y=0;} x<  10; x++, y++){
           }
 }

Aren't x and y in a sub-scope that ends before you use x and y? Bye, bearophile

I don't miss anything.
Jan 23 2012