created by mayson al-duwais1. using exit to terminate repetition statements to terminate different...

33
Exit...Continue & String operations Created By Mayson Al-Duwais 1

Upload: barnard-underwood

Post on 24-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1
  • Created By Mayson Al-Duwais1
  • Slide 2
  • Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special statement called: Exit statement. The program terminates that repetition statement and continues execution with the first statement after the repetition statement. Exit Do statement to exit from Do While Loop, Do Loop While statement, Exit Select statement to exit from a Select Case statement. Exit For statement to exit from For Next and Created By Mayson Al-Duwais 2
  • Slide 3 10 Then Label1.Text &= Exit Do" Exit Do End If i += 1 Loop While (i < 10) Dim i, result As Integer i = 1 result = 1 Do While (i < 10) result *= i Label1.Text &= ("i= " & i & " result= " & result & vbCrLf) If result > 10 Then Label1.Text &= Exit Do" Exit Do End If i += 1 Loop Created By Mayson Al-Duwais3 When this condition is met, Exit Do statement is executed causing loop to stop and the execution control goes to the first statement after the loop">
  • Example Exit Do Do...Loop While Do While....Loop Dim i, result As Integer i = 1 result = 1 Do result *= i Label1.Text &= ("i= " & i & "result= " & result & vbCrLf) If result > 10 Then Label1.Text &= Exit Do" Exit Do End If i += 1 Loop While (i < 10) Dim i, result As Integer i = 1 result = 1 Do While (i < 10) result *= i Label1.Text &= ("i= " & i & " result= " & result & vbCrLf) If result > 10 Then Label1.Text &= Exit Do" Exit Do End If i += 1 Loop Created By Mayson Al-Duwais3 When this condition is met, Exit Do statement is executed causing loop to stop and the execution control goes to the first statement after the loop
  • Slide 4
  • Examples: Exit Select, Exit For Select...Case For...Next Dim name As String = Ahmad Select name Case hind Label1.Text= Name is & name Case Ahmad Label1.Text= Exit Select Exit Select Case Asma Label1.Text= Name is & name Case Else Label1.Text= Not Correct End Select For number1 As Integer =1 To 5 Label1.Text= number1 = & number1 & vbCrLf If number1 Mod 3 = 0 Then Label1.Text &= Exit For Exit For End If Next Created By Mayson Al-Duwais4
  • Slide 5
  • Using Continue in Repetition Statements A Continue statement terminates only the current iteration of a repetition statement and continues execution with the next iteration of the loop. The Continue Do statement can be used in a Do While Loop and Do Loop While statement. The Continue For statement can be used in For Next statement. Created By Mayson Al-Duwais 5
  • Slide 6
  • How Continue For work? When Continue For is encountered in a For Next statement, execution continues with the statements increment expression, then the program evaluates the loop- continuation test. Created By Mayson Al-Duwais 6
  • Slide 7
  • Example Continue For For number1 As Integer = 1 To 5 Label1.Text &= "number1 =" & number1 & vbCrLf If number1 Mod 3 = 0 Then Label1.Text &= "Continue For" & vbCrLf Continue For End If Next Created By Mayson Al-Duwais7
  • Slide 8
  • How Continue Do Work? When Continue is used, the program evaluates the loop- continuation (or loop-termination) test immediately after the Continue statement executes. If a control variables increment occurs in the loop body after the Continue statement, the increment is skipped. Note that Continue Do is used for both Do...Loop While and Do While...Loop Created By Mayson Al-Duwais8
  • Slide 9
  • Example Continue Do When Increment is After Continue Do When Increment is Before Continue Do Dim i, j As Integer i = 1 j = 1 Do While (i < 6) j *= i Label1.Text &= ("i=" & i & ,j=" & j & vbCrLf) If j = 6 Then Label1.Text &= "Continue Do" & vbCrLf Continue Do End If i += 1 increment After continue Loop Dim i, j As Integer i = 1 j = 1 Do While (i < 6) j *= i Label1.Text &= ("i=" & i & ,j=" & j & vbCrLf) i += 1 increment Before continue If j = 6 Then Label1.Text &= "Continue Do" & vbCrLf Continue Do End If Loop Created By Mayson Al-Duwais9
  • Slide 10
  • Using Continue in Repetition Statements Exit and Continue in Nested Control Statements Exit For or Continue For can be used in a Do While...Loop and Do...Loop While statements, as long as that statement is nested in a For Next statement. Created By Mayson Al-Duwais 10
  • Slide 11
  • Using Exit with Nested Repetition Statements of different types Dim result As Integer = 1 For index As Integer = 1 To 3 Do While result < 8 result += 1 Label1.Text &= "index = " & index & " result = " & result & vbCrLf If result = 3 Then Label1.Text &= "Continue While index = " & index & ", result = " & result & vbCrLf Continue Do End If ' End If If result = 5 Then Label1.Text &= "Exit While index = " & index & ", result = " & result & vbCrLf Exit Do End If ' End If Loop ' End Do While...Loop Next ' End For...Loop Created By Mayson Al-Duwais11
  • Slide 12
  • Using Exit with Nested Repetition Statements of different types Created By Mayson Al-Duwais12
  • Slide 13
  • Using Continue in Repetition Statements Exit and Continue in Nested Control Statements If there are nested loops of the same type (for example, a For Next statement within a For Next statement), the statement that immediately comes after the Exit or Continue statement is the one affected. Created By Mayson Al-Duwais13
  • Slide 14
  • Using Exit with Nested Repetition Statements of same types Dim result, index As Integer result = index = 1 Do While index < 3 index += 1 Do While result < 8 result += 1 Label1.Text &= "index = " & index & " result = " & result & vbCrLf If result = 3 Then Label1.Text &= "Continue While index = " & index & ", result = " & result & vbCrLf Continue Do End If ' End If If result = 5 Then Label1.Text &= "Exit While index = " & index & ", result = " & result & vbCrLf Exit Do End If ' End If Loop ' End Do While...Loop Created By Mayson Al-Duwais14
  • Slide 15
  • Using Exit with Nested Repetition Statements of same types Created By Mayson Al-Duwais15
  • Slide 16
  • Created By Mayson Al-Duwais16
  • Slide 17
  • 17 3.2 Strings Variables and Strings Using Text Boxes for Input and Output String Properties and Methods: LengthToUpper TrimToLower IndexOfSubstring
  • Slide 18
  • Strings (continued) Concatenation The Empty String Initial Value of a String Widening and Narrowing Internal Documentation Line Continuation Scope of a Variable 18
  • Slide 19
  • 19 String Literal A string literal is a sequence of characters surrounded by quotation marks. Examples: "hello" "123-45-6789" "#ab cde?"
  • Slide 20
  • 20 String Variable A string variable is a name to which a string value can be assigned. Examples: country ssn word firstName
  • Slide 21
  • String Variable (continued) Declaration: Dim firstName As String 21 variable namedata type Assignment: firstName = "Fred"
  • Slide 22
  • String Variable (continued) You can declare a string variable and assign it a value at the same time. Dim firstName As String = "Fred" 22
  • Slide 23
  • String Variable You can assign the value of one string variable to another. Dim strVar1 As String = "Hello" Dim strVar2 As String = "Goodbye" strVar2 = strVar1 lstOutput.Items.Add(strVar2) Output: Hello 23
  • Slide 24
  • Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim president As String president = "George Washington" lstOutput.Items.Add("president") lstOutput.Items.Add(president) End Sub Output: president George Washington 24
  • Slide 25
  • Data Conversion Because the contents of a text box is always a string, sometimes you must convert the input or output. dblVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) 25 converts a String to a Double converts a number to a string
  • Slide 26
  • Concatenation Combining two strings to make a new string quote1 = "We'll always " quote2 = "have Paris." quote = quote1 & quote2 txtOutput.Text = quote & " - Humphrey Bogart" Output: We'll always have Paris. - Humphrey Bogart 26 Created By Mayson Al-Duwais
  • Slide 27
  • Appending To append str to the string variable var var = var & str Or as a shortcut var &= str 27 Created By Mayson Al-Duwais
  • Slide 28
  • String Properties and Methods "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123 Hike".Length is 8. "123 Hike".ToLower is 123 hike. "a" & " bcd ".Trim & "efg" is abcdefg. 28 Created By Mayson Al-Duwais
  • Slide 29
  • Positions in a String Positions of characters in a string are numbered 0, 1, 2, . Consider the string Visual Basic. Position 0: V Position 1: i Position 7: B Substring al begins at position 4 29 Created By Mayson Al-Duwais
  • Slide 30
  • Substring Method Let str be a string. str.Substring(m, n) is the substring of length n, beginning at position m in str. Visual Basic.Substring(2, 3) is sua Visual Basic.Substring(0, 1) is V 30 Created By Mayson Al-Duwais
  • Slide 31
  • IndexOf Method Let str1 and str2 be strings. str1.IndexOf(str2) is the position of the first occurrence of str2 in str1. (Note: Has value -1 if str2 is not a substring of str1.) "Visual Basic".IndexOf("is") is 1. "Visual Basic".IndexOf("si") is 9. "Visual Basic".IndexOf("ab") is -1. 31 Created By Mayson Al-Duwais
  • Slide 32
  • The Empty String The string "", which has no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = "" 32 Created By Mayson Al-Duwais
  • Slide 33
  • Initial Value of a String Variable By default the initial value is the keyword Nothing Strings can be given a different initial value as follows: Dim name As String = "Fred" 33 Created By Mayson Al-Duwais