Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Answers to Self-Review Exercises

4.1

  1. 2.
  2. 3.
  3. 1.
  4. 3.
  5. 3.
  6. 2.
  7. 4.
  8. 3.
  9. 2.
  10. 1.
  11. 4.
  12. 4.
  13. 2.
  14. 2.
  15. 3.
  16. 2.
  17. 2.
  18. 3.
  19. 1.

4.2

  1. False. Pseudocode normally converts easily into Visual Basic code.
  2. True.
  3. True.
  4. True.
  5. False. It's terminated with the keyword Loop.

4.3

number = number + 1 number += 1

4.4

  1. Dim sum As Integer = 0
    Dim count As Integer = 1
    
                   While count <= 15
       sum += count
       count += 2
    
                   End While
                
  2.                Dim sum As Integer = 0
                   Dim count As Integer = 2
    
    
                   Do While count <= 15
       sum += count ^ 2
       count += 2
    
                   Loop
                
  3.                Dim counterIndex As Integer = 5
    
                   Do Until counterIndex < 1
       resultListBox.Items.Add(counterIndex)
       counterIndex -= 1
    
                   Loop
                
  4.                Dim counterIndex As Integer = 5
    
                   Do While counterIndex >= 1
       resultListBox.Items.Add(counterIndex)
       counterIndex -= 1
    
                   Loop
                

4.5

  1.                Dim sum As Integer
    
                   Dim number As Integer
                
  2. number = 1
  3. sum = 0
  4. sum += number or sum = sum + number
  5. resultLabel.Text = "The sum is: " & sum

4.6

See the code below:

 
1   ' Ex. 4.6: CalculateSum.vb
2   ' Calculates the sum of the integers from 1 to 10.
3   Public Class CalculateSum
4      ' performs the calculation
5     Private Sub CalculateSum_Load(ByVal sender As System.Object,
6         ByVal e As System.EventArgs) Handles MyBase.Load
7

8         Dim sum As Integer

9         Dim number As Integer
10
11         sum = 0
12         number = 1
13
14         Do While number <= 10
15            sum += number
16            number += 1
17         Loop
18
19         resultLabel.Text = "The sum is: " & sum
20      End Sub ' CalculateSum_Load
21   End Class ' CalculateSum

4.7

  1. Error: Repetition condition never becomes false, resulting in an infinite loop.
    While value >= 0
       sum += value
       value -= 1
    
    End While
                
  2. Errors: The counter is never incremented, resulting in an infinite loop. Therepetition condition uses the wrong relational operator. Keywords End While are used instead of keyword Loop.
                   Dim number As Integer = 1
    
    
                   Do While number <= 10
       resultListBox.Items.Add(number ^ 2)
       number += 1
    
                   Loop
                
  3. Error: The values are never displayed and are decremented instead of incremented.
    value = 888
    
    
                   While value <= 1000
       resultListBox.Items.Add(value)
       value += 1
    
                   End While
                

4.8

  1. False. Pseudocode is not a programming language.
  2. False. The loop condition must evaluate to true for the body to be executed.
  3. False. The loop condition must evaluate to true for the body to be executed.
  4. True.
  • + Share This
  • 🔖 Save To Your Account