Sei sulla pagina 1di 12

Exercise Worksheet VBA Programming

ENGR 115 Computer Applications for Engineering


Homework #4 -50 Points.
Due 3/26/09
Name_________________
The following exercises are meant to reinforce your understanding of basic programming
structures, and programming syntax in VBA. You may print this sheet and write answers
by hand on a hardcopy of this sheet or type programming statements directly into this
document. For more benefit, you are encouraged to check your work by entering these
statements into the VBA editor and executing them.

Use the following pages for reference in completing these problems:


 Strings, Math Functions in VB .NET
 Conditional Statements in VB .NET
 Loops in VB .NET - For Loop, While Loop, Do Loop

Conditionals
For exercises 1 to 27, indicate the message box that will be produced. If no message box
is displayed, write “No Message” and if multiple message boxes will be displayed,
indicate them in order. For example, if a message box displaying “apple” would be
followed by a message box displaying “orange” write:
“apple”
“orange”

Const MAXIMUM as Integer = 25, LIMIT As Integer = 100


Const num1 as Integer = 12, num2 as Integer = 25
Const num3 as Integer = 87

1. If num1 < MAXIMUM Then


MsgBox “apple”
End If

2. if num2 <= MAXIMUM Then


MsgBox “apple”
End If
MsgBox “orange”

3. If MAXIMUM > num3 Then


MsgBox “apple”
End If
MsgBox “orange”
Exercise Worksheet VBA Programming

4. If num3 >= LIMIT Then


MsgBox “apple”
MsgBox “orange”
End If
MsgBox “pear”

5. If num2 = MAXIMUM Then


MsgBox “apple”
MsgBox “orange”
End If
MsgBox “pear”

6. If num3-num2 > 2*MAXIMUM Then


MsgBox “apple”
Else
MsgBox “orange”
End If

7. If LIMIT+num3 <= 150 Then


MsgBox “apple”
MsgBox “orange”
Else
MsgBox “pear”
End If

8. If 2*num1 <> num2 Then


MsgBox “apple”
Else

MsgBox “orange”
MsgBox “pear”
End If

9. If num1 + 4 = num1 + MAXIMUM-num2 Then

MsgBox “apple”
MsgBox “orange”
Else
MsgBox “pear”
MsgBox “banana”
End If

10. If num1 < MAXIMUM Then


If LIMIT >= num2 Then
Exercise Worksheet VBA Programming

MsgBox “apple”
End If
MsgBox “orange”
End If

11. If LIMIT <= LIMIT Then


If num3 = num1 Then
MsgBox “apple”
MsgBox “orange”
End If
End If

12. If num2 > 18 Then


If num1 < 0 Then
MsgBox “apple”
Else
MsgBox “orange”
End If
MsgBox “pear”
End If

13. If LIMIT >= 4*num2 Then


If MAXIMUM = 25 Then
MsgBox “apple”
Else
MsgBox “orange”
End If
Else
MsgBox “pear”
End If

14. If num2 < num1 Then


If num3 < LIMIT Then
MsgBox “apple”
Else
MsgBox “orange”
End If
End If
MsgBox “pear”

15. If num3 = 87 Then


If num2 <> MAXIMUM Then
MsgBox “apple”
End If
Else
MsgBox “orange”
Exercise Worksheet VBA Programming

MsgBox “pear”
End If

16. If num1+num2 > num3 Then


MsgBox “apple”
Else
If num2*LIMIT <> 3298 Then
MsgBox “orange”
End If
End If

17. If num3 = 3 Then


MsgBox “apple”
Else
If num2 = MAXIMUM Then
MsgBox “orange”
Else
MsgBox “pear”
End If
End If

18. If num3 >= MAXIMUM Then


If MAXIMUM/num2 = 1 Then
MsgBox “apple”
MsgBox “orange”
If LIMIT-num3 > num1+2 Then
MsgBox “pear”
Else

MsgBox “banana”
MsgBox “kiwi”
End If
End If
Else
If num2*2 = MAXIMUM*2 Then
MsgBox “grapefruit”
Else
MsgBox “lime”
MsgBox “coconut”
End If
End If

19. If num2 > num1 And LIMIT <> 100 Then


MsgBox “apple”
End If
MsgBox “orange”
Exercise Worksheet VBA Programming

20. If num3 = num2 And MAXIMUM > 50 Then


MsgBox “apple”
MsgBox “orange”
End If

21. If num1 > 7 And LIMIT <= 100 Then


MsgBox “apple”
End If
MsgBox “orange”

22. If num3 < 40 Or num3 > 50 Then


MsgBox “apple”
End If
MsgBox “orange”

