| Thread overview | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to RenatoL | > for (int x = 0, int y = 0; .....)
for (int x=0, y=0; ...)
| |||
January 22, 2012 Re: for loop | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Trass3r | Ops, tk u .. sometimes C# is a bad teacher :-) | |||
January 22, 2012 Re: for loop | ||||
|---|---|---|---|---|
| ||||
Posted in reply to RenatoL | 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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Max Klyga | 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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zachary Lund | 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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zachary Lund | 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 | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | > void main(){
> for ({int x=0; short y=0;} x < 10; x++, y++){
> }
> }
wtf?
| |||
January 23, 2012 Re: for loop | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply