1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

23
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9

Transcript of 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

Page 1: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

1CC111 Lec9 : Visual Basic

Visual Basic (3)

Lecture 9

Page 2: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

2CC111 Lec9 : Visual Basic

Controlling your Application

• Sequence

• Selection– If...Then...Else statement– Select Case statement

• Repititon – For...Next Loop statement– For Each...Next statement– Do Loops

Page 3: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

3CC111 Lec9 : Visual Basic

Selection If..Then..Else

If (condition) Then (statement[s])[ElseIf (condition) Then

(statement[s])][Else (statement[s])]

Page 4: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

4CC111 Lec9 : Visual Basic

Selection Application: Convert from Numerical Grade to Letter Grade

•Input: a numeric grade between 0 and 100

•Output: depends on input

Input Output

049 F

5054 D

5559 C-

6064 C

6569 C+

7074 B-

7579 B

8084 B+

8589 A-

9094 A

95100 A+

Page 5: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

5CC111 Lec9 : Visual Basic

Selection Application Convert from Numerical Grade to Letter Grade (Form View)

Letter_Grade_Calculator.exe

Page 6: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

6CC111 Lec9 : Visual Basic

Page 7: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

7CC111 Lec9 : Visual Basic

Selection Application: Convert from Numerical Grade to Letter Grade (Code View)Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (ComboBox1.SelectedIndex = 0) Then Label2.Text = "F" End If If (ComboBox1.SelectedIndex = 1) Then Label2.Text = "D" End If If (ComboBox1.SelectedIndex = 2) Then Label2.Text = "C-" End If If (ComboBox1.SelectedIndex = 3) Then Label2.Text = "C" End If If (ComboBox1.SelectedIndex = 4) Then Label2.Text = "C+" End If If (ComboBox1.SelectedIndex = 5) Then Label2.Text = "B-" End If If (ComboBox1.SelectedIndex = 6) Then Label2.Text = "B" End If If (ComboBox1.SelectedIndex = 7) Then Label2.Text = "B+" End If If (ComboBox1.SelectedIndex = 8) Then Label2.Text = "A-" End If If (ComboBox1.SelectedIndex = 9) Then Label2.Text = "A" End If If (ComboBox1.SelectedIndex = 10) Then Label2.Text = "A+" End If

End Sub

Page 8: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

8CC111 Lec9 : Visual Basic

Selection Application Convert from Numerical Grade to Letter Grade (Run)

Page 9: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

9CC111 Lec9 : Visual Basic

For...Next Loop Structure

For counter = start To end Step incrementstatements

Next counter

WhereCounter is tested to see if less than end.

If so, repeat loop again. If not, go to statement after Next.

Page 10: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

10CC111 Lec9 : Visual Basic

Loops Application: Factorial

• Factorial (N)= N*(N-1)*(N-2)……….*(1)

Page 11: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

11CC111 Lec9 : Visual Basic

Factorial Flowchart (N>0)

Page 12: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

12CC111 Lec9 : Visual Basic

Factorial Application: Form View

Factorial.exe

Page 13: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

13CC111 Lec9 : Visual Basic

Factorial Application: Code ViewUsing For..Next Loop

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n As Integer Dim m As Integer Dim f As Integer n = Val(TextBox1.Text) f = 1 For m = 1 To n Step 1 f = f * m Next m Label2.Text = "Factorial=" + Str(f) End Sub

Page 14: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

14CC111 Lec9 : Visual Basic

Factorial Application: Run

Page 15: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

15CC111 Lec9 : Visual Basic

Do While...Loop Structure

Do While conditionstatements

Loop

WhereThe condition is tested, and if true the loop isrepeated.

When the condition is false, the statements are skipped after Loop is executed.

Page 16: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

16CC111 Lec9 : Visual Basic

Loops Application: Factorial

• Factorial (N)= N*(N-1)*(N-2)……….*(1)

Page 17: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

17CC111 Lec9 : Visual Basic

Factorial Application: Form View

Page 18: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

18CC111 Lec9 : Visual Basic

Factorial Application: Code ViewUsing Do..While..Loop

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n As Integer Dim m As Integer Dim f As Integer n = Val(TextBox1.Text) f = 1 m = 1 Do While m <= n f = f * m m = m + 1 Loop Label2.Text = "Factorial=" + Str(f) End SubEnd Class

Page 19: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

19CC111 Lec9 : Visual Basic

Do Until...Loop Structure

Do Until conditionstatements

Loop

WhereThe condition is tested, and if false the loop isrepeated.

When the condition is true, the statements are skipped after Loop is executed.

Page 20: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

20CC111 Lec9 : Visual Basic

Loops Application: Factorial

• Factorial (N)= N*(N-1)*(N-2)……….*(1)

Page 21: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

21CC111 Lec9 : Visual Basic

Factorial Application: Form View

Page 22: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

22CC111 Lec9 : Visual Basic

Factorial Application: Code ViewUsing Do..Until..Loop

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim n As Integer Dim m As Integer Dim f As Integer n = Val(TextBox1.Text) f = 1 m = 1 Do Until m > n f = f * m m = m + 1 Loop

Label2.Text = "Factorial=" + Str(f) End Sub

Page 23: 1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.

23CC111 Lec9 : Visual Basic

Factorial Application: Run