skip navigation
 
 
 
WebInSight
Previous Next

Conditionals

The first step toward doing something intelligent is being able to choose to take a different action based on the input that you're given. For example, if you're given chocolate cake you might choose to eat it, else if you're given Brussels sprouts you might not. Conditional expressions allow you to specify similar options for your programs.

if

The most simple conditional is an if statement. Let's first look at the syntax:

if (<test>) 
{
	<statements>
}

An if statement defines a block of code (with curly braces) that is only executed if a test (or condition) is true. Here is a more concrete example:

string response = "What is the password?";
if (message == "password") 
{
	response = "Password accepted. You are now in the if statement.";
}
return response;

Copy this example into your chatbot's HandleMessage method body, compile, run, and observe its behavior. What is the response if you type anything besides "password"? How about if you type "password"? The == (two equal signs) symbol means equals, as opposed to = (one equal sign), which means 'is assigned to'. The code here says to enter the if statement only if the two strings are equal.

Here is the full source for this example: IfBot.cs

if-else

The following code that could be placed in the HandleMessage method causes the chatbot to respond with "Yum!" if the message received is "cake". Otherwise, the chatbot responds with "Yuk. No thanks."

if(message == "cake") 
{
	return "Yum!";
} 
else 
{
	return "Yuk. No thanks.";
}

The if-else statement is very similar to the if statement. In an if-else statement, if the test is true, then the "if" block is executed, else (if the test is false) the "else" block is executed. If the first string is not equal to the second string, the computer will follow the instructions in the block of code defined by the curly brackets following the "else." Note that the return statement, if executed, interrupts the program flow, causing the method it is in, to stop.

Here is the full source for this example: IfElseBot.cs

You can also test to see if two things are not equal by using != instead of ==. In computer science lingo, the exclamation point is pronounced "not," so != is pronounced not equals.

if-else-if

There is one more variant of the if statement that is useful when you want to enter one of many blocks conditionally, instead of just one of two. Observe the following code sample:

if (message == "one") 
{
	return "Entered the first block";
} 
else if (message == "two") 
{
	return "Entered the second block";
} 
else 
{
	return "Entered the third block";
}

The if-"else if"-else provides a way to chain together many if-else statements so that only one of the conditional blocks are executed. This can be useful in a chatbot because we can only return one time per HandleMessage execution. Let's look at a more practical example:

if (message == "hi") 
{
	return "hello";
} 
else if (message == "hey") 
{
	return "hello";
} 
else if (message == "bye") 
{
	return "Alright, see you later";
} 
else 
{
	return "Good conversation. Let's keep it rolling!";
}

Here is the full source for this example: IfElseIfBot.cs

Nested conditionals

Since if statements define blocks of code that can have any statements inside of them, it is possible to put another if statement inside this block. This technique is known as nesting conditionals. We'll talk more about this in a later section.

Previous Next
 
This work is supported by the following National Science Foundation Grants: CNS-087508, CNS-0549481, IIS-0811884, IIS-0415273
Send comments to Richard Ladner