MKoD - D Programming Language

Basic While..Loop examples in D - code-name whileloop.d

Basic Newbie Stuff While..Loop examples:

// whileloop.d
// while..loop and do..while examples in D
// To Compile: C:\dmd\MKoD_ex>..\bin\dmd whileloop.d
private import std.stdio;

int main()
{
    int ix = 0;
    
    writefln( "while..loop 1 to 10 (is using main's ix, which equal 0)" );
    while ( ix < 10 )
        writefln( " ix=%d", ++ix ); //pre-addition happens before it's printed
        
    writefln();
    
    writefln( "while..loop 10 to 1 (is using main's ix, which now equals 10)" );    
    while ( ix > 1 )
        writefln( " ix=%d", --ix ); //pre-subraction happens before it's printed

    writefln();
 
    ix = 0;
    writefln( "do..while loop, 1 to 10 step by 2" );    
    do
    {
        ix += 2;
        writefln( " ix=%d", ix );
        
    } while ( ix < 10 )
    
    writefln();
   
    ix = 0;
    writefln( "forever while..loop, that breaks out on 10" );  
    while ( true )
    {
        ix++;
        writefln( " ix=%d", ix );
        if ( ix == 10 ) break;
    }
            
    return 0;
    
} // end int main()
C:\dmd\MKOD_ex>..\bin\dmd whileloop.d
C:\dmd\bin\..\..\dm\bin\link.exe whileloop,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>whileloop
while..loop 1 to 10 (is using main's ix, which equal 0)
 ix=1
 ix=2
 ix=3
 ix=4
 ix=5
 ix=6
 ix=7
 ix=8
 ix=9
 ix=10

while..loop 10 to 1 (is using main's ix, which now equals 10)
 ix=9
 ix=8
 ix=7
 ix=6
 ix=5
 ix=4
 ix=3
 ix=2
 ix=1

do..while loop, 1 to 10 step by 2
 ix=2
 ix=4
 ix=6
 ix=8
 ix=10

forever while..loop, that 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>
Mars: fourth Rock from the Sun.