View mode: basic / threaded / horizontal-split · Log in · Help
January 22, 2012
for loop
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
Re: for loop
> for (int x = 0, int y = 0; .....)

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

wtf?
January 23, 2012
Re: for loop
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
Top | Discussion index | About this forum | D home