Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Week Two

Making Decisions
The CSC221 Team
[2023|2024]
Main Text for this Course
Title
Starting out with Visual C#

Author
Tony Gaddis

Download for your progressive learning


OUTLINE

 Decision Structures
 If statement
 If-else statement
 Nested if statements

 Switch statement
 Introduction to Radio buttons, check boxes and list boxes
Decision Structures
 A decision structure allows a program to perform actions only
under certain conditions.

 They are control structures that handles the order in which


statements executes.
Example 1: Display a “good standing” message if a student’s test score is at least 25
Relational Operators & Logical Operators

 The Boolean expression that is tested by an if statement is formed


with a relational operator.
A relational operator determines whether a specific
relationship exists between two values.

 Complex Boolean expressions can be made using logical


operators.
Boolean Expressions and meaning Logical Operators and meaning
Expression Meaning Expression Meaning

X>Y Is x greater than y? X > Y && A < B Is x greater that y AND is a


less than b
X<Y Is x less than y?

X >= Y Is x greater than or equal to y? X == Y || X == Z Is x equal to y OR is x equal


to z
X <= Y Is x less than or equal to y?

X == Y Is x equal to y? ! (X > Y) Is the expression x > y NOT


true?
X != Y Is x not equal to y?
7
Decision Structures: If-Else statements

 A decision structure can have a single-alternative or dual-


alternative decision structure.

 A type of dual-alternative decision structure is called if-else


statements.
This expression has two parts: an if clause and
an else clause.

Example 2: if-else statement


Check Inputted data type

 C# allows you to verify the value you feed your program with and
convert to the required datatype.

Syntax:
datatype.TryParse(string, out targetVariable)

Examples:
int.TryParse(string, out targetVariable)
decimal.TryParse(string, out targetVariable)
double.TryParse(string, out targetVariable)
Example 3: Input Validation statement with the TryParse construct
Decision Structures: Nested Decisions

 A decision can be dependent on another decision. There can be


more than one Boolean expression within an expression
 That’s called a Nested decision.
 An example is in the previous slide
 an if and else statement in another if and else statement
Decision Structures: else-if

 C# allows a special kind of decision structure, else-if statement


 This allows you to have multiple if statements before the final
default (else) statement
Example 4: else-if statements
Decision Structure: Switch statement

 The switch statement is a multiple-alternative decision structure.


 It allows you to test the value of a variable or an expression and
then use that value to determine which statement or set of
statements to execute.
 It deals with constants, not Boolean expressions.
private void button1_Click(object sender, EventArgs e)
{ //entry point of the button click event
//Matching Numbers and their month in the year
int month_number;
if (int.TryParse(agetxt.Text, out month_number))
{ //if-statement begins here
switch (month_number) //The variable to be tested
{ //switch statement begins here
case 1:
designationlbl.Text = "January"; /*this statement runs if its the value
in the variable*/
break; //This takes the program out of the loop
case 2:
designationlbl.Text = "February";
break;
case 3:
designationlbl.Text = "March";
break;
case 4:
designationlbl.Text = "April";
break;

Example 5: An example of a switch statement


case 5:
designationlbl.Text = "May";
break;
case 6:
designationlbl.Text = "June";
break;
case 7:
designationlbl.Text = "July";
break;
case 8:
designationlbl.Text = "August";
break;
case 9:
designationlbl.Text = "September";
break;
case 10:
designationlbl.Text = "October";
break;
case 11:
designationlbl.Text = "November";
break;

Example 5: An example of a switch statement (Cont’d)


case 12:
designationlbl.Text = "December";
break;
default: /*The statement here runs if none of the outlined
cases matches the value of the variable*/
MessageBox.Show("Month entered not within twelve months of the year");
break;
} //switch statement ends here
} //if-statement ends here
else
{
MessageBox.Show("Enter valid Integer");
}

} //exit point of the button click event

Example 5: An example of a switch statement (Cont’d)


Comparing Strings

 Relational operators and methods can be used to compare strings.


Introduction to Radio buttons and Check boxes

 Radio buttons, checkboxes, and List boxes can be used for


selection.
 Radio buttons are used to pick just one option from a set
 The Radio button can be used to trigger an event.
 To group a set of options, put them on a Panel. Radio buttons on
the same Panel are members of the same group
Panel
Radio Button
Introduction to Check boxes
 Check boxes are similar to the radio buttons
 Check boxes allows you to pick more than one option from a group of
check boxes at a time.
 Let’s see an ordering example on the next slide
 In this example, there are three items a customer can order
 One order can be chosen and in a similar vein, all the items can be chosen if s/he
wishes to order all
 In this scenario, radio buttons would not be appropriate, instead check boxes are
used.
private void Order_Click(object sender, EventArgs e)
{
int itemsPicked = 0;
string order = "You have ordered:" + "\n";
if (toastcb.Checked){
order += toastcb.Text + "\n";
itemsPicked++;
}
if (brandegcb.Checked){
order += brandegcb.Text + "\n";
itemsPicked++;
}
if (drinkcb.Checked){
order += drinkcb.Text + "\n";
itemsPicked++;
}
if(itemsPicked == 0)
MessageBox.Show("Make a choice");
else
MessageBox.Show(order);
}
}
Example 6: Item Ordering example with check boxes
Sample Outputs based on varieties of choices
Question 1: Why the use of multiple if statements and not else-if or nested if
statements?
Answer 1: Using checkbox controls instead of radio buttons simply means multiple
choices can be made at the same time. Therefore, using an else-if statement will not
consider all the possible options, i.e., if a more than one choice has been made at
the same time.

Question 2: Why declare the integer variable “itemsPicked”?


Answer 2: This variable keeps track of how many options are selected as the
code execution navigates through each if statement. If no option is picked, then
none of the first three if-statements will be executed and therefore a message
will be displayed that the user should make a choice.

Explanation on the code in Example 6


Question 3: What does the expression below mean?

order += brandegcb.Text + "\n";


Answer 3: It concatenates the current text in the variable “order” with the
expression on the RHS of the += operator

So that, in case a previous item had been picked (toast in this case), the message
indicating the selection of “Bread and Egg” will not override that of the
previous selection. Rather, a concatenation is done.

The string literal "\n" makes any possible concatenation to the current string
to appear on a new line.

Explanation on the code in Example 6 (Cont’d)


Introduction to List boxes

 List boxes display a list of items and allow the user to select an item
from the list.
Each list equals a new item on the list To add Items to the list after adding it to the form,
click the button below in the property menu
Note
SelectedIndex is an integer value representing
the index of the selected item in the ListBox.
WHILE
SelectedItem represents the value of the actual
item selected in the List Box
REVIEW QUESTIONS
Review Question 1
Income Tax Calculation
Income tax is payable on the basis of tax rate applied to a specific range of taxable income, which is
called a tax bracket. Following is a fictitious tax rate schedule:
Tax Rate Tax Bracket
10% $0 – $10,000
15% $10001 – $50,000
25% $50,001 – $100,000
30% over $100,001
Create an application that lets the user enter his or her taxable income. The program should then check
which tax bracket he or she is in, calculate and display the amount of the tax to be paid, and his/her net
income after paying tax.
Review Question 2

Time Calculator
Create an application that lets the user enter a number of seconds and works as
follows:
 There are 60 seconds in a minute. If the number of seconds entered by the user is greater than
or equal to 60, the program should display the number of minutes in that many seconds.
 There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater
than or equal to 3,600, the program should display the number of hours in that many seconds.
 There are 86,400 seconds in a day. If the number of seconds entered by the user is greater
than or equal to 86,400, the program should display the number of days in that many seconds.
Review Question 3
Distance Converter
 In the English measurement system, 1 yard equals 3 feet and 1 foot
equals 12 inches.
 Use this information to create an application that lets the user
convert distances to and from inches, feet, and yards.
 The user enters the distance to be converted into a TextBox. A ListBox
allows the user to select the units being converted from, and another ListBox
allows the user to select the units being converted to.
Review Question 3 (Cont’d)

Note: Be sure to handle the situation where the user picks the same units from both list boxes. The
converted value will be the same as the value entered.

You might also like