|
MKoD - D Programming Language
Basic Foreach..Loop examples in D - code-name foreach.d
Foreach..Loop examples:
// foreach.d
// foreach..loop examples in D - last tested with D v1.0
// To Compile: C:\dmd\MKoD_ex>..\bin\dmd foreach.d
private import std.stdio;
int main()
{
int[] ix = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
char[] sAlpha = "ABCDEFGHIJ";
writefln( "foreach..loop 1 to 10 (with an index)" );
foreach( int iy, int iz; ix )
{
writefln( "ix[ %d ]=%d == iz=%2d", iy, ix[ iy ], iz );
}
writefln();
writefln( "foreach..loop 1 to 10 (without an index)" );
foreach( int iz; ix )
{
writefln( "ix[]=%d", iz );
}
writefln();
writefln( "foreach..loop A to J (with an index)" );
foreach( int ix, dchar dc; sAlpha )
{
writefln( "sAlpha[ %d ]=%s == dc=%s", ix, sAlpha[ ix ], dc );
}
return 0;
} // end int main()
C:\dmd\MKOD_ex>dmd foreach.d
C:\dmd\bin\..\..\dm\bin\link.exe foreach,,,user32+kernel32/noi;
C:\dmd\MKOD_ex>foreach
foreach..loop 1 to 10 (with an index)
ix[ 0 ]=1 == iz= 1
ix[ 1 ]=2 == iz= 2
ix[ 2 ]=3 == iz= 3
ix[ 3 ]=4 == iz= 4
ix[ 4 ]=5 == iz= 5
ix[ 5 ]=6 == iz= 6
ix[ 6 ]=7 == iz= 7
ix[ 7 ]=8 == iz= 8
ix[ 8 ]=9 == iz= 9
ix[ 9 ]=10 == iz=10
foreach..loop 1 to 10 (without an index)
ix[]=1
ix[]=2
ix[]=3
ix[]=4
ix[]=5
ix[]=6
ix[]=7
ix[]=8
ix[]=9
ix[]=10
foreach..loop A to J (with an index)
sAlpha[ 0 ]=A == dc=A
sAlpha[ 1 ]=B == dc=B
sAlpha[ 2 ]=C == dc=C
sAlpha[ 3 ]=D == dc=D
sAlpha[ 4 ]=E == dc=E
sAlpha[ 5 ]=F == dc=F
sAlpha[ 6 ]=G == dc=G
sAlpha[ 7 ]=H == dc=H
sAlpha[ 8 ]=I == dc=I
sAlpha[ 9 ]=J == dc=J
C:\dmd\MKOD_ex>
|