controlling program flow with decision structures

15
Controlling Program Flow With Decision Structures

Upload: yael-perry

Post on 02-Jan-2016

36 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Controlling Program Flow With Decision Structures

Controlling Program FlowWith Decision Structures

Page 2: 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

Page 3: Controlling Program Flow With Decision Structures

The If…Then Statement

• Many If…Then statements will use relational operators

• =• <• < =• >• > =• < >

Page 4: Controlling Program Flow With Decision Structures

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

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

If…Then…ElseIf Statement

• Used to decide between three or more actions

Page 7: Controlling Program Flow With Decision Structures

Select…Case Statement

• Another decision structure

• Sometimes preferred over the if…then…elseif

Page 8: Controlling Program Flow With Decision Structures

Select…Case Is Statement

• Compares a range of values by using relational operators.

Page 9: Controlling Program Flow With Decision Structures

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

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

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

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

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

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

Checkbox Control

• Prefix - chk

• Similar to radio buttons

• More than one may be selected

• .checked property

To do

Menu