23. If MAXIMUM = LIMIT Or num1*2 = num2 Then


MsgBox “apple”
MsgBox “orange”
End If

24. If num2 <> 0 Or num3 > LIMIT Then


MsgBox “apple”
MsgBox “orange”
End If

25. If MAXIMUM = 25 And num2 <> MAXIMUM Or num1 < num3 Then
MsgBox “apple”
End If
MsgBox “orange”

26. If num3 = 87 Or num2 > num1 And MAXIMUM > LIMIT Then
MsgBox “apple”
End If
MsgBox “orange”

27. If (num3 = 87 Or num2 > num1) And MAXIMUM > LIMIT Then
MsgBox “apple”
MsgBox “orange”
End If

Flowcharting
Exercise Worksheet VBA Programming

In the spaces below, construct a flow chart to represent the control flow of problems 18
and 25 above. You may hand-draw on this sheet or use the Flowcharting shapes of
Microsoft Word.
Problem 18

Problem 25
Exercise Worksheet VBA Programming

For exercises 28 to 34, write code segments that will perform the specified action.
Assume that all variables have already been declared and given values.

28. Display a message box that says ‘Hurrah!’ if sum divided by count is
zero.

29. Increment the integer variable total if total is zero and decrement total
otherwise.

30. Display a message box that says ‘num is zero’, ‘num is negative’, or
‘num is positive’ as appropriate based on the current value of num.
Exercise Worksheet VBA Programming

31. Display a message box that says ‘Victory’ only if result is greater than or
equal to 500 and penalty is equal to zero (use nested ifs).

32. Display a message box that says ‘Victory’ only if result is greater than or
equal to 500 and penalty is equal to zero (use logical operators).

33. Assign the smallest of two integer values num1 and num2 to the variable
smallest. (use an if-else statement)

34. Assign the smallest of three integer values num1, num2, and num3 to the
variable smallest.

Loops
For exercises 35 to 49, indicate the message box that will be produced. If no message
box is displayed, write “No Message” and if multiple message boxes will be displayed,
indicate them in order. For example, if a message box displaying “apple” would be
followed by a message box displaying “orange” write:
“apple”
“orange”
Assume the following declarations are in effect at the beginning of each problem: Refer
to Loops in VB .NET - For Loop, While Loop, Do Loop for assistance.

Const MIN As Integer = 10, MAXIMUM as Integer = 20


Dim num as Integer
Num = 15
35. Do
If num > MAXIMUM Then Exit Do
MsgBox num
num = num + 1
Loop
Exercise Worksheet VBA Programming

36. Do
If num < MAXIMUM Then Exit Do
num = num + 1
MsgBox num
Loop
37. Do
If num >= MAXIMUM Then Exit Do
num = num + 1
MsgBox num
Loop

38. Do
If num < MIN Then Exit Do
MsgBox num
num = num - 1
Loop
39. Do
If num > MIN Then Exit Do
MsgBox num
num = num - 1
Loop

40. Do
If num > MAXIMUM Then Exit Do
MsgBox num
num = num + 2
Loop
41. Do
If num > MAXIMUM + MIN Then Exit Do
If num - MIN > MIN Then
MsgBox num
End If
Num = num + 1
Loop
42. Do
If num >= MAXIMUM Then Exit Do
num = num + 1
if num*3 > 2*MAXIMUM+num Then
MsgBox num
End If
Loop
43. For value=0 To 7 Step 1
MsgBox value
Next value

44. . For value=7 To 1 Step -1


MsgBox value
Exercise Worksheet VBA Programming

Next value
45. . For value=1 To 20 Step 4
MsgBox value
Next value
46. . For value=num To MAXIMUM
MsgBox value
Next value
47. . For value=num To MAXIMUM
If value/4 > 4 Then
MsgBox value
End If
Next value
48. .
Dim outstring As String
outstring = “”

For count1=1 To 3
For count2=1 To 3
Outstring = outstring & “#”
Next count2
MsgBox outstring
Next count1
49. .
For count1=1 To 3
For count2=1 To 3
MsgBox count1*count2

Next count2
Next count1

For exercises 50 to 51, write code segments that will perform the specified action.

50. Display in message box the multiples of 5 from 5 to100

51. Display in message box the multiples of 3 from 300 down to 3


Exercise Worksheet VBA Programming

Flowcharting
In the spaces below, construct a flow chart to represent the control flow of problems 41
and 48 above. You may hand-draw on this sheet or use the Flowcharting shapes of
Microsoft Word.
Problem 41
Exercise Worksheet VBA Programming

Problem 48

Potrebbero piacerti anche