Homework/Project #1 : Calculator
Due: 03/02
Homework #1 Calculator
Use Message boxes to inform user of eventual problems. For example:
Division by 0 forbidden!
Set control variable to initial value Is control variable > terminating value?
No
Execute statements within loop Increment control variable (automatic)
Figure 6-4: Pseudocode and flowchart for the first example shown in Figure 6-2
***************
8
10
1 3 5 7 9 11 13 15 - 17 - 19
11
Next IntCounter
12
Do Until (condition)
14
Loop statements
No
Loop
15
Yes
16
If there are several statements that need to be repeated, you need to decide when to stop repeating them. You can decide to stop them:
The Do Until iterations means that the loop statement will be executed at least once.
18
20
Exercise
Write a program to display all the numbers between 1 and 1000 that are perfect squares. (A perfect square is an integer that is the square of another integer; 1,4,9,16, )
Use: #1 Do While #2 Do Until #3 For Next
21
Exercise Solution
Dim intX, intY as integer intX=1 IntY = IntX * IntX lblDisp.text= "The perfect squares between 1 and 1000 are"
Do While IntY <= 1000 lblDisp.text= lblDisp.text & conver.toString(IntY) & , IntX = IntX + 1 IntY = IntX * IntX Loop
Do
lblDisp.text =lblDisp.text & conver.toString(IntY) & , IntX = IntX + 1 IntY = IntX * IntX Loop Until IntY >= 1000
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961
22
Exercise Solution
lblDisp.text= "The perfect squares between 1 and 1000 are"
For intX=1 to 100 (Could have used another end value) intY = intX * intX If intY > 1000 Then Exit For Else lblDisp.text=convert.toString(IntY) End If Next IntX
23
Exercise #7
Exercise #7
24
Lab Exercises
#8 Display a row of 50 stars (asterisks).
#9 Request a number from 1 to 20 (using an input box) and display a row of that many stars. If number >20 or <1 display a warning message (message box) and request the number again.
25
Lab Exercise
#10 Find the sum 1 + + 1/3 + + + 1/100 Solution: 5.187378 #11 You are offered two salary options for ten days of work. Option 1: $100 per day
26
Lab Exercise
Option2: $1 the first day, $2 the second day, $4 the third day, and so on, with the amount of doubling each day. Write a program to determine which option pays better.
Solution: Option1: $1000 Option 2: $1023
27
30
31
32
33
34
Default stepvalue is 1
35
Figure 6-4: Pseudocode and flowchart for the first example shown in Figure 6-2
Programming with Microsoft Visual Basic .NET, Second Edition
36
38
39
40
41
Figure 6-9: Flowcharts for the examples shown in Figure 6-7 (continued)
Programming with Microsoft Visual Basic .NET, Second Edition
42
43
44
45
46
47
48
270300
240269 210239 180209 below 180
A
B C D F
49
50
51
Figure 6-20: Sample run of the application that contains the procedure
Programming with Microsoft Visual Basic .NET, Second Edition
52
53
Shoppers Haven
The manager of Shoppers Haven wants an application that the store clerks can use to calculate the discounted price of an item, using discount rates from 10% through 30% in increments of 5%
The clerks will enter the items original price The application should display the discount rates and the discounted prices in the interface
54
55
56
Figure 6-23: Pseudocode for the Calculate buttons Click event procedure
Programming with Microsoft Visual Basic .NET, Second Edition
57
58
textbox is the name of the text box whose text you want to select
59
60
61
62
63
64
65
Summary
Repetition structure (loop): the computer repeats a set of instructions until some condition is met
Code a repetition structure in Visual Basic .NET using one of the following statements: For...Next, Do...Loop, and For EachNext The For...Next statement is pretest loops only
66
Summary (continued)
To use a counter or accumulator:
Initialize, if necessary Update using an assignment statement in a repetition structure
To nest a repetition structure, place the entire inner loop within the outer loop
67
Summary (continued)
To process code when the user tabs to a control, or when the Focus method is used in code to send the focus to the control, enter the code in the controls Enter event procedure
To process code when a form is about to be closed, enter the code in the forms Closing event procedure
68