C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
Course: C#
Class: Batch13
Chapter Four
Making Decisions
Topics
• 4.1 Decision Structures and the if Statement
• 4.2 The if-else Statement
• 4.3 Nested Decision Structures
• 4.4 Logical Operators
• 4.5 bool Variables and Flags
• 4.6 Comparing Strings
• 4.7 Preventing Data Conversion Exceptions with the TryParse Method
• 4.8 Input Validation
• 4.9 Radio Buttons and CheckBoxes
• 4.10 The switch Statement
• 4.11 Introduction to List Boxes
4.1 Decision Structures
• A control structure is a logical design that
controls the order in which statements execute
• A sequence structure is a set of statements
that execute in the order that they appear
• A decision structure execute statements only
under certain circumstances
– A specific action is performed only if a certain
condition exists
– Also known as a selection structure
A Simple Decision
Structure
• The flowchart is a single-alternative decision
structure
• It provides only one alternative path of
execution
• In C#, you can use the if statement to write
True
such structures. A generic format is: Cold
outside
if (expression)
{ Wear a coat
Statements; False
Statements;
etc.;
}
== Equal to x == y Is x equal to y?
End
The if-else-if Statement
• You can also create a decision structure that evaluates multiple
conditions to make the final decision using the if-else-if statement
• In C#, the generic format is: int grade = double.Parse(textBox1.Text);
if (grade >=90)
if (expression) {
{ MessageBox.Show("A");
} }
else if (grade >=80)
else if (expression) {
{ MessageBox.Show("B");
}
} else if (grade >=70)
else if (expression) {
MessageBox.Show("C");
{
}
} else if (grade >=60)
… {
MessageBox.Show("D");
else }
{ else
{
} MessageBox.Show("F");
}
4.4 Logical Operators
• The logical AND operator (&&) and the logical OR operator (||) allow
you to connect multiple Boolean expressions to create a compound
expression
• The logical NOT operator (!) reverses the truth of a Boolean
expression
Operator Meaning Description
&& AND Both subexpression must be true for the compound expression to be true
|| OR One or both subexpression must be true for the compound expression to
be true
! NOT It negates (reverses) the value to its opposite one.
Expression Meaning
x >y && a < b Is x greater than y AND is a less than b?
x == y || x == z Is x equal to y OR is x equal to z?
! (x > y) Is the expression x > y NOT true?
Sample Decision Structures
with Logical Operators
• The && operator
if (temperature < 20 && minutes > 12)
{
MessageBox.Show(“The temperature is in the danger zone.”);
}
• The || operator
if (temperature < 20 || temperature > 100)
{
MessageBox.Show(“The temperature is in the danger zone.”);
}
• The ! Operator
if (!(temperature > 100))
{
MessageBox.Show(“The is below the maximum temperature.”);
}
4.5 bool Variables and
Flags
• You can store the values true or false in bool variables,
which are commonly used as flags
• A flag is a variable that signals when some condition
exists in the program
– False – indicates the condition does not exist
– True – indicates the condition exists
if (grandMaster)
{
powerLevel += 500;
}
If (!grandMaster)
{
powerLevel = 100;
}
4.6 Comparing Strings
• You can use certain relational operators and methods to
compare strings. For example,
– The == operator can compare two strings
string name1 = “Mary”;
string name2 = “Mark”;
if (name1 == name2) { }
– You can compare string variables with string literals, too
if (month ! = “October”) { }
//double.TryParse
double number;
if (double.TryParse(inputTextBox.Text, out number)) { } else { }
//decimal.TryParse
decimal number;
if (decimal.TryParse(inputTextBox.Text, out number)) { } else { }
4.8 Input Validation
• Input validation is the process of inspecting data that has been
entered into a program to make sure it is valid before it is used
• TryParse methods check if the user enters the data, but it does not
check the integrity of the data. For example,
– In a payroll program we might validate the number of hours worked.
– In a program that gets test scores, we can limits its data to an integer
range of 0 through 100.
If (choiceRadioButton.Checked) { } else { }
month
case 2:
MessageBox.Show(“February”);
break;
case 3: Display
MessageBox.Show(“March”); Display Display Display “Error:
“January” “February” “March” Invalid
break; month”
default:
MessageBox.Show(“Error: Invalid month”);
break;
}
4.11 Introduction to List
Boxes
• A list box displays a list of items and allow the user to select an item
from the list
• In C#, you can use the ListBox control to create a list box
• Commonly used properties are:
– Text: Gets or searches for the text of the currently selected item in the
ListBox
– Items: Gets the items of the ListBox
– SelectedItem: Gets or sets the currently selected item in the ListBox.
When the user selects an item, the item is stored in this property. You
can use the following to get the selected item from a list box:
selectedFruit = fruitListBox.SelectedItem.ToString();
Questions?