Controlling Program Flow With Decision Structures

Download Report

Transcript Controlling Program Flow With Decision Structures

Controlling Program Flow
With Decision Structures
The If…Then Statement
• Decision structures to control flow
• True/False (boolean) decision structure
• If a condition is true then certain
statements will operate
• If intGuess = 7 Then
– Me.lblMessage.Text = “You guessed it”
End If
The If…Then Statement
• Many If…Then statements will use
relational operators
•
•
•
•
•
•
=
<
<=
>
>=
<>
The If…Then…Else Statement
• Else clause: evaluated when the If
condition evaluates to false.
• If intX = intSecret Then
• Me.lblMessage.Text = “You Guessed it”
Else
Me.lblMessage.Text = “Try Again.”
End If
Nested If…Then…Else Statements
• When you have an If…Then decision
structure within an If…Then decision
structure
If…Then…ElseIf Statement
• Used to decide between three or more
actions
Select…Case Statement
• Another decision structure
• Sometimes preferred over the
if…then…elseif
Select…Case Is Statement
• Compares a range of values by using
relational operators.
Random Numbers
• Rnd generates random numbers
• Equal to or greater than 0, less than 1
• (High – Low +1) * Rnd () + Low
• Eliminate everything after the decimal Int
• Randomize
Static Variables
• Retains the value of a variable throughout
program execution
• Keep the scope as narrow as possible
• Static intSecret As Integer
Algorithms
• An algorithm states the code in words
– Determine a secret number
– Get a number from the player
– Compare the number and the secret #
– If higher than…
– If lower than…
– If equal…
• Pseudocode: an algorithm with some
code
Logical Operators
• Joins two boolean expressions
– Not, And, Or (order of operation)
• If intGrade < 0 Or intGrade > 100 then
• ‘out of range
• If strItem = “book” And intquant > 10 then
• ‘discount items over 10
• If Not strItem = “book” then
• ‘done for every item except book
me.radrock.checked
Message Box
• Dialog box
• Created by using its own statement
MessageBox.Show(“display”, “title bar”)
• Place anywhere you want a messagebox
to appear
Counter Variables
• A variable that is incremented by a
constant number
• Keeps track of number of guesses, button
clicks, lives in a game
• To increment a counter it is the
counter = counter + constant
intNumTries = intNumTries + 1
Test Grade
Checkbox Control
•
•
•
•
Prefix - chk
Similar to radio buttons
More than one may be selected
.checked property
To do
Menu