Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.

Similar presentations


Presentation on theme: "1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection."— Presentation transcript:

1

2 1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection

3 2 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you will be able to : 1. Compare types of relational operators 2. Write programs using If..Else Construct 3. Use the Truth Table 4. Write programs using Select Case Construct 5. Use Nested If Statements

4 3 VB-04-Control Structures 16 March 2016 Key Words 1. If …Then..Else 2. AND 3. OR 4. NOT 5. Select Case

5 4 VB-04-Control Structures 16 March 2016 Relational Operators > greater than <less than <=less than equal >=bigger than or equal =equal to <>not equal to

6 5 VB-04-Control Structures 16 March 2016 IF Blocks If grade >= 60 then if a > b then MsgBox “Credit” MsgBox “a > b” Else else MsgBox “Pass” MsgBox “b > a” End If end if If a > b then if a b then MsgBox “a is larger” MsgBox “a not equal to b”end if

7 6 VB-04-Control Structures 16 March 2016 Group Exercise Write a program to accept two numbers and determine the largest number Use two text boxes to accept the numbers and a label to display the answer e.g “6 is larger than 5”

8 7 VB-04-Control Structures 16 March 2016 Truth Table

9 8 VB-04-Control Structures 16 March 2016 Truth Table - Examples Assume a = 2 and b = 3 Condition Answer (a < b) or (b < a) true (a < b) and (b < a) false Not (b < a) true

10 9 VB-04-Control Structures 16 March 2016 Suppose n = 4 and answ = “Y” Determine whether the following conditions are true or false 1. ((n > 2 ) And ( n < 6 )) Or (answ = “Y”) 2. ((2 < n) And (n = 5 + 1)) Or (answ = “No”) 3. Not (4 < 6) 4. (n = 2) And ((n = 7) Or (answ = “Y”)) 5. Not (answ = “y”) AND OR and NOT Operator

11 10 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program using the constructs that you have learned to determine a students grade based on the following grading scale. n Accept the mark using an InputBox ScaleGrade 75 - 100 A 60 - 74 B 50 - 59 C 40 - 49 D 0 - 39 F

12 11 VB-04-Control Structures 16 March 2016 Solution If grade >=75 and grade <= 100 then MsgBox “A” else if grade >= 60 and grade <= 64 then MsgBox “B” else if grade >=50 and grade <= 59 then MsgBox “C” else if grade >=40 and grade <= 49 then MsgBox “D” else MsgBox “F” end if

13 12 VB-04-Control Structures 16 March 2016 Using If…. Else If If condition1 then action1 Else If condition2 then action2 Else If condition3 then action3 Else action4 End if

14 13 VB-04-Control Structures 16 March 2016 Example grade = val(text1.text) If grade >=75 and grade <= 100 then MsgBox “A” elseif grade >= 60 and grade <= 64 then MsgBox “B” elseif grade >=50 and grade <= 59 then MsgBox “C” elseif grade >=40 and grade <= 49 then MsgBox “D” else MsgBox “F” end if

15 14 VB-04-Control Structures 16 March 2016 Select Case Blocks Select Case selector Case valuelist1 action1 Case valuelist2 action2 Case Else action of last resort End Select

16 15 VB-04-Control Structures 16 March 2016 Select Case - using To and IS number = val(txtinput.text) Select case number Case 1 to 4 MsgBox “Admin” Case 5 to 7 MsgBox “Accounts” Case 8,9 MsgBox “Staff Room” Case 10 MsgBox “Computer Labs” Case is < 1 MsgBox “Error” Case else MsgBox “Invalid Number” End select

17 16 VB-04-Control Structures 16 March 2016 Write a program using the select case construct to determine a traffic message based on a color code. Accept a colour code from a text box and display the following message : ColourMessage Red “STOP” Orange “GET READY” Green “GO” other color “Invalid” Group Exercise

18 17 VB-04-Control Structures 16 March 2016 Nested IF - Example If b < 5 Then If a > 2 Then MsgBox “Red" Else MsgBox “Blue" End If Else If a > 2 Then MsgBox “Green" Else MsgBox “Yellow" End If

19 18 VB-04-Control Structures 16 March 2016 Home Work n Write a program using Select Case to determine whether a person is a teenager or an adult. Use a Textbox for input. n Age 13 – 19 Teenager n Age > 19 Adult

20 19 VB-04-Control Structures 16 March 2016 Homework Develop an algorithm to accept a customer’s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following: 0 tax exempt 1 state sales tax only (3%) 2 federal and state sales tax (5%) 3 special sales tax (7%) The program must then compute the sales tax and the total amount due and display the customer’s name, purchase amount, sales tax and the total amount due.

21 20 VB-04-Control Structures 16 March 2016 Homework n Develop a program that will determine the gross pay for an employee. The company pays “straight-time” for the first 40 hours worked by each employee and pays “time and a half” for all hours worked in excess of 40 hours. n You program should accept the number of hours each employee worked and the hourly rate and display the employee’s gross pay. Use Textboxes for input and a Label to display your answer.

22 21 VB-04-Control Structures 16 March 2016 Control Structures Repetition

23 22 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you should be able to : 1. Write a While..Wend iteration 2. Write a Do..While iteration 3. Write a Do..Until Loop 4. Use a For.. Next iterative construct 5. Write a Nested For Loop

24 23 VB-04-Control Structures 16 March 2016 Key words used in this lecture 1. While..Wend 2. Do..While/Loop 3. Do..Until/Loop 4. For..Next 5. ListBox 6. ComboBox

25 24 VB-04-Control Structures 16 March 2016 Comparing Do While and Do Until n While … Wend and Do.. While loop will execute statements in the loop when the condition is True n Do until loop will execute statements in the loop when the condition is False

26 25 VB-04-Control Structures 16 March 2016 While/Wend While condition is True execute statement(s) Wend

27 26 VB-04-Control Structures 16 March 2016 Flowchart – While/Wend Ctr <=10 ctr = ctr + 1 True False

28 27 VB-04-Control Structures 16 March 2016 Display numbers from 1 to 10 n = 1 While n <=10 List1.Additem n n = n + 1 Wend While/Wend

29 28 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program and draw a flowchart to display the sum of digits from 1 to 10 i.e 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 n Show your answer using the While..Wend Loop n Use a MsgBox to display the Sum

30 29 VB-04-Control Structures 16 March 2016 While/Wend Repetition Solution Dim ctr, sum as integer ctr = 1 While ctr <=10 sum = sum + ctr ctr = ctr + 1 Wend MsgBox “The sum is” & sum

31 30 VB-04-Control Structures 16 March 2016 Do While/Loop Do While condition is True execute statement(s) Loop

32 31 VB-04-Control Structures 16 March 2016 Flowchart – Do While/Loop Ctr <=10 ctr = ctr + 1 True False Ctr = 1

33 32 VB-04-Control Structures 16 March 2016 Do While n = 1 Do while n <=10 list1.additem n n = n + 1 Loop

34 33 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program that will accept 15 Fahrenheit temperatures using an InputBox and convert it to Celsius. n Display all the converted temperature in Celsius using a list box. Formula : FtoC = (fahrenheit - 32) * 5/9 n Use a Do While Loop

35 34 VB-04-Control Structures 16 March 2016 Control Structures - Repetition

36 35 VB-04-Control Structures 16 March 2016 Learning Outcomes At the end of this lecture you should be able to : 1. Write a Do..Until Loop 2. Use a For..Next Loop 3. Write a Nested For..Next Loop

37 36 VB-04-Control Structures 16 March 2016 Keywords 1. Do..Until/Loop 2. For..Next 3. ListBox 4. ComboBox

38 37 VB-04-Control Structures 16 March 2016 Do Until/Loop Do Until condition is False execute statement(s) Loop

39 38 VB-04-Control Structures 16 March 2016 Flowchart – Do Until/Loop ctr > 10 ctr = ctr + 1 False True ctr = 1

40 39 VB-04-Control Structures 16 March 2016 Do Until/Loop Dim ctr, sum as Integer ctr = 1 Do until ctr > 10 combo1.additem ctr ctr = ctr + 1 Loop

41 40 VB-04-Control Structures 16 March 2016 Exercise n Write a program to display the following on a list box using a Do…Until Loop 1 2 4 3 9 4 16 5 25

42 41 VB-04-Control Structures 16 March 2016 Answer Private Sub Command1_Click() ctr = 1 Do Until ctr > 5 List1.AddItem ctr & " " & ctr ^ 2 ctr = ctr + 1 Loop End Sub

43 42 VB-04-Control Structures 16 March 2016 Group Exercise n Write a program to determine the Factorial of N Example N = 5 5! = 5 * 4 * 3 * 2 * 1 = 120 Use a Do Until Loop

44 43 VB-04-Control Structures 16 March 2016 Solution Private Sub Command1_Click() Dim n, x, f As Integer n = Val(Text1.Text) x = n f = 1 Do Until x < 1 f = f * x x = x - 1 Loop MsgBox "Factorial of " & n & "is" & f End Sub

45 44 VB-04-Control Structures 16 March 2016 For…Next Loops n Is used when we know how many times a loop should be executed. Format For x = initialvalue to endvalue Step increment execute statement (s) Next x Example For x = m to n step 1 statement(s) Next x

46 45 VB-04-Control Structures 16 March 2016 For…Next Loops ‘Display numbers from 1 to 10 in a ComboBox Private sub cmdbutton_click() Dim n as integer For n = 1 to 10 step 1 Combo1.Additem n Next n End Sub

47 46 VB-04-Control Structures 16 March 2016 Group Exercise Use for.. next loop to determine the following 1. Find the sum of digits from 1 to 10 2. Display the following number sequence a.2 4 6 8 10 b.1 3 5 7 9 c.10 8 6 4 2 0

48 47 VB-04-Control Structures 16 March 2016 Nested For…Next n Two For..Next statements which are running together n Consist of an Outer Loop and an Inner Loop Private Sub Command1_Click() List1.Clear For i = 1 To 5 For j = 1 To 3 pro = i * j List1.AddItem i & " * " & j & " = " & pro Next j Print ‘Leave a New Line Next i End Sub Outer Loop Inner Loop

49 48 VB-04-Control Structures 16 March 2016 Do/Loop While n The Do Loop While structure tests the loop condition after the loop body is performed n It executes the loop at least once

50 49 VB-04-Control Structures 16 March 2016 Flowchart – Do/Loop While action condition False True

51 50 VB-04-Control Structures 16 March 2016 Do/Loop While Dim ctr as integer Ctr = 1 Do MsgBox ctr ctr = ctr + 1 Loop while ctr <=10

52 51 VB-04-Control Structures 16 March 2016 Summary n Do While Wend execute statements when condition is true n Do Until Loop executes statements when condition is false n Do Loop While executes a loop at least once

53 52 VB-04-Control Structures 16 March 2016 Homework Print out the the following sequence in a list box 1 1 2 3 5 8 Print the following stars pattern in a list box ***** **** *** ** *


Download ppt "1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection."

Similar presentations


Ads by Google