Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Programming COMP153-08S Lecture 6: Making Decisions Part II.

Similar presentations


Presentation on theme: "Practical Programming COMP153-08S Lecture 6: Making Decisions Part II."— Presentation transcript:

1 Practical Programming COMP153-08S Lecture 6: Making Decisions Part II

2 Programming So Far First: Controls, Properties, Events Second: Types and Arithmetic Third: Variables and some Input/Output Fourth: Strings & Formatting Fifth: If and selection components Today: Scope, Select Case, Complex If

3 Variable scope - inside

4 Outside = Global

5 Variable scope Lifetime of variable depends on where you put the declaration Local Variables –Declarations in event handlers –Exist only during event –Can only be used in that event handler Global Variables –Declaration outside handler –Exists for the lifetime of the program –Can be used anywhere – shared by handlers

6 Two handlers

7 Fruit prices

8 The Select Case statement Select Case expression Case value statements Case value statements Case Else statements End Select

9 Fruit prices Select Case ItemTextBox.Text Case “1” PriceLabel.Text = (Convert.ToInt32(WeightTextBox.Text)* 3.99).ToString Case “2” PriceLabel.Text = (Convert.ToInt32(WeightTextBox.Text) * 5.5).ToString Case “3” PriceLabel.Text = (Convert.ToInt32(WeightTextBox.Text) * 6.0).ToString Case Else MessageBox.Show(“Please Enter 1, 2 or 3”) End Select

10 Calculator (2) Select Case (TextBox3.Text) Case "+“ Label4.Text = (Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text)).ToString Case "-" Label4.Text = (Convert.ToInt32(TextBox1.Text) - Convert.ToInt32(TextBox2.Text)).ToString Case “*” Label4.Text = (Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox2.Text)).ToString Case “/” Label4.Text = (Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text)).ToString Case Else Label4.Text = “You must use on of (*,+,-,/,^)” End Select

11 Case is clever Case 1, 3, 5 Case 1 To 10 Case 1 To 4, 7 To 9, 11, 13 Dim Number As Integer (filled in somehow) Select Case 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

12 Comparisons =Equal to <>Not equal to >Greater than =Greater than or equal <=Less than or equal to

13 Select Case and Is Dim Age As Integer (filled in somehow) Select Case Age Case Is < 5 MessageBox.Show(“Pre-school") Case 5 To 12 MessageBox.Show(“Primary School") Case Is > 12 MessageBox.Show(“Secondary or higher") Case Else MessageBox.Show(“Should never happen") End Select

14 Logical Connectives AndIf both true then result true OrIf either true then result true NotIf false then result true (vv) XorIf one and only one true then result is true – if both true or false then result is false

15 Complex If If Price >= 50 And PocketMoney < 50 Then Misery = 10 If PocketMoney > Price Or Price = SalePrice Then Misery = 0 If Tea Xor Coffee Then Drink = True Short circuit operators AndElse OrElse

16 THE END of the lecture


Download ppt "Practical Programming COMP153-08S Lecture 6: Making Decisions Part II."

Similar presentations


Ads by Google