Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Programming COMP153-08S Lecture: Repetition Continued.

Similar presentations


Presentation on theme: "Practical Programming COMP153-08S Lecture: Repetition Continued."— Presentation transcript:

1 Practical Programming COMP153-08S Lecture: Repetition Continued

2 Programming So Far First: Controls, Properties, Events Second: Types and Arithmetic Third: Variables (Global/Local) Fourth: Decisions If and Select This week: Repetition

3 Protecting Input Dim count As Integer If IsNumeric(TextBox1.Text) Then count = Convert.ToInt32(TextBox1.Text) count = count Label1.Text = count.ToString Else MessageBox.Show("please enter a number") End If ‘More flexible if use Val in line 3

4 Displaying counters in a textbox A textbox by default is a single-lined component. Setting the multiline property changes all of that and enables text to appear on several lines – setting the ScrollBars property to vertical helps! Special characters are needed to enable lines to be displayed separately.

5 Button Event Dim i As Integer Dim wrap As String ' Carriage return Chr(13) and line feed Chr(10) wrap = Chr(13) & Chr(10) For i = 1 To 10 TextBox1.Text = TextBox1.Text & "Line " & i & wrap Next i

6 Result

7 More complex loops What if we wanted lines that were multiples of 5 say from 5 to 100 Dim i As Integer Dim wrap As String ' Carriage return Chr(13) and line feed Chr(10) wrap = Chr(13) & Chr(10) For i = 5 To 100 Step 5 TextBox1.Text = TextBox1.Text & "Line " & i & wrap Next i

8 Step results Note the scroll bar is now active

9 Exit a For Next Loop What if you don’t want to complete all the repetitions requiring premature exit? Dim i As Integer Dim wrap As String ' Carriage return Chr(13) and line feed Chr(10) wrap = Chr(13) & Chr(10) For i = 5 To 100 Step 5 If i = 20 Then Exit For TextBox1.Text = TextBox1.Text & "Line " & i & wrap Next i

10 Exit results

11 Potentially unrestricted loops Syntax has two forms: Do While condition block of statements Loop Do block of statements Loop While condition

12 Bouncing bear – real fast! Dim GoingRight As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GoingRight = True End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click While True If GoingRight Then PictureBox1.Left = PictureBox1.Left + 2 If PictureBox1.Left > ActiveForm.Width - PictureBox1.Width Then GoingRight = False End If Else PictureBox1.Left = PictureBox1.Left - 2 If PictureBox1.Left = 0 Then GoingRight = True End If End While End Sub

13 Why have two forms of while loop? The first may not do the block of statements (if condition is false) The second does the block at least once – sometimes this is better.

14 Example – Do While Dim InpName As String InpName = “” Do While InpName <> “Done” InpName = InputBox(“Enter your name or type Done to quit.”) If InpName <> “Done” Then TextBox1.Text = InpName End If Loop

15 Example Loop While Dim InpName As String Do InpName = InputBox(“Enter your name or type Done to quit.”) If InpName <> “Done” Then TextBox1.Text = InpName End If Loop While InpName <> “Done” -- This is better since we don’t make any assumptions about InpName. NOTE: To exit we use Exit Do

16 Final Example Here we will write a form load event handler – just double click on the Form in design mode. Dim Ftemp, Celsius As Single Dim strFTemp As String Const Prompt As String = "Enter the temp in F" Do strFTemp = InputBox(Prompt, "F to C") If strFTemp <> "" Then Ftemp = Convert.ToSingle(strFTemp) Celsius = ((Ftemp + 40) * 5 / ) MessageBox.Show(Celsius.ToString("N0"), "Temp in C") End If Loop While strFTemp <> ""

17 F to C

18 THE END of the lecture


Download ppt "Practical Programming COMP153-08S Lecture: Repetition Continued."

Similar presentations


Ads by Google