Jump to page: 1 2
Thread overview
for loop
Jan 22, 2012
RenatoL
Jan 22, 2012
Trass3r
Jan 22, 2012
RenatoL
Jan 22, 2012
Max Klyga
Jan 22, 2012
bearophile
Jan 22, 2012
Zachary Lund
Jan 22, 2012
Zachary Lund
Jan 23, 2012
Ellery Newcomer
Jan 23, 2012
Trass3r
Jan 23, 2012
bearophile
Jan 23, 2012
Mantis
Jan 23, 2012
Timon Gehr
Jan 23, 2012
Jonathan M Davis
January 22, 2012
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.
January 22, 2012
> for (int x = 0, int y = 0; .....)

for (int x=0, y=0; ...)
January 22, 2012
Ops, tk u .. sometimes C# is a bad teacher :-)
January 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
> 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) { ... }

January 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
January 22, 2012
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)
		{
		}
	}
}
/+++++++++++++++++++++++++++++/
January 22, 2012
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...
January 23, 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++){
        }
}
January 23, 2012
> void main(){
>        for ({int x=0; short y=0;} x < 10; x++, y++){
>        }
> }

wtf?
January 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
« First   ‹ Prev
1 2