1 / 18

CS 102

CS 102. Control Structures. Overview. Need to have program statements that control execution flow Simple statements to branch execution based on conditions (If/Then/Else) Nested If statements And/Or/XOR Not Select Case statements Class variables. Conditional Statements.

walter
Télécharger la présentation

CS 102

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. CS 102 Control Structures

  2. Overview • Need to have program statements that control execution flow • Simple statements to branch execution based on conditions (If/Then/Else) • Nested If statements • And/Or/XOR • Not • Select Case statements • Class variables

  3. Conditional Statements • All of the previous VB.NET that you have been shown are imperative commands: • Do this, then that • Linear • Only branching is some of them are event handlers • It is essential to have conditional branching in your programs: • User input • Based on current machine condition • Other factors

  4. Conditional Example Select random number (1-100) Ask for their guess Tell them if it was low/high Is it right? No Yes Yay!!

  5. Conditional Structure • Boolean logic (True/False) • Can be complex statement • Often uses variables • Structure is: If <condition> Then <statement(s)> End If • Condition can use “And” and “Or” • Statement block can be as long and complex as needed • Must ALWAYS have an “End If” to match the If

  6. Example • As long as the left side resolves to True/False: If 3 * 2 = 6 Then MsgBox("It is = 6!!") End If • More complex: If ChkTest.Checked Then MsgBox("Text box is checked") End If • Other logic: If ChkTest.Checked And ChkTest2.Checked Then MsgBox("Text boxes are both checked") Else MsgBox("They are not both checked") End If

  7. Else And ElseIf • Else clause executes if the “If” clause fails • Else can have any statement(s) included in the block If Not ChkTest.Checked Then MsgBox("Text box 1 not checked") Else If ChkTest2.Checked Then MsgBox("Both are checked!") End If End If • Results can be understood in a chart

  8. Nested If • You can nest If statements as deeply as you want • Each If must have an “End If” to match it • Can nest as deeply as you want If ChkTest.Checked Then MsgBox("Text box 1 is checked") Else If ChkTest2.Checked Then MsgBox("Checkbox 2 is checked") Else If chktest3.checked Then MsgBox("Checkbox 3 is checked") End If End If End If

  9. Other Boolean Operators • ElseIf – Can combine both into one keyword • And – Both conditions must be true • Or – Only one condition may be true • Not – True if the condition is False, otherwise true • Xor – Exclusive Or. Only true if 1 (and only 1) of the conditions is true. Otherwise false

  10. Chart of Boolean Logic

  11. Chart Of Boolean Logic

  12. Some Other Useful Functions • String.Empty – Is true if a string has no data in it If txtMyText = String.Empty Then… • ToUpper and ToLower change the case of a string strTest = “Hello” strTest = strTest.ToUpper() • IsNumeric - Is a string expresion numeric strTest = "123abc“ MsgBox(IsNumeric(strTest)) • Length – Returns the length of a string MsgBox(“Length is: “ & strTest.Length)

  13. Some Other Useful Functions • TrimStart, TrimEnd, Trim – Trim whitespace from a string • Substring – Returns a string from the middle of another string • Multiple versions of this function. Check documentation • IndexOf – Returns an integer with the starting location of a substring within a string strTest = “123abc” MsgBox("Start location is:" & strTest.IndexOf("ab"))

  14. Select Case • Complex logic with If/Else can get long and complex • Sometimes you just want to find the one case (of many) that is true • You can use the “Select Case” statement for this • It evaluates each of the cases in turn • The first one that is true is executed • It then continues on to the next case and evaluates it • Can exit the Select Case statement with

  15. Example Select Case CInt(TxtCheck.Text) Case 0 MsgBox("A zero?? Hard to believe") Case 1 To 59 MsgBox("That's a fail :(") Case 60 To 69 MsgBox("D") Case 70 To 79 MsgBox("C") Case 80 To 89 MsgBox("B") Case 90 To 99 MsgBox("A") Case 100 MsgBox("Wow! Perfect!!") Case Else MsgBox("Not a valid score. Try again") End Select

  16. Select Case • Can have individual items • Case 0 • Can have a range of items • Case 3, 4 • Can have a catchall (to get everything else) • Case Else • Select Case can have a variety of types • Integers • Strings • Virtually any other type

  17. Class Variables • All variables have a scope. That is, the part of the program for which the variable is recognized • Variables declared in a Subroutine are only defined for that Sub/Function • Class variables are declared at the top of the class, and can be accessed anywhere in that class • If you need access to the variable, you can pass the information in one of two ways: • Input/output argument • Class variables

  18. Class Variables • Declare a class variable Public Class Form1 Dim aVar as Integer ‘ This variable can be used anywhere in the class • Pass an input variable mySub(44) Private Sub mySub(ByVal intMyInt As Integer) MsgBox(intMyInt) End Sub

More Related