for loop parens

Ali Çehreli acehreli at yahoo.com
Fri Jul 12 23:56:46 PDT 2013


On 07/12/2013 10:34 PM, QAston wrote:> On Saturday, 13 July 2013 at 
04:42:58 UTC, QAston wrote:
 >> Also, i don't know what's wrong with parens - 2 additional keystrokes?
 >> I didn't see a for loop i a long time - ranges + foreach are
 >> everywhere. And foreach is 4 chars more to type than for :P.
 >
 > Replying to myself, but well, this is flawed, foreach is ususally
 > shorter than for.

Especially when for loops can be infinitely long. :) I think the actual 
syntax of 'for' may be surprising to most:

   http://dlang.org/statement.html#ForStatement

import std.stdio;

void main()
{
     for ( {
             int i = 0;
             double d = 0.5;

             struct S
             {
                 string s;
             }

             auto s = S("hello");

             writeln("I am in the initialization clause of a for loop!");
         } i < 10; ++i) {

         writeln("i: ", i, ", d: ", d);
         d /= s.s.length;
     }
}

Ali



More information about the Digitalmars-d-learn mailing list