Presentation is loading. Please wait.

Presentation is loading. Please wait.

TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.

Similar presentations


Presentation on theme: "TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout."— Presentation transcript:

1 TUTORIAL 4 Visual Basic 6.0 Mr. Crone

2 Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout of his program Example) Create number variable Multiply variable by 100 Print variable to Main label

3 Selection Structure When programming, it is sometimes necessary for your program to make a decision on its own A Selection Structure, or Decision Structure, allows a program to execute different lines of code depending on if a condition is true or false In Visual Basic, the If-Then-Else statement is an example of a selection structure

4 If-Then-Else Statement The If-Then-Else statement has the following structure: If someCondition Then ‘code to be executed Else ‘code to be executed End If

5 Relational Operators Relational OperatorMeaning =Equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to <>Not equal to

6 If – Statement Condition The condition may contain variables, constants, properties, functions, mathematical operators, and logical operators The condition will always evaluate to true or false Mathematical operators have higher precedence than relational operators +, /, *, - will be performed before all relational operators Example) 5 + 3 > 3 – 1 ‘Evaluates to true

7 Logical Operators 1) Not 2) And 3) Or - Logical operators are used to combine several conditions into one compound condition - The operators above are listed in order of precedence - Not will be evaluated first, then And, then Or Example) 12 > 0 And 12<20 ‘Evaluates to True

8 Operator Precedence Because And has a higher precedence than Or, the statement below: P Or Q And R Is the same as: P Or (Q And R)

9 Truth Tables Truth Tables are used to determine the output of a complicated boolean expression The Left side of the truth table lists the possible combinations for the boolean variables If n is the number of boolean variables (or operands), there are 2^n combinations of true and false

10 Truth Tables How many possible combinations of true and false are there for 4 boolean variables?

11 Truth Tables How many possible combinations of true and false are there for 4 boolean variables? Answer: 2^4 = 16

12 UCase Function Visual Basic is Case Sensative “P” is different than “p” Ucase is used to return the uppercase equivalent of the input string Example) lbl1.Caption = UCase("Hello") ‘Sets the caption to “HELLO”

13 Comparing Strings When comparing strings, ensure that each string is set to uppercase Dim strIn As String strIn = "bye" Example) (strIn = "BYE") ‘Returns false Example) (UCase(strIn) = “BYE”) ‘Returns true

14 Nested If-Then Statements A nested selection structure is one in which either the true path or the false path includes yet another selection structure Example) If x < y Then lbl1.Caption = “1” Else If x > y Then lbl1.Caption = “2” Else lbl1.Caption = “3” End If

15 Select Case Statement The Select Case Statement can be used in place of multiple If-Then-Else Statements The Select Case Statement has the structure below: Select Case testExpression Case expression Case Else End Select

16 Select Case Statement Example) Dim intX As Integer intX = 5 Select Case intX Case 1 To 5 lbl1.Caption = "First Case" Case 6 To 10 lbl1.Caption = "Second Case" Case Else lbl1.Caption = "Else" End Select

17 Select Case Statement Example) Dim intX As Integer intX = 1 Select Case intX Case Is < 10 lbl1.Caption = "Small" Case Is < 100 lbl1.Caption = "Medium" Case Is < 1000 lbl1.Caption = "Large" End Select

18 Select Case Statement Example) txtInput.Text = "Wrangler" Select Case UCase(txtInput.Text) Case "CORVETTE" lbl1.Caption = "That's a fast car!" Case "RAM 1500" lbl1.Caption = "It's better than a Ford!" Case "WRANGLER" lbl1.Caption = "What a good beach vehicle!" Case Else lbl1.Caption = "I do not know what type of car that is." End Select

19 Option Button Control Only one option button can be selected at a time Option buttons are used in situations where you want to limit the user to only one of many possible choices Option Buttons can have a value of true or false

20 Check Box Controls Check Box controls allow the user to select many options from many choices Unlike Option Buttons, many Check Box controls can be selected at the same time

21 Check Box Controls Check Box Controls can have three values: 0, 1, or 2 The value of a CheckBox can be accessed or changed with the line: chkBox1.Value = 1

22 Integer Function The integer function returns the integer portion of a floating point number Example) Int(3.9) returns 3 Int(3.3) returns 3 Int(9.9) returns 9

23 Random Function The Random Function returns a random number from [0, 1) Including 0, not including 1 Use the random function by writing Rnd Example) lbl1.caption = Rnd

24 Random Function Before using the Random Function, the Randomize Statement must be used The Randomize statement initializes Visual Basic’s random-number generator Example) Randomize lbl1.caption = Rnd

25 Random Generator Formula To generate a random integer in a specific range, use the formula below: Int((upperbound – lowerbound + 1) * Rnd + lowerbound)

26 Random Generator Formula To generate a random integer in a specific range, use the formula below: Int((upperbound – lowerbound + 1) * Rnd + lowerbound) Example below generates number from 1 – 10) lbl1.Caption = Int((10 - 1 + 1) * Rnd + 1)

27 User-Defined Sub Procedures User-defined procedures are sections of code that can be invoked from one or more places in your program When a portion of code needs to be written more than once, it is more efficient to use a user-defined sub procedure rather than writing the same code twice Example) Private sub MakeRandom() Randomize intRand = Rnd End Sub

28 Call Statement A Call statement is used to invoke a user-defined sub procedure A Call statement has the form: Call ProcedureName Example) Call MakeRandom()

29 The Len Function The Len, or Length, Function is used to determine the number of characters in a String Example) Dim str as String str = “Hello” lbl1.Caption = Len(str) ‘lbl1.Caption has a value of 5


Download ppt "TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout."

Similar presentations


Ads by Google