Goto statement
Goto label
jmp instruction in assembler
cmp, followed by a jnz instruction
for loop, while loop, examples of syntactic sugar
in BASIC, my first program:
10 Print "Hello"
20 Goto 10
in Visual Basic:
here:
Debug.Print("Hello")
Goto here
Debug.Print("Goodbye")
an infinite loop never ends
how would we do this without Gotos?
Do
Debug.Print("Hello")
Loop
I = 0
here:
Debug.Print("Hello" & I)
I = I + 1
If i < 3 Then
Goto here
EndIf
I = 0
Do
Debug.Print("Hello" & I)
I = I + 1
Loop While i < 3
' this is called a post-test
what if we want a pretest?
I = 0
Do While i < 3
Debug.Print("Hello" & I)
I = I + 1
Loop
I = 0
goto there
here:
Debug.Print("Hello" & I)
I = I + 1
there:
If i < 3 Then
Goto here
EndIf
So why don't we use Gotos?!
1) simpler
2) higher
3) more controlled
4) spaghetti code
http://en.wikipedia.org/wiki/Spaghetti_code
10 i = 0
20 i = i + 1
30 PRINT i; " squared = "; i * i
40 IF i >= 10 THEN GOTO 60
50 GOTO 20
60 PRINT "Program Completed."
70 END
FOR i = 1 TO 10
PRINT i; " squared = "; i * i
NEXT i
PRINT "Program Completed."
END
a move to eliminate gotos
Goto statement considered harmful
http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html
Goto statement considered harmful considered harmful
rules for using goto:
1) it should always be fairly close
2) only goto above you, never below you
Dim response As Integer, quotation As String = ""
Do
response = CInt(InputBox("Enter a number from 1 to 3."))
Loop Until response >= 1 And response <= 3
Monday, November 15, 2010
Wednesday, November 10, 2010
functions - return a value, which we can use
x = Int(2.6)
y = Int(7.6) + 6
Public Class Form1
Function Foo() As String
Dim x As Integer
x = 7
Return "Hello"
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = Foo()
Button1.Text = s
End Sub
End Class
y = f(x)
money today is worth more than money a year from now
FV = PV * (1+RATE)^NPER
but can compount mult times per year. need divide by freq
HW: Chapter 4 Programming Projects
1-4
x = Int(2.6)
y = Int(7.6) + 6
Public Class Form1
Function Foo() As String
Dim x As Integer
x = 7
Return "Hello"
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = Foo()
Button1.Text = s
End Sub
End Class
y = f(x)
money today is worth more than money a year from now
FV = PV * (1+RATE)^NPER
but can compount mult times per year. need divide by freq
HW: Chapter 4 Programming Projects
1-4
Monday, November 8, 2010
notes from lecture
static, local, class-level variables
lifetime vs. scope
final is wendesday, 12/15, from 4-6 PM in our regular room
lifetime vs. scope
final is wendesday, 12/15, from 4-6 PM in our regular room
Wednesday, November 3, 2010
lecture notes
Subs, part II
ByVal
ByRef
actual vs. formal parameters
changes to ByVal parameters do not "stick"
changes to ByRef parameters do "stick"
a reference is an arrow we can follow to the variable
a reference is an alias to the variable; an alias is another name for the variable
passing them in order
keyword parameters
we got up to end of example 2
ByVal
ByRef
actual vs. formal parameters
changes to ByVal parameters do not "stick"
changes to ByRef parameters do "stick"
a reference is an arrow we can follow to the variable
a reference is an alias to the variable; an alias is another name for the variable
passing them in order
keyword parameters
we got up to end of example 2
Another bit of feedback
regarding one of the practice midterms:
The question:
On the tracing ex. Y’s final value is 4. It therefore should be that the statement should be the else clause because <>4 or <>5 is false.
Is that correct bec. On the exam’s answer you wrote that the answer is it is there, which is the then statement.
My Answer:
https://docs.google.com/document/pub?id=1ukHo2jysIGz1wXkkpsk80-d9n9vQIpzNb4lMqEUscJA&pli=1
this is a very tricky question which, if i recall correctly, nobody got right.
Indeed, Y's final value is 4.
Consider: If y <> 4 Or y <> 5 Then
y <> 4 evaluates to False
y <> 5 evaluates to True, because 4 is not 5
So, False Or True is True
This becomes If True Then
So it will go into the If clause rather than the Else clause.
As I noted, this is a very tricky question, but if you copy the code into Visual Basic, you will see that this is what happens. People get it wrong because of the disconnect between human speech, such as English, and programming languages. You read it as "if it not 4 or five" but that is not what it means.
The question:
On the tracing ex. Y’s final value is 4. It therefore should be that the statement should be the else clause because <>4 or <>5 is false.
Is that correct bec. On the exam’s answer you wrote that the answer is it is there, which is the then statement.
My Answer:
https://docs.google.com/document/pub?id=1ukHo2jysIGz1wXkkpsk80-d9n9vQIpzNb4lMqEUscJA&pli=1
this is a very tricky question which, if i recall correctly, nobody got right.
Indeed, Y's final value is 4.
Consider: If y <> 4 Or y <> 5 Then
y <> 4 evaluates to False
y <> 5 evaluates to True, because 4 is not 5
So, False Or True is True
This becomes If True Then
So it will go into the If clause rather than the Else clause.
As I noted, this is a very tricky question, but if you copy the code into Visual Basic, you will see that this is what happens. People get it wrong because of the disconnect between human speech, such as English, and programming languages. You read it as "if it not 4 or five" but that is not what it means.
Monday, November 1, 2010
lecture notes
HW:
Sub Procedures, Part I
question 6, 8, 36, 38, 48-54 even
look at answer to one of the odd questions to get a sense of how to do it.
answer to q 38 for If blocks
If year mod 400 = 0 Then
'leap
ElseIf year mod 100 = 0 Then
' not leap
ElseIf year mod 4 = 0 Then
'leap
Else
' not leap
Endif
' leap year
If year mod 400 = 0 Or (year mod 100 <> 0 And year mod 4 = 0) Then
MsgBox("Leap year!")
MessageBox.Show("Leap year!")
Else
MsgBox("Not a leap year!")
Endif
Write a program that allows the user to use a button to toggle the color of the text in a text box between black and red.
' this will NOT work
If textbox1.ForeColor = Color.Black Then
textbox1.ForeColor = Color.Red
Endif
If textbox1.ForeColor = Color.Red Then
textbox1.ForeColor = Color.Black
Endif
' this WILL work
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.ForeColor = Color.Black Then
TextBox1.ForeColor = Color.Red
Else ' If TextBox1.ForeColor = Color.Red Then
TextBox1.ForeColor = Color.Black
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.ForeColor = Color.Black
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Visible = Not TextBox1.Visible
End Sub
biggest:
Public Class Form1
Dim biggest As Integer
Dim secondBiggest As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theNum As Integer
theNum = Val(TextBox1.Text)
If theNum >= biggest Then
' update my maxes
secondBiggest = biggest
biggest = theNum
ElseIf theNum > secondBiggest Then
secondBiggest = theNum
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.ForeColor = Color.Black
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Text = "Biggest: " & biggest & vbTab & "SecondBiggest: " & secondBiggest
End Sub
End Class
Sub Procedures, Part I
question 6, 8, 36, 38, 48-54 even
look at answer to one of the odd questions to get a sense of how to do it.
answer to q 38 for If blocks
If year mod 400 = 0 Then
'leap
ElseIf year mod 100 = 0 Then
' not leap
ElseIf year mod 4 = 0 Then
'leap
Else
' not leap
Endif
' leap year
If year mod 400 = 0 Or (year mod 100 <> 0 And year mod 4 = 0) Then
MsgBox("Leap year!")
MessageBox.Show("Leap year!")
Else
MsgBox("Not a leap year!")
Endif
Write a program that allows the user to use a button to toggle the color of the text in a text box between black and red.
' this will NOT work
If textbox1.ForeColor = Color.Black Then
textbox1.ForeColor = Color.Red
Endif
If textbox1.ForeColor = Color.Red Then
textbox1.ForeColor = Color.Black
Endif
' this WILL work
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.ForeColor = Color.Black Then
TextBox1.ForeColor = Color.Red
Else ' If TextBox1.ForeColor = Color.Red Then
TextBox1.ForeColor = Color.Black
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.ForeColor = Color.Black
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Visible = Not TextBox1.Visible
End Sub
biggest:
Public Class Form1
Dim biggest As Integer
Dim secondBiggest As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theNum As Integer
theNum = Val(TextBox1.Text)
If theNum >= biggest Then
' update my maxes
secondBiggest = biggest
biggest = theNum
ElseIf theNum > secondBiggest Then
secondBiggest = theNum
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.ForeColor = Color.Black
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Text = "Biggest: " & biggest & vbTab & "SecondBiggest: " & secondBiggest
End Sub
End Class
Subscribe to:
Posts (Atom)