Write the pseudocode for a program that uses a single while loop to print the numbers from 20 to 25 and from 40 to 45. your loop will have at least one nested "if" statement that will determine when the numbers should be printed.

If X >= 20 Then
If X <= 25 Then Print X
End If
If X>=40 Then
If X <=45 Then Print X
End If

Dani AI

Generated

A few clarifications and small fixes so both exercises behave correctly and meet the “single while loop + at least one nested IF” requirement.

For the 20..25 and 40..45 printer: start the loop at 20 (avoid unnecessary iterations), make sure the loop variable is explicitly initialized and incremented, and use a nested IF to satisfy the assignment. Example pseudocode (different structure from earlier posts):

X = 20
WHILE X <= 45
    IF X <= 25 THEN
        PRINT X          ' prints 20..25
    ELSE
        IF X >= 40 THEN
            PRINT X      ' prints 40..45
        END IF
    END IF
    X = X + 1
END WHILE

This fulfills the nested-IF requirement while keeping the loop simple. ’s compound condition is more concise in practice, but it isn’t nested — use it only if the assignment doesn’t insist on nesting.

For the inventory/tagging pseudocode: the common mistakes in your version () were missing initializations and replacing the running total instead of accumulating it. A corrected pattern:

tagCount = 0
totalRetail = 0.0
ReadFirstRecord()
WHILE NOT EOF
    PRINT ItemNumber, Description, QuantityOnHand, RetailPrice
    tagCount = tagCount + 1
    totalRetail = totalRetail + (QuantityOnHand * RetailPrice)
    ReadNextRecord()
END WHILE
PRINT "Tags printed: " & tagCount
PRINT "Total retail value: $" & Format(totalRetail, "0.00")

Troubleshooting notes: initialize counters and totals before the loop; use a decimal/currency type for money; guard against NULL or non-numeric fields; verify the EOF/read pattern so you don’t skip the first/last record; always MoveNext/ReadNext to avoid infinite loops; format totals to two decimals when printing. These small changes fix the logical errors and make the routines robust.

Recommended Answers

All 7 Replies

Hi,
Your code is doing well.
Just in case, I have added the while loop in yours:

Dim x As Integer
        Do While x <= 45
            If x >= 20 Then
                If x <= 25 Then Debug.Print(x)
            End If
            If x >= 40 Then
                If x <= 45 Then Debug.Print(x)
            End If
            x = x + 1
        Loop

Or, you can do this:

Dim x As Integer = 20
        Do While x <= 45
            If (x >= 20 And x <= 25) Or (x >= 40 And x <= 45) Then
                Debug.Print(x)
            End If
            x = x + 1
        Loop

So here's the question. I need to write the pseudocode for a program that uses a single while...loop to print the numbers from 20 to 25 and from 40 to 45. Your loop will have at least one nested "if" statement that will determine when the numbers should be printed.

Here's what I came up with.

If X > = 20 Then
If X < = 25 Then Print X
End If
If X > =40 Then
If X < =45 Then Print X
End If

Well...
That is what I've answered...
Your code have *at least* one nested "If". Actually, yours has four "If" statements.

If you need the pseudo-code, you can write:

X = 0
WHILE X <= 45
    IF (X >= 20 AND X <= 25) OR (X >= 40 AND X <= 45)
            PRINT X
    END IF
    X = X + 1
END WHILE

which, isn't too much different from the actual VB.Net code...

Hi,
Is your problem solved?

Yes it is thank you for all your help on this.

Hi,
Then, mark this thread solved.

Hey I was wondering if this is correct or not. I need to read each record from the inventory database. Print a price tag for each item in inventory. Count & print the total number of tags printed. Sum and print the total retail value of the inventory in the store.


Read the first record
While not eof
Print fldItemnumber , fldDescription , fldQuantityonhand , fldRetailPrice
ItemCount = ItemCount +1
Totalretailvalue = (fldQuantity * fldRetailprice )
Read next record
Loop
Print “Retail price : “ & ItemCount
Print “Value of Inventory = “ & Totalretaivalue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.