Visual Basic 2005 Chapter 6

Visual Basic 2005 Chapter 6 Slide 1 Ch6. Using Decision Structures Visual Basic 2005 Slide 2 Agenda Review Chapter 5 If –then –else decision structur...
Author: Ashlee Adams
1 downloads 1 Views 520KB Size
Visual Basic 2005 Chapter 6 Slide 1 Ch6. Using Decision Structures Visual Basic 2005

Slide 2 Agenda Review Chapter 5 If –then –else decision structure  If-without else  Nested ifs  Lab: a trivia quiz  

Slide 3 Variables  

Dim Age As Integer Age= 10

The two statements above are equivalent to  Dim Age As Integer =10

1|Page

Visual Basic 2005 Chapter 6 Slide 4

Calculate the average (why it doesn’t work? ) Dim Test1, Test2, Test As Double Test1=TextBox1.Text Test2=TextBox1.Text Test2=TextBox1.Text Label1.Text =Test1+Test2+ Test3 /3

Slide 5

5

Slide 6 Translate into VB Dim Destination As________ Destination = InputBox(“Enter your destination:”) If Destination

= “Bismarck” then

exit 456 Else go straight ahead End if

6

2|Page

Visual Basic 2005 Chapter 6 Slide 7 If Block The program will take a course of action based on whether a condition is true. If condition Then action1 Else action2 End If

Will be executed if condition is true Will be executed if condition is false

7

Slide 8

Flow chart: A graphic representation of the logic or steps in a program or system. A flowchart represents how a program or activity moves through various processes or program routines. It uses symbols to represent the activities, and it uses arrows to represent the direction of activity through the processes. Flowcharts can be used to define the behavior of a single program or a system (a combination of programs).

Flow chart for if-else true

Condition

false

Action 1

Action 2

8

Slide 9 Example 1: Find the larger number

TextBox1 TextBox2

Label1

9

3|Page

Visual Basic 2005 Chapter 6 Slide 10 Example 1: Code of button1_click Dim num1, num2, largerNum As Double num1 = ______________________ num2 = ______________________ If num1 > num2 Then largerNum = ________ Else largerNum = ___________ End If Label1.Text = "The larger number is " +__________

10

Slide 11 Example 1: Output

11

Slide 12

An extension of the If block allows for more than two possible alternatives with the inclusion of ElseIf clauses. Note: there is no space between the word "Else" and "If" Only one "End If" is required.

ElseIf clause If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If

Chapter 5 - VB 2005 by Schneider

4|Page

12

Visual Basic 2005 Chapter 6 Slide 13 Flowchart for if-else- if expr 1

true

block 1

false

expr 2

true

block 2

false false

expr N

true

block N

false

default block 13

Slide 14

Letter grade example (complete the code) Dim LetterGrade As String;



Dim Mark As Double



Mark=Textbox1.Text



If Mark > =90 then LetterGrade= “A+” ElseIf

  

Mark>= 90 -----A+ Mark> =80 -----A Mark >= 70-----B Mark> = 60-----C Mark>=50------D Mark= 16 then Label1.Text= “You can drive.” End if Label1.Text= “You are too young to drive.”

Slide 18 What will be displayed in Lable1? Dim Age As Integer Dim Message as String Age =TextBox1.Text Message = “You are too young to drive.” If Age >= 16 then Message= “You can drive.” End if Label1.Text=Message

6|Page

Visual Basic 2005 Chapter 6 Slide 19 Nested If Blocks When one If block is contained inside another If block, the structure is referred to as nested If blocks.  Care should be taken to make If blocks easy to understand. 

19

Slide 20 Simplified Nested If Statement If cond1 Then If cond2 Then action

If cond1 And cond2 Then action End If

End If End If

Less Confusing

Nested If

20

Slide 21 Lab: a trivia quiz    

7|Page

Combo box for question selection Textbox for question display Radio buttons for options When the check button is click, a message box pops up telling whether the answer was correct or not.

Visual Basic 2005 Chapter 6 Slide 22 Double clicked the combo box If ComboBox1.Selec tedItem = "Country" Then TextBox1.Text = " Which country am I going to visit this summer?" RadioB utton1.Text = "Canada" RadioB utton2.Text = "Italy" RadioB utton3.Text = "China" RadioB utton4.Text = "France" ElseIf ComboBox1.SelectedItem = "Flower" Then TextBox1.Text = "Which of the following is my favorite flower? " RadioB utton1.Text = "Lilly" RadioB utton2.Text = "Rose" RadioB utton3.Text = "Daisy" RadioB utton4.Text = "Ginge r Flower" ElseIf ….

Slide 23 Double Click the Check Button If ComboBox1.SelectedItem = "Country" Then If RadioButton1.Checked = True Then MsgBox("You got it!") Else MsgBox("Wrong!") End If End If ‘ Improved version If ComboBox1.SelectedItem = "Country" AND RadioButton1.Checked = True Then MsgBox("You got it!") Else MsgBox("Wrong!") End If

8|Page