Controlling Program Flow With Decision Structures
-
Upload
yael-perry -
Category
Documents
-
view
30 -
download
2
description
Transcript of Controlling Program Flow With Decision Structures
![Page 1: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/1.jpg)
Controlling Program FlowWith Decision Structures
![Page 2: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/2.jpg)
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
![Page 3: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/3.jpg)
The If…Then Statement
• Many If…Then statements will use relational operators
• =• <• < =• >• > =• < >
![Page 4: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/4.jpg)
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”
ElseMe.lblMessage.Text = “Try Again.”
End If
![Page 5: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/5.jpg)
Nested If…Then…Else Statements
• When you have an If…Then decision structure within an If…Then decision structure
![Page 6: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/6.jpg)
If…Then…ElseIf Statement
• Used to decide between three or more actions
![Page 7: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/7.jpg)
Select…Case Statement
• Another decision structure
• Sometimes preferred over the if…then…elseif
![Page 8: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/8.jpg)
Select…Case Is Statement
• Compares a range of values by using relational operators.
![Page 9: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/9.jpg)
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
![Page 10: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/10.jpg)
Static Variables
• Retains the value of a variable throughout program execution
• Keep the scope as narrow as possible
• Static intSecret As Integer
![Page 11: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/11.jpg)
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
![Page 12: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/12.jpg)
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
![Page 13: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/13.jpg)
Message Box
• Dialog box
• Created by using its own statement MessageBox.Show(“display”, “title bar”)
• Place anywhere you want a messagebox to appear
![Page 14: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/14.jpg)
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
![Page 15: Controlling Program Flow With Decision Structures](https://reader034.fdocuments.us/reader034/viewer/2022042608/568134cf550346895d9bf5ab/html5/thumbnails/15.jpg)
Checkbox Control
• Prefix - chk
• Similar to radio buttons
• More than one may be selected
• .checked property
To do
Menu