Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

If/Else

Part of TutorialFundamentals

Description

Simple (yet effective) flow control.

Example

import std.stdio;

void main()
{
    int anInteger;
    
    if (anInteger == 0)
        writef("It's zero!");
    else
        writef("It's not zero!");
}

Output

It's zero!

More Information

In D, all variable declarations that do not have a specific initializer get initialized with the value of their type's .init property. In this case, anInteger is of type int, and is assigned the value of int.init, which is zero.

To find out the initializer for other types, you may use the write function like so:

import std.stdio;

void main() 
{
    writeln(int.init);
    writeln(float.init);
}

Output

0
nan

For more information about .init and other properties, refer to the Properties documentation.