engr 112
Download
Skip this Video
Download Presentation
ENGR 112

Loading in 2 Seconds...

play fullscreen
1 / 32

ENGR 112 - PowerPoint PPT Presentation


  • 209 Views
  • Uploaded on

ENGR 112. Decision Structures. Control Structures. Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else  Double selection Select Case  Multiple selection. Control Structures. Repetition structures For/Next Do/While Loop.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'ENGR 112' - walda


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
engr 112

ENGR 112

Decision Structures

control structures
Control Structures
  • Sequence structures
    • Built into Visual Basic
  • Selection structures
    • If/Then  Single selection
    • If/Then/Else  Double selection
    • Select Case  Multiple selection
control structures1
Control Structures
  • Repetition structures
    • For/Next
    • Do/While Loop
selection structure
Is Condition Met?

Statements in Else Clause

Statements in Then Clause

Selection Structure

NO

YES

if then else statement
If/Then/Else Statement
  • Allows us to execute one statement if TRUE and another if FALSE

If A=0 Then

msg = “ERROR...Division by ZERO”

MsgBox(msg)

Exit Sub

Else

C = B/A

End If

if then else for checking
If/Then/Else for checking
  • To determine if user has entered a string in the from of a date

If IsDate(variable) Then

‘do what you want with the date

Else

‘provide a message to the user

End If

example
Example

Dim DT

Private Sub Command1_Click()

DT = Text1.Text

If IsDate(DT) Then

MsgBox "Good Job"

Else

MsgBox "please enter the text in the form of a date"

End If

End Sub

if then else for checking1
If/Then/Else for checking
  • To determine if variable can be converted to a number

If IsNumeric(variable) Then

‘do what you want with the number

Else

‘provide a message to the user

End If

example1
Example

Dim NT

Private Sub Command2_Click()

NT = Text2.Text

If IsNumeric(NT) Then

MsgBox "Good Job"

Else

MsgBox "please enter a number"

End If

End Sub

if then else statement1
If/Then/Else Statement
  • Nested conditional statements

If A = 0 Then

value = “x”

Else

If A=1 Then

value = “y”

Else

If A=2 Then

value = “z”

End If

End If

End If

select case structure
Select Case structure
  • Practical when more than three levels of if/Then/Else are necessary

Select Case inputNumber

Case 0

value = “x”

Case 1

value = “y”

Case 2

value = “z”

Case 3

value = “Null”

End Select

example2
Example
  • Design a program to compute grades based on the average of 4 exams
  • If the average is 90 or higher, the student gets an “A”
  • If the average is 80 – 89, the student gets a “B”
  • Etc.
if then else pseudocode version
If/Then/Else Pseudocode Version

If Grade >= 90 Then

YourGrade = “A”

Else If Grade >= 80 Then

YourGrade = “B”

Else If Grade >= 70 Then

etc

case pseudocode
Case Pseudocode

Select Case Grade

Case is >= 90

YourGrade = “A”

Case is >= 80

YourGrade = “B”

Case is >= 70

YourGrade = “C”

Etc.

for next statement
Optional

Increments counter

For/Next Statement
  • Also known as a For … Next loop
  • Allows repetition of statements
  • Syntax

For CounterVar = StartNum to EndNum [Step StepNum]

VB Statements

Next CounterVar

determinate structure
Set counter variable to starting value

Increment counter variable

Body of Loop

Move to statements after loop

Is counter value greater than ending value?

Determinate Structure

YES

NO

for next statement1
For/Next Statement
  • Counter increments by 1 unless otherwise specified
  • VB keyword Step is used to increment by a number other than 1 (including negative numbers)
  • Can be terminated prematurely with keywords Exit For
for next statement2
For/Next Statement
  • Examples

Answ = 0

For X=1 To 10

Answ = Answ + X

Next X

Answ = 0

For X=1 To 10 Step 2

Answ = Answ + X

If X > 10 Then Exit For

Next X

E

E

do loop statements
Do…Loop Statements
  • Conditional loops are executed as long or until a condition exists
  • Their key feature is the condition
  • The condition can be
    • A Boolean variable (True or False)
    • Value of a property
    • Expression (NumVal < 15)
indeterminate structure 1
Move to statements after loop

Body of Loop

Is conditionmet?

Indeterminate Structure 1

Test at End of Loop

YES

NO

do loop test at end example
Do Loop – Test at End Example

Dim password

Do

password= inputBox$(“Password please?”)

Loop Until password = “Vanilla orange”

indeterminate structure 2
Move to statements after loop

Body of Loop

Is conditionmet?

Indeterminate Structure 2

Test at Beginning of Loop

YES

NO

do loop test at beginning example
Do Loop –Test at beginning example

Dim entry

Dim NameCount As Integer

Private Sub Command1_Click()

NameCount = 0

entry = InputBox("Enter name")

Do Until entry = "zzz"

NameCount = NameCount + 1

entry = InputBox("Enter name")

Loop

Print "The total number of names is "; NameCount

End Sub

do while loop
Equivalent

Equivalent

Do While Loop

Do

Loop Until variablename <> “”

Do

Loop While variablename = “”

Do

Loop Until variablename > 5

Do

Loop While variablename <= 5

do while loop statement
Condition

Condition being

affected

Do...While/Loop Statement
  • Example

X = 10

Do While X > 0

Ht = (60 + 2.13) * X^4

Print Ht ‘Ht=Height of Rocket

X = X - 1

Loop

problems with loops
Problems with Loops
  • What’s the problem with this loop?

Dim i As Integer

Do While i = 0

i = 0

Loop

problems with loops1
Problems with Loops
  • What’s the problem with this loop?

Private Sub CmdDisplay_Click()

Dim x As Single

x = 1

Do While x > 0

x = x + 1

PicOutput.Print x

Loop

End Sub

problems with loops2
Problems with Loops
  • What’s the problem with this loop?

Private Sub CmdDisplay_Click()

Dim m As Single

For m = 1 To 20.5 Step – 1

PicOutput.Print m

Next m

End Sub

ad