Monday, October 18, 2010

lecture notes

Decision making (ch 5)
we skip the chapter on "General Procedures" for now

Relational operators
>
<
>=
<=
=
<>

yields a Boolean type value (true or false)
Dim x as Boolean
x = 5 > 6
Button1.Text = x


x = 5 = 6
Dim y as Integer
x = y = 6
is y equal to 6? if it is, assign True to x
else assign False to x

context dependent

Comparing strings, lexicographical order
Compares it using ASCII value

gives a number to each symbol

Option Compare Text
Option Compare Binary

Dim x as String
x = "A"
' Convert to a number
' add 32 to it
' Convert back to a string
' A = 65; 65 + 32 = 97; 97 = a
' B = 66; 66 + 32 = 98; 98 = b

remember your truth tables from High School

de Morgan's laws

Do

Loop While x = 5 And (y = 7 Or z <> 2)

rewrite it using Until

Do

Loop Until Not(x = 5 And (y = 7 Or z <> 2))

Loop Until x <> 5 Or Not (y = 7 Or z <> 2)

Loop Until x <> 5 Or y <> 7 And z = 2

n = 4; answ = "Y"

(( 2 < n) And (n = 5 + 1)) Or (answ = "No")
(( True) And (False)) Or (False)
(False) Or (False)
False


Short Circuiting


Dim x as String
' maybe x = "bob"
If x.Length = 3 Then

' crashes

' but short circuiting cab be our friend
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String
        If x IsNot Nothing And x.Length = 3 Then
            Button1.Text = x
        End If
    End Sub

This does NOT provide short circuiting
AndAlso OrElse

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String
        If x IsNot Nothing AndAlso x.Length = 3 Then
            Button1.Text = x
        End If
    End Sub


If condition Then
  action1
  action2
  action3
Else
  action4
  action5
End If

If block

example 1 is find the Max



Private Sub btnFindMin_Click(...) Handles btnFindLarger.Click
  Dim num1, num2, min As Double
  num1 = txtFirstNum.Text
  num2 = txtSecondNum.Text
  If num1 < num2 Then
    min = num1
  Else
    min = num2
  EndIf
  txtResult.Text = "The smaller number is " & min
End Sub


example 2 is Nesting of ifs


next time, show how to remove the nesting in this example


If

Select Case

No comments:

Post a Comment