Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)

Similar presentations


Presentation on theme: "Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)"— Presentation transcript:

1 Control Structures

2  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  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 Select random number (1- 100) Yay!! Ask for their guess Is it right ? Tell them if it was low/high No Yes

5  Boolean logic (True/False)  Can be complex statement  Often uses variables  Structure is: If Then 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  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 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  Results can be understood in a chart

8  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

9  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 Cond1 (A)Cond2 (B)A Or BA And BA Xor BA nand B False True False True

11 Cond1 (A)Cond2 (B)A Or BA And BA Xor BA nand B False True FalseTrue FalseTrue FalseTrueFalseTrue False

12  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  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  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 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  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  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  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


Download ppt "Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)"

Similar presentations


Ads by Google