Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.

Similar presentations


Presentation on theme: "Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition."— Presentation transcript:

1 Chapter 3 Control Structures

2 The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition is True. Takes the form: If condition Then statements End If

3 If…Then cont… Condition = What the computer evaluates to be either True or False Statements = What the computer does if the condition is True Example: If intGuess = 7 Then Me.lblMessage.Text = “You guessed it!” End If

4 Relational Operators Relational Operators can be used to form Boolean expressions (remember Boolean is either True or False) OperatorMeaning =Equal to <Less than <=Less than or equal to >Greater than >=Greater than or equal to <>Not equal to

5 If…Then…Else Statement Includes an optional Else clause that is executed when the If condition evaluates to False. Takes the form: If condition Then statements Else statements End If

6 Nested If…Then…Else Statement An If…Then…Else statement can contain another If…Then…Else or If…Then statement, which is said to be nested. Nested statements should be indented for good programming style. Continued on next page..

7 Nested If…Then…Else Statement cont… Takes the form: If condition Then statements Else If condition Then statements Else statements End If

8 The If…Then…ElseIf Statement Used to decide among three or more actions Takes the form: If condition Then statements ElseIf condition Then statements … Else statements End If

9 The If…Then…ElseIf Statement cont… There can be multiple ElseIf clauses, and the last Else clause is optional. Comments are a must to keep complicated code much more readable. This is especially important for the last branch of a decision structure which usually does not include an explicit condition.

10 The Select…Case Statement The Select…Case Statement is a decision structure that uses the result of an expression to determine which block of code to execute. The Select…Case Statement is sometimes preferable to the If…Then…ElseIf statement because code may be easier to read.

11 Select…Case Statement cont… Select…Case takes the form: Select expression Case value statements … Case Else statements End Select

12 Example of Select…Case Example: Select Case intScore Case 0, 10‘Score is 0 or 10 statements Case 20 To 25‘Score is 20,21,22,23,24 statementsor 25 Case Else‘Score other than 0,10,20, statements21,22,23,24 or 25 End Select

13 Select…Case Statement cont… The Select…Case must evaluate to a built-in data type. Ex. Integer, single, … Their can be multiple Case clauses, and the Case Else clause is optional. IMPORTANT: The value type should match the expression type and can be a single value, a list separated by commas, of a range separated by the keyword To.

14 The Select…Case Is Statement Compares the result of an expression to a range of values when a relational operator is part of the value. Example: Select Case intScore Case Is < 10 statements Case Is < 25 statements Case Is >= 25 statements End Select

15 Generating Random Numbers The phrase Randomize() must be included at the beginning of the procedure that is using the random number. To generate a random number use the built in Rnd() function The formula for a random number is: (High Number – Low Number) * Rnd() + Low Number Which is set equal to some variable.

16 Message Box Can be displayed to alert the user of invalid data or as a reminder of options required for an application to continue. How to code a message box? MessageBox.Show(“message”,”title”) The “title” is optional.

17 Counter Variables A counter is a variable storing a number that is incremented by a constant value. Counters are useful for keeping track of the number of times a user clicks a button, enters a guess, or types a password. Updating a counter takes the form: counter = counter + constant

18 Counter Variables cont… Counter is the numeric variable that is updated. Constant is the number that is added to the current value of counter. A counter should be initialized when it is declared and then updated by an unchanging amount.

19 Static Variables Anytime a counter variable is declared it should be declared as a static variable instead of dimensional. A variable declared as a local variable is redeclared every time through the event procedure unless it is declared as a static. Ex: Static intCounter As Integer

20 Static Variables cont… Extends the lifetime of a local variable. The value of a static variable is not redeclared and reinitialized each time the Click event occurs. Useful when assigning random numbers to variables.

21 The CheckBox Control Name = prefix = chk Text = text displayed next to the box Checked = can be set to either True or False to display the check box with or without a check, respectively. Related check boxes are sometimes placed together in a GroupBox object. As with radio buttons, a group box should be added to the form before adding check boxes.

22 Line-Continuation Character Statements that are long can be typed onto two or more lines when the line- continuation character is used. The underscore ( _ ) is the line- continuation character. Must have a space before it and nothing after it and cannot occur within quotation marks.

23 Logical Operators A Boolean expression can be formed using the logical operators And and Or. A logical operator joins two expressions to create an expression that evaluates to either True or False. A third logical operator used is Not. Not changes an expression either from True to False or from False to True.

24 Logical Operators cont… Logical Operators And Or Expression1 Expression2 Result True True True True True True True False False True False True False True False False True True False False False False False False Not Expression Result True False False True

25 String Concatenation Two or more strings can be joined together in a process call concatenation. The & operator can be used to concatenate string. The & operator is used in an expression similar to the following: newString = string1 & string2 newString is a String variable that will store the result of the expression. string1 and string2 are String variables or string enclosed in quotation marks.

26 & cont… Example Dim strFirstName As String Dim strLastName As String Dim strFullName as String strFirstName = “Al” strLastName = “Bundy” strFullName = strFirstName & “ “ & strLastName StrFullName will = Al Bundy

27 & cont… Concatenation can also be used to combine two labels into one. Ex: Me.lblMessage.text = “Your answer is “ & sngAnswer & “ inches”


Download ppt "Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition."

Similar presentations


Ads by Google