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

Post on 31-Dec-2015

230 views 4 download

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

1CC111 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

3CC111 Lec9 : Visual Basic

Selection If..Then..Else

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

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

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+

5CC111 Lec9 : Visual Basic

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

Letter_Grade_Calculator.exe

6CC111 Lec9 : Visual Basic

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

8CC111 Lec9 : Visual Basic

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

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.

10CC111 Lec9 : Visual Basic

Loops Application: Factorial

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

11CC111 Lec9 : Visual Basic

Factorial Flowchart (N>0)

12CC111 Lec9 : Visual Basic

Factorial Application: Form View

Factorial.exe

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

14CC111 Lec9 : Visual Basic

Factorial Application: Run

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.

16CC111 Lec9 : Visual Basic

Loops Application: Factorial

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

17CC111 Lec9 : Visual Basic

Factorial Application: Form View

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

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.

20CC111 Lec9 : Visual Basic

Loops Application: Factorial

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

21CC111 Lec9 : Visual Basic

Factorial Application: Form View

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

23CC111 Lec9 : Visual Basic

Factorial Application: Run