Learn D Programming Lesson 5 - Decisions
The if statement
The if statement is used to run code based on a condition. The if statement is made up of the word if followed by the condition in brackets which is followed by the code to run based on the condition. Here is an example that asks the user to enter a number and then tests the number to see if it is equal to 5. If the number is equal to 5 then it prints a message and it does nothing if it isn't.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
if (number == 5)
writefln("The number is equal to 5");
}
An important thing to notice is that when you test if two things are equal then you must use a double equals (==) which might be confusing because up until now you have been using a single equals (=) to set variable values. You will also see that the writefln has been indented and that is because it makes it easier to see that it belongs to the if statement before it.
The else statement
It is important that you understand that when a condition is tested it returns either a true or a false to the if statement. In the example above if a 5 is entered then the number entered by the user is equal to 5 which means it is true and if any other number is entered then it is false. You can run code for when the condition is false by using the else statement. Here is the if statement from the example above which also has an else part that tells the user when the number is not equal to 5.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
if (number == 5)
writefln("The number is equal to 5");
else
writefln("The number is NOT equal to 5");
}
Grouping if statement commands
The examples above have only had one command in their if and else parts. If you want to have more than one command then you need to put the commands inside curly brackets ({}). Here is an example that uses curly brackets to group an extra command in both the if and the else parts.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
if (number == 5)
{
writefln("The number is equal to 5");
writefln("Another command for if");
}
else
{
writefln("The number is NOT equal to 5");
writefln("Another command for else");
}
}
Other comparison types
So far we have only tested whether or not values are equal using the equality operator (==). You can also do something like test whether a value is not equal or you can test if it is greater than or less than another value. Here is a table of the operators that can be used followed by an example that makes use of all of them.
| == | Equal |
| != | Not equal |
| > | Greater than |
| >= | Greater than or equal to |
| < | Less than |
| <= | Less than or equal to |
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
if (number == 5)
writefln("The number is equal to 5");
if (number != 5)
writefln("The number is NOT equal to 5");
if (number > 5)
writefln("The number is greater than 5");
if (number >= 5)
writefln("The number is greater than or equal to 5");
if (number < 5)
writefln("The number is less than 5");
if (number <= 5)
writefln("The number is less than or equal to 5");
}
Testing multiple conditions
You can test more than one condition at the same time by using either the AND operator (&&) or the OR operator (||). The AND operator goes between two comparisons and is used to test if both the first comparison and the second comparison evaluate to true. Here is an example which asks the user to enter 2 numbers and checks if they are both equal to 5.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter the first number");
string UserInput = din.readLine();
int FirstNumber = toInt(UserInput);
writefln("Please enter the second number");
UserInput = din.readLine();
int SecondNumber = toInt(UserInput);
if (FirstNumber == 5 && SecondNumber == 5)
writefln("Both numbers are equal to 5");
}
The OR operator checks if at least one of the comparisons on either side of it evaluate to true. Here is an example.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter the first number");
string UserInput = din.readLine();
int FirstNumber = toInt(UserInput);
writefln("Please enter the second number");
UserInput = din.readLine();
int SecondNumber = toInt(UserInput);
if (FirstNumber == 5 || SecondNumber == 5)
writefln("At least one of the numbers is equal to 5");
}
The relationship between booleans and if statement conditions
if statement conditions are evaluated to booleans which means they return either a true or a false. This means that you can set the value of a boolean variable to the result of a condition. Here is an example that tests if a number is greater than 5 and stores the result in a boolean variable.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
bool IsGreater = number > 5;
if (IsGreater)
writefln("The number is greater than 5");
}
You won't do this kind of thing very often but it is important for you to learn about it because it will help you understand how an if statement really works.
The NOT operator
The NOT operator (!) is used to negate boolean values. This means that if a boolean value is true then the NOT operator makes it false and if it is false then it makes it true. Here is an example of how it is used to test whether the number that the user entered is not greater than 5.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
bool IsGreater = number > 5;
if (!IsGreater)
writefln("The number is NOT greater than 5");
}
Operator precedence and grouping
The AND, OR and NOT operaters are evaluated in a certain order. NOT is always evaluated first and then AND and finally OR. If you want to change the order of evaluation then you need to use brackets. The following is an example of a system that someone called John has set up to check his username and password. John is very forgetful so he has made it so that his system accepts 2 different passwords in case he forgets one of them. When you run the program you will see that it will only accept john as the username and either dog or cat as the password. There is one huge problem. Because of the order of precedence of the AND and the OR operators it will also allow you to login with any username as long as you use the password cat. Here is the code.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter your username");
string username = din.readLine();
writefln("Please enter your password");
string password = din.readLine();
if (username == "john" && password == "dog" || password == "cat")
writefln("ACCESS GRANTED");
else
writefln("ACCESS DENIED");
}
You can fix the problem by putting brackets around the part that tests for the password because the brackets will make it evaluate the OR before the AND. Here is the corrected version of the program.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter your username");
string username = din.readLine();
writefln("Please enter your password");
string password = din.readLine();
if (username == "john" && (password == "dog" || password == "cat"))
writefln("ACCESS GRANTED");
else
writefln("ACCESS DENIED");
}
The switch statement
The switch statement is something that is used to test for a whole lot of different conditions. Here is an example of the switch statement with an explanation of how it works underneath it.
import std.stdio;
import std.cstream;
import std.conv;
void main()
{
writefln("Please enter a number");
string UserInput = din.readLine();
int number = toInt(UserInput);
switch (number)
{
case 1:
writefln("You entered 1");
break;
case 2:
writefln("You entered 2");
break;
case 3:
case 4:
case 5:
writefln("You entered 3, 4 or 5");
break;
default:
writefln("You entered something else");
break;
}
}
The switch statement in the program above is followed by the variable called number in brackets because that is the variable which we are going to test against. The contents of a switch statement must always be grouped together inside curly brackets. The case keyword is used followed by the value to compare the switch variable to. If they are equal then the code inside the case part is run. The break keyword is used to break out of the switch statement after the code for the case part has been run. You will see that their is a part where 3 cases are stacked on top of each other. It is used to say that it must run the code after the last of the cases in the stack if the switch variable is equal to any one of them. There is also a part which uses the default keyword which is optional and is run if the switch variable is not equal to any of the cases.
Practice
You can practise the things in this lesson by thinking up a program that asks the user to enter something and then tests what the user enters using the if statement or the switch statement. For example you can write a program that asks a student for his school results and then checks if he has passed or failed.
- Learn D Programming Lesson 1 - Getting started
- Learn D Programming Lesson 2 - First program
- Learn D Programming Lesson 3 - Variables
- Learn D Programming Lesson 4 - More on strings
- Learn D Programming Lesson 5 - Decisions
- Learn D Programming Lesson 6 - Loops
- Learn D Programming Lesson 7 - Arrays
- Learn D Programming Lesson 8 - Functions
- Learn D Programming Lesson 9 - Classes
- Learn D Programming Lesson 10 - Inheritance