ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic...

32
ENGR 112 Decision Structures

Transcript of ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic...

Page 1: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

ENGR 112

Decision Structures

Page 2: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Control Structures Sequence structures

Built into Visual Basic

Selection structures If/Then Single selection If/Then/Else Double selection Select Case Multiple selection

Page 3: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Control Structures Repetition structures

For/Next Do/While Loop

Page 4: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Selection Structures

Page 5: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Selection Structure

Is Condition Met?

Statements in Else Clause

Statements in Then Clause

NO YES

Page 6: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

If/Then/Else Statement Allows us to execute one statement if

TRUE and another if FALSEIf A=0 Then

msg = “ERROR...Division by ZERO”MsgBox(msg)Exit Sub

Else C = B/A

End If

Page 7: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

If/Then/Else for checking To determine if user has entered a

string in the from of a dateIf IsDate(variable) Then ‘do what you want with the dateElse ‘provide a message to the userEnd If

Page 8: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Example Dim DTPrivate 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 IfEnd Sub

Page 9: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

If/Then/Else for checking To determine if variable can be

converted to a numberIf IsNumeric(variable) Then ‘do what you want with the numberElse ‘provide a message to the userEnd If

Page 10: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

ExampleDim NTPrivate Sub Command2_Click() NT = Text2.Text If IsNumeric(NT) Then MsgBox "Good Job" Else MsgBox "please enter a number" End IfEnd Sub

Page 11: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

If/Then/Else Statement Nested conditional statements

If A = 0 Thenvalue = “x”

Else If A=1 Then

value = “y” Else

If A=2 Then value = “z” End If

End If End If

Page 12: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

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

Page 13: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

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.

Page 14: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

If/Then/Else Pseudocode Version

If Grade >= 90 Then YourGrade = “A”Else If Grade >= 80 Then YourGrade = “B”Else If Grade >= 70 Thenetc

Page 15: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Case Pseudocode

Select Case Grade Case is >= 90 YourGrade = “A” Case is >= 80 YourGrade = “B” Case is >= 70 YourGrade = “C”Etc.

Page 16: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Repetition Structures

Page 17: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Determinant Loops

Page 18: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

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

Optional

Increments counter

Page 19: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Determinate StructureSet counter variable to starting value

Increment counter variable

Is counter value greater than

ending value?

Body of Loop

Move to statements after loop

NO

YES

Page 20: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

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

Page 21: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

For/Next Statement Examples

Answ = 0

For X=1 To 10Answ = Answ + X

Next X

Answ = 0

For X=1 To 10 Step 2Answ = Answ + X

If X > 10 Then Exit For

Next X

Page 22: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Indeterminate Loop

Page 23: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

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)

Page 24: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Indeterminate Structure 1

Move to statements after loop

Body of Loop

Is conditionmet?

NO

YES

Test at End of Loop

Page 25: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Do Loop – Test at End Example

Dim passwordDo password= inputBox$(“Password

please?”)Loop Until password = “Vanilla

orange”

Page 26: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Indeterminate Structure 2

Move to statements after loopBody of Loop

Is conditionmet?

NO

YES

Test at Beginning of Loop

Page 27: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Do Loop –Test at beginning exampleDim entryDim NameCount As IntegerPrivate Sub Command1_Click()NameCount = 0entry = InputBox("Enter name")Do Until entry = "zzz" NameCount = NameCount + 1 entry = InputBox("Enter name")LoopPrint "The total number of names is "; NameCountEnd Sub

Page 28: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Do While LoopDoLoop Until variablename <> “”

DoLoop While variablename = “”

DoLoop Until variablename > 5

DoLoop While variablename <= 5

Equivalent

Equivalent

Page 29: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Do...While/Loop Statement Example

X = 10

Do While X > 0Ht = (60 + 2.13) * X^4

Print Ht ‘Ht=Height of Rocket

X = X - 1

Loop

Condition

Condition being affected

Page 30: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Problems with Loops What’s the problem with this loop?

Dim i As Integer

Do While i = 0

i = 0

Loop

Page 31: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Problems with LoopsWhat’s the problem with this loop?

Private Sub CmdDisplay_Click()Dim x As Singlex = 1Do While x > 0

x = x + 1PicOutput.Print x

LoopEnd Sub

Page 32: ENGR 112 Decision Structures. Control Structures Sequence structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else.

Problems with LoopsWhat’s the problem with this loop?

Private Sub CmdDisplay_Click()Dim m As SingleFor m = 1 To 20.5 Step – 1

PicOutput.Print m

Next m

End Sub