D: Decisions


The D programming languages provides the following types of decision control structures:

  • IF statement and nested IF statements
  • Switch statement and nested Switch statements
  • Ternary ? operator

Example: IF statement in D


	import std.stdio;
	void main() {

		int salary = 45000;

		if (salary > 40000)
			writeln("Salary: ", salary);
	}
	

Output:

  Salary: 45000


Example: IF statement with multi-line block using {} in D


	import std.stdio;
	void main() {
		int salary = 45000;
		
		if (salary > 40000)
		{
			writeln("You reecived a raise.");
			writeln("Salary: ", salary);
		}
	}
	

Output:

  You reecived a raise.
  Salary: 45000


Example: IF-ELSE statement in D


	import std.stdio;
	void main() {

		int salary = 35000;

		if (salary > 40000)
			writeln("You reecived a raise.");
		else
			writeln("You did not reecive a raise.");
	}
	

Output:

  You did not reecive a raise.


Example: IF-ELSE IF-ELSE statement in D


	import std.stdio;
	void main() {
		int score = 120;
		
		if (score > 200)
			writeln("Great game.");
		else if (score > 150)
			writeln("Very good game.");
		else
			writeln("Good game.");
	}
	

Output:

  Good game.