1 / 29

UTPA – Fall 2011

CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2). UTPA – Fall 2011. Objectives. In this chapter, you will: Learn more control structures Repetition statements For … Next, Do … Loop While, Do … Loop Until Selection Select … Case

sullivanp
Download Presentation

UTPA – Fall 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011

  2. Objectives • In this chapter, you will: • Learn more control structures • Repetition statements • For … Next, Do … Loop While, Do … Loop Until • Selection • Select … Case • Know how to use Exit and Continue statements to break or terminate the current iteration • Learn how to use logical operators

  3. In the Last Class: Repetition Structure • Visual Basic provides 7 repetition statements • Do While … Loop • While … End While • Do Until … Loop • Do … Loop While • Do … Loop Until • For … Next • For Each … Next

  4. Example of For … Next Repetition Statement • Output even numbers between 2 and 10 • For counter AsInteger = 2 To 10 Step 2 • outputLabel.Text &= counter & " " • Next

  5. Discussions on For … Next Repetition Statement • For counter AsInteger = 2 To 10 Step 2 … Next • Keywords: For, To, Step, Next • "counter AsInteger" declares a counter of integer data type • counter – control variable name • Integer – control variable type • Initial value of control variable – 2 • Final value of control variable – 10

  6. Discussions on For … Next Repetition Statement (cont'd) • For counter AsInteger = 2 To 10 Step 2 … Next • Increment of control variable – Step 2 • If "Step 2" is missing, the default value is 1 • Output all integers from 2 to 10 • For counter AsInteger = 2 To 10 outputLabel.Text &= counter & " " Next • Result: 2, 3, 4, 5, 6, 7, 8, 9, 10

  7. General Form of a For … Next Statement • ForinitializationTofinalValueStepincrement statement Next

  8. Other Examples of For … Next (1) • Declaring the control variable before a For…Next Statement • Dim counter AsInteger For counter = 2 To 10 Step 2 outputLabel.Text &=counter & " " Next • Using expressions in the For…Next statement • For j AsInteger = x To 4*x*y Step y \ x

  9. Other Examples of For … Next (2) • Default increment: 1 • For i = 1 To 100 • For i = 1 To 100 Step 1 • Decrement • For i = 100To1Step-1

  10. Example 5.5: InterestCalculator.vb • URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

  11. Example 5.5: InterestCalculator.vb (cont'd) • NumericUpDown control • Minimum property • Maximum property • Increment property • Value property

  12. Example 5.5: InterestCalculator.vb (cont'd) • Val(…) function • Convert strings to numbers • Dim principal AsDecimal = Val(principalTextBox.Text) • Ignores white space in the string • E.g. "33 5" will be converted to 335 • vbTab • Format by adding a tab

  13. Example 5.5: InterestCalculator.vb (cont'd) • String.Format("{0:C}", amount) • Output variable amount in currency format • E.g., $1,050.00 • TextChanged event of controls TextBox and NumericUpDown • resultListBox.Items.Clear()

  14. Nested Repetition Statements • For i = 1 To 10 Step 1 For j = 1 To 20 Step 2 … Next i Next j

  15. Example 5.8: SquareOfCharacters.vb • URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

  16. Example 5.8: SquareOfCharacters.vb (cont'd) • For row AsInteger = 1 To sideLength • For column AsInteger = 1 To sideLength • outputTextBox.AppendText(fillCharacter & " ") • Next column • outputTextBox.AppendText(vbCrLf) • Next row

  17. Do…Loop While and Do…Loop Until Repetition Statements • The loop body is always executed at least once Dim product AsInteger = 1 Do product = product * 3 Loop While product <=100 ------------------------------------------------------------------------------- Dim product AsInteger = 1 Do product = product * 3 Loop Until product >100

  18. Use Exit to Terminate Repetition Statements • Exit Do • Terminate the repetition statements such as: • Do While…Loop, Do Until…Loop, Do…Loop While, Do…Loop Until • Exit For • Terminate For…Next • Exit While • Terminate While…End While • Exit Select • Terminate Select…Case

  19. Use Continue in Repetition Statements • Continue statementonly terminates the current iteration • Continue Do • Terminate the repetition statements such as: • Do While…Loop, Do Until…Loop, Do…Loop While, Do…Loop Until • Continue For • Terminate For…Next • Continue While • Terminate While…End While

  20. In the Last Class: Selection Structure • If … Then • If … Then … Else • Select … Case [grade>=60] display "passed" [grade<60]

  21. Multiple-Selection Statement:Select … Case SelectCase grade Case 100 perfectScoreCount += 1 ' increment perfectScoreCount aCount += 1 ' increment aCount Case 90 To 99 ' grade was between 90 and 99 aCount += 1 ' increment aCount Case 80 To 89 ' grade was between 80 and 89 bCount += 1 ' increment bCount Case 70 To 79 ' grade was between 70 and 79 cCount += 1 ' increment cCount Case 60 To 69 ' grade was between 60 and 69 dCount += 1 ' increment dCount CaseElse ' grade was less than 60 fCount += 1 ' increment fCount EndSelect

  22. Multiple-Selection Statement:Select … Case • Case Else is optional • If no case matches and there is no Case Else, then program control continues with the first statement after Select … Case • End Select terminates the Select … Case statement

  23. Example 5.9: ClassAverage.vb • URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html • TextBox • gradeTextBox.Focus() gives the focus to the gradeTextBox • String.Empty

  24. Logical Operators • Logical And operator • If gender = "F" And age >=65 Then • seniorFemales += 1 • End If

  25. Logical Operators (cont'd) • Logical Or Operator

  26. Short-Circuit Evaluation • AndAlso • If gender = "F" AndAlso age >=65 • If gender is not "F", then "age >=65" will not be evaluated • OrElse • If gender = "F" OrElse age >=65 • If gender is "F", then "age >=65" will not be evalutated

  27. Other Logical Operators • Logical Xor operator • Logical Not operator • IfNot (value = 0) Then …

  28. Rules of Operator Precedence priority • ^ • +, - (sign operations) • *, / • \ • Mod • +, - (addition and subtraction) • & • =, <>, <, <=, >, >= (equality and relational) • Not • And, AndAlso • Or, OrElse • Xor • =, +=, -=, *=, /=, \=, ^=, &= high low

More Related