Control flow

An application's flow can be controlled conditionally with if and else statements:

if (a == 5) {
    writeln("Condition is met");
} else if (a > 10) {
    writeln("Another condition is met");
} else {
    writeln("Nothing is met!");

When an if or else block only contains one statement, the braces can be omitted.

D provides the same operators as C/C++ and Java to test variables for equality or compare them:

  • == and != for testing equality and inequality
  • <, <=, > and >= for testing less than (or equal to) and greater than (or equal to)

For combining multiple conditions, the || operator represents the logical OR, and && the logical AND.

D also defines a statement which executes one case depending on the value of one variable. switch works with all basic types as well as strings! It's even possible to define ranges for integral types using the case START: .. case END: syntax. Make sure to take a look at the source code example.


Basic references

Advanced references

rdmd playground.d