Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision...
-
Upload
amos-atkins -
Category
Documents
-
view
225 -
download
0
Transcript of Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision...
![Page 1: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/1.jpg)
Chapter 3
Control Structures
![Page 2: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/3.jpg)
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
![Page 4: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/4.jpg)
Relational Operators
Relational Operators can be used to form Boolean expressions (remember Boolean is either True or False)
Operator Meaning
= Equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
<> Not equal to
![Page 5: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/5.jpg)
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
statementsElse
statementsEnd If
![Page 6: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/6.jpg)
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..
![Page 7: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/7.jpg)
Nested If…Then…Else Statement cont…
Takes the form:If condition Then
statementsElse
If condition Thenstatements
Elsestatements
End IfEnd If
![Page 8: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/8.jpg)
The If…Then…ElseIf Statement
Used to decide among three or more actions Takes the form:
If condition Thenstatements
ElseIf condition Thenstatements
…Else
statementsEnd If
![Page 9: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/9.jpg)
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.
![Page 10: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/10.jpg)
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.
![Page 11: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/11.jpg)
Select…Case Statement cont…
Select…Case takes the form:
Select expression
Case value
statements
…
Case Else
statements
End Select
![Page 12: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/12.jpg)
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
statements or 25
Case Else ‘Score other than 0,10,20,
statements 21,22,23,24 or 25
End Select
![Page 13: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/13.jpg)
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.
![Page 14: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/14.jpg)
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 < 10statements
Case Is < 25statements
Case Is >= 25statements
End Select
![Page 15: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/15.jpg)
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.
![Page 16: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/16.jpg)
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.
![Page 17: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/17.jpg)
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
![Page 18: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/18.jpg)
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.
![Page 19: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/19.jpg)
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
![Page 20: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/20.jpg)
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.
![Page 21: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/21.jpg)
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.
![Page 22: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/22.jpg)
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.
![Page 23: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/23.jpg)
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.
![Page 24: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/24.jpg)
Logical Operators cont…
Logical OperatorsAnd OrExpression1 Expression2 Result 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
NotExpression Result True False False True
![Page 25: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/25.jpg)
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.
![Page 26: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/26.jpg)
& 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
![Page 27: 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.](https://reader033.fdocuments.in/reader033/viewer/2022051116/5697bfb81a28abf838c9f18f/html5/thumbnails/27.jpg)
& cont…
Concatenation can also be used to combine two labels into one.
Ex: Me.lblMessage.text = “Your answer is “ & sngAnswer & “ inches”