Make your own free website on Tripod.com

MKoD - D Programming Language

Basic If..Else If..Else examples in D - code-name ifelse.d

Basic Newbie Stuff If..Else If..Else examples:

// ifelse.d
// if..else if..else examples in D
// Updated for D v0.126 replacing '===' which is deprecated, with 'is' instead.
// To Compile: C:\dmd\MKoD_ex>..\bin\dmd ifelse.d
private import std.stdio;

int main()
{
    const bool on  = 1;
    const bool off = 0;
    enum enumModes : char { O='O', R='R', F='F' }
    
    bool bSwitchState = off;  // default Switch State is off
    char cCopierState = 'F';  // default Copier's State is powered down
    
    // Flip the Copier's on/off switch
    if ( bSwitchState == on )
    {
        writefln( "Switch was %s and has been turned off", bSwitchState ? "on" : "off" );
        bSwitchState = bSwitchState ? off : on;
        
        cCopierState = 'F';
        writefln( "Copier switched to powering down mode" );
    }    
    else
    {
        writefln( "Switch was %s and has been turned on", bSwitchState ? "on" : "off" );  
        bSwitchState = bSwitchState ? off : on;
        
        cCopierState = 'O';
        writefln( "Copier switched to power up mode...warming up" );
    }    

    // Copier is in one of three modes
    // O = On...warming up, R = On...ready, F = Powering down / Already off
    if ( cCopierState is enumModes.F )
        writefln( "Copier has powered down" );
        
    else if ( cCopierState is enumModes.O || cCopierState is enumModes.R )
        writefln( "Copier powered on and ready to use" );
          
    else
        writefln( "Copier is in an unknown state" );
              
    return 0;
    
} // end int main()
C:\dmd\MKOD_ex>..\bin\dmd ifelse.d
C:\dmd\bin\..\..\dm\bin\link.exe ifelse,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>ifelse
Switch was off and has been turned on
Copier switched to power up mode...warming up
Copier powered on and ready to use

C:\dmd\MKOD_ex>
Mars: fourth Rock from the Sun.