|
MKoD - D Programming Language
Basic For..Loop examples in D - code-name forloop.d
For..Loop examples:
// ** forloop.d
// for..loop examples in D - last tested with D v1.0
// To Compile: C:\dmd\MKoD_ex>..\bin\dmd forloop.d
private import std.stdio;
int main()
{
int ix = 0;
writefln( "for..loop 1 to 10 (for..loop that's using main's ix)" );
for ( ix = 1; ix <= 10; ix++ )
writefln( " ix=%d", ix );
writefln();
writefln( "for..loop 10 to 1 (for..loop that's not using main's ix, but it's own)" );
for ( ix = 10; ix >= 1; ix-- )
writefln( " ix=%d", ix );
writefln();
writefln( "for..loop 1 to 10 step by 2 (for..loop that's not using main's ix, but it's own)" );
for ( ix = 1; ix <= 10; ix += 2 )
writefln( " ix=%d", ix );
writefln();
ix = 0;
writefln( "forever for..loop, breaks out on 10 (is using main's ix)" );
for ( ;; )
{
ix++;
writefln( " ix=%d", ix );
if ( ix == 10 ) break;
}
return 0;
} // end int main()
C:\dmd\MKOD_ex>..\bin\dmd forloop.d
C:\dmd\bin\..\..\dm\bin\link.exe forloop,,,user32+kernel32/noi;
C:\dmd\MKOD_ex>forloop
for..loop 1 to 10
ix=1
ix=2
ix=3
ix=4
ix=5
ix=6
ix=7
ix=8
ix=9
ix=10
for..loop 10 to 1
ix=10
ix=9
ix=8
ix=7
ix=6
ix=5
ix=4
ix=3
ix=2
ix=1
for..loop 1 to 10 step by 2
ix=1
ix=3
ix=5
ix=7
ix=9
forever for..loop, breaks out on 10
ix=1
ix=2
ix=3
ix=4
ix=5
ix=6
ix=7
ix=8
ix=9
ix=10
C:\dmd\MKOD_ex>
|