Professional Documents
Culture Documents
CSC221 Lecture 2
CSC221 Lecture 2
Making Decisions
The CSC221 Team
[2023|2024]
Main Text for this Course
Title
Starting out with Visual C#
Author
Tony Gaddis
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.
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
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.
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.