Skip to content

Exercise 4 of 10 · Loops

Vending Machine Restock

What you will make

A program that restocks a vending machine slot one item at a time until it reaches capacity.

The one new idea: Repeating a block with a while loop until a condition becomes false

Sometimes you don't know in advance how many times to repeat something — a while loop repeats based on a changing condition instead of a fixed count, which a for loop isn't built for.

Go straight to the code ↓

A for loop is great when you know up front how many times to repeat something. A while loop is for the more common real-world case: keep going as long as some condition holds, however many times that ends up being. A while (condition) { ... } checks its condition before every pass through the body, including the very first one, and stops the moment that condition becomes false.

Worked example

Here's a storage closet being filled with stacked chairs up to a limit:

C#
int chairLimit = 6;
int chairsStacked = 2;

Console.WriteLine($"Starting stack: {chairsStacked}");

while (chairsStacked < chairLimit)
{
    chairsStacked = chairsStacked + 1;
    Console.WriteLine($"Stacked one more. Total: {chairsStacked}");
}

Console.WriteLine($"Closet stack is full at {chairsStacked} chairs.");
Output
Starting stack: 2
Stacked one more. Total: 3
Stacked one more. Total: 4
Stacked one more. Total: 5
Stacked one more. Total: 6
Closet stack is full at 6 chairs.

The loop keeps adding one chair at a time only while chairsStacked < chairLimit stays true, and it stops the instant the stack reaches the limit.

Your turn

The starter code restocks a vending machine slot from a starting count up to its capacity. Its while condition is written backwards, currentCount > slotCapacity, which is false right from the start since the starting count is well below capacity, so the loop body never runs even once and nothing gets restocked. Fix the condition so the loop runs while there's still room left in the slot.

If something goes wrong

If the program seems to hang and never finishes, check that the loop body actually changes the variable the condition depends on — a while loop whose condition never becomes false will run forever. If instead nothing happens at all, like in the starter code, check whether your condition is backwards: a while loop meant to fill something up should keep going while the current amount is less than the target, not greater than it.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing a while condition backwards, like > instead of <.
Depending on the starting values, this either skips the loop's body entirely or makes it run when it shouldn't.
Forgetting to update the variable a while condition depends on, inside the loop body.
If the condition's inputs never change, the loop either never runs or never stops.
Confusing 'while this is true, keep going' with 'while this is true, stop'.
A while loop's body runs only while the condition currently evaluates to true — the moment it's false, the loop ends.

Want a blank editor instead? Open the C# playground.