This lesson will cover changing the flow of code using conditional logic. You will be making if() statements in code that represent paths that your code can take - like a fork in the road - branching off in different directions depending on whether or not a particular condition is met. In addition to this, we will introduce new Server Controls like the CheckBox that give you more flexibility and options with your code.
Available for use in the Before folder of the provided code is a started project, with the Server Controls already in place. If you choose to use this, continue to step 2, otherwise follow the directions below.
Create a new ASP.NET project and call it “CS-ASP_011,” and in Design Mode set up Default.aspx as follows:
The programmatic IDs for these Controls are:
The purpose of this application will be to evaluate what the user enters for each TextBox Control. A message will then be displayed depending on whether or not the values are considered to be equivalent.
First, double-click the okButton to create an EventHandler for the okButton_Click event. Write an empty if() statement inside of this code block:
The part inside the parentheses that says “true” will be replaced by an expression that will be evaluated as either true or false. The squiggly brackets directly beneath this if() statement contain a code block that will execute only in the case that the expression evaluates as true. Let’s now evaluate whether or not each TextBox is equivalent to the other:
Notice that we are using the double-equal’s sign, which is the equivalence operator. The assignment operator (=) is used to make two values equivalent, i.e. resultLabel.Text = "Hi". In this example, we don't want to assign anything at all, we just want to check for equivalence, which is why we use the equivalence operator. Now, let’s specify what happens if the values entered for each TextBox are equivalent and test it out by running the application:
You may notice that once the message is displayed, it will continue to stay there even when inequivalent values are set, because we never change its value. What we will need to do is reset resultLabel.Text each time the okButton_Click event is run. We achieve this by initializing the resultLabel.Text to an empty string. And now, when inequivalent values are entered, the code will completely skip the conditional code block leaving an empty resultLabel.Text to display:
But what if we want to display a message in the event that the values are nonequivalent? Logically speaking, we need to run another conditional check to catch this event. However, in this case there are only two possible results: The values are equivalent or they are not. In these cases, an else() clause will satisfy the requirement by catching the outlying case. In English, the logic would go like this: If the values are equivalent, do this. Else, do this:
The key point worth noting about conditional statements is how execution flow changes depending on whether or not the condition evaluates as true. Your application can have entire functions that are never used unless certain conditions are met. This allows applications to be more dynamic than simply executing one line after the other, flowing in a static pattern.
Next, add a CheckBox Server Control in the Defalut.aspx between the TextBoxes and the okButton:
Now set the programmatic ID for this CheckBox to “coolCheckBox,” and set the text to “Are you cool?” Also, notice how the CheckBox can be initialized in the Properties window to one of two possible boolean values (true or false).
In the okButton_Click event, comment out the previous conditional block and create a new one to determine what happens whether or not the CheckBox is checked, using the Checked property. If it is checked, change the Text property of the resultLabel:
The conditional being evaluated here is the current state of coolCheckBox.Checked. Since it is a bool, and is inherently either true or false, we don’t even have to include the reference to “true” in the evaluation. Because of this, you can just write:
This is true of all variables or properties that are boolean in nature. The if() conditional is already checking to see if the value is true, so you can save yourself time and simplify your code by simply checking the property.
Now back in the designer, add three separate RadioButton Controls to Default.aspx, and change the Text properties of each to what follows:
Make the respective programmatic ID’s as follows:
To evaluate all of these CheckBoxes, we could write out separate if() statements to check which RadioButton is selected in the okButton_Click event. However, there is a much more elegant and intuitive way to handle multiple possibilities, and that is by sandwiching else if() statements in between if() and else() statements. Comment out the previous conditional code blocks and create this new set:
Whereas the else() statement does not check a condition because it always runs when the other code checks are false, the else if() does take in a condition. This means that the else if() code blocks will only execute if a) all preceding if() or else if() statements are false, and b) their condition is true. Otherwise, the final else() statement will execute.
When you run the application now you will see the result of these multiple, branching conditionals:
The if(), else if(), else() set of conditional statements cannot be done out of order. You must always start with a single if() statement, and (optionally) end with a single else() statement. Any number of else if() statements can be included, but these always have to come between the if() and else() statements.
RadioButtons are meant to be mutually exclusive selections; in other words, only a single RadioButton can be selected at any given moment. However, there is a problem in our code because at the moment we can select as many as we want:
What we need to do is make each RadioButton a part of a group. This can be achieved by setting the GroupName property for each Radio Button. To do this, change each GroupName to “FoodGroup”:
Also worth mentioning is that the application does not let you deselect a RadioButton, it only lets you change a selection from one to another. This means that the only case where the final else() clause will execute is if we never make a single selection, yet press the “OK” button anyways:
Note that we can force a selection and eliminate the need for the final else() clause by setting the default Checked value of one of the RadioButtons to “True”:
This covers the basics of conditional statements in C#. You've learned how to create if(), else if() and else() statements to evaluate properties and values. In the future, we'll cover the switch statement, which handles more complex conditional logic, but for all our needs right now, these statements will be sufficient. Good job so far, keep it up!
Lesson 11 - Conditional If Statements