Making Decisions
In programming some problems require making decisions.
A decision making is a basic programming construct that allows execution of instructions if a condition is true.
Example-1
Write a program (Speed) that allows the user to enter the speed that he travels when he is on the road to Dubai. The program with then display a based on his rate of speed. If he drives faster than 120, he is speeding; otherwise he is driving within the speed limit.
Steps
- Set up the try-catch statement for handling exceptions
- Declare the variables needed (speed)
- Get user input from the speed textbox, convert it to an integer, and store it in the variable (speed)
Determine if user is driving over the speed limit and display appropriate messages.
if (speed > 120)
{
outputLabel.Text = "You are SPEEDING!"
}
else
{
outputLabel.Text = "You are within the speed limit."
}
Example-2
Write a program (SalesBonus) that allows a salesman to input the amount of sales he has made this month. The program will give a message telling him if he will get a bonus.The company gives a bonus of AED 500.00 (BONUS) if monthly sales are more than 25,000.00 (HIGH_SALES).
Other Steps
- Set up the try-catch statement for handling exceptions
- Declare the named constants (HIGH_SALES and BONUS)
- Declare the variables needed (sales)
- Get user input from the sales textbox, convert it to a double, and store it in the variable (sales)
Determine if user is driving over the speed limit and display appropriate messages
if (sales > HIGH_SALES)
{
MessageBox.Show("You earned a bonus of " + BONUS.ToString("c"));
}
else
{
MessageBox.Show("Sorry, you sales are less than " + HIGH_SALES.ToString("c"));
}