Information Discovery Interactive Computing

Download Report

Transcript Information Discovery Interactive Computing

Practical Programming
COMP153-06S
Lecture 3: Making
Decisions
Administration
• We are doing Lab 2
– MSDN available from CS office
– Labs and Lectures are available in the
lab and on the web
– Try to finish by Friday (20th)
• You should have had Lab 1 verified
by today, or at least by the end of
this week
Programming So Far
• First: Controls, Properties, Events
• Second: Types and Arithmetic
• The test tutorial had a simple
calculator which would have been
more useful if we could have
supplied an operation as well as 2
numbers. That is today’s topic.
Today – Making Decisions
• Computer can compare and branch
• In VB .NET
– The If statement
– The Select Case statement
• New type – Boolean (true or false)
– Eg: enabled, visible properties
• The checkbox control allows people
to make decisions – message box
for popping up a window
The If statement
If condition Then
statements
ElseIf condition Then
statements
Else
statements
End If
Condition: either a Boolean property or a
comparision (with and / or / not)
Statements: any VB .NET statements
Eg: Setting background
If condition Then
statements
Else
statements
End If
If (YellowCheckBox.Checked) Then
ActiveForm.BackColor = Color.Yellow
Else
ActiveForm.BackColor = SystemColors.Control
End If
Calculator
If (TextBox3.Text = "+") Then
Label4.Text =
Str(Val(TextBox1.Text) +
Val(TextBox2.Text))
ElseIf (TextBox3.Text = "-") Then
Label4.Text =
Str(Val(TextBox1.Text) Val(TextBox2.Text))
ElseIf (etc.)
End If
Tax rates
Tax rates (2)
•
http://www.ird.govt.nz/income-tax-individual/itaxsalaryandwageincometaxrates.html
• Up to $38,000
• 38,001 – 60,000
• Over 60,000
19.5 cents/dollar
33 cents/dollar
45 cents/dollar
If (IncomeTextBox.Text <= 38000) Then
TaxLabel.Text = IncomeTextBox.Text * 19.5 / 100
ElseIf (IncomeTextBox.Text <= 60000) Then
TaxLabel.Text = 7410 + (IncomeTextBox.Text –
38000) * 33 / 100
Else
TaxLabel.Text = 7410 + 7260 +
(IncomeTextBox.Text - 60000) * 39 / 100
End If
• Comparison with strings is sort order
The Select Case statement
Select Case expression
Case value
statements
Case value
statements
Case Else
statements
End Select
Fruit prices
Fruit prices (2)
Select Case ItemTextBox.Text
Case 1
PriceLabel.Text = WeightTextBox.Text * 3.99
Case 2
PriceLabel.Text = WeightTextBox.Text * 5.5
Case 3
PriceLabel.Text = WeightTextBox.Text * 6.0
Case Else
MessageBox.Show(“Please Enter 1, 2 or 3”)
End Select
Calculator (2)
Select Case (TextBox3.Text)
Case "+"
Label4.Text = Str(Val(TextBox1.Text) + Val(TextBox2.Text))
Case "-"
Label4.Text = Str(Val(TextBox1.Text) - Val(TextBox2.Text))
Case “*”
Label4.Text = Str(Val(TextBox1.Text) * Val(TextBox2.Text))
Case “/”
Label4.Text = Str(Val(TextBox1.Text) / Val(TextBox2.Text))
Case “^”
Label4.Text = Str(Val(TextBox1.Text) ^ Val(TextBox2.Text))
Case Else
Label4.Text = “You must use on of (*,+,-,/,^)”
End Select
Case is clever (first variable)
•
•
•
Case 1, 3, 5
Case 1 To 10
Case 1 To 4, 7 To 9, 11, 13
Dim Number As Integer = 8
Select Number ' Evaluate Number.
Case 1 To 5 ' Number between 1 and 5, inclusive.
MessageBox.Show("Between 1 and 5")
Case 6, 7, 8 ' Number between 6 and 8.
MessageBox.Show("Between 6 and 8")
Case 9 To 10 ' Number is 9 or 10.
MessageBox.Show("Greater than 8")
Case Else ' Other values.
MessageBox.Show("Not between 1 and 10")
End Select
THE END
of the lecture