There is No While Loop in GO

Most programming languages have a concept of a while loop. Because Go allows for the omission of sections of a for loop, a while loop is just a for loop that only has a CONDITION.

for CONDITION {
  // do some stuff while CONDITION is true
}

For example:

plantHeight := 1
for plantHeight < 5 {
  fmt.Println("still growing! current height:", plantHeight)
  plantHeight++
}
fmt.Println("plant has grown to ", plantHeight, "inches")

Which prints:

still growing! current height: 1
still growing! current height: 2
still growing! current height: 3
still growing! current height: 4
plant has grown to 5 inches

Go also allows you to omit the condition entirely to create an infinite loop:

for {
  // do some stuff forever
}

Assignment

We have an interesting new cost structure from our SMS vendor. They charge exponentially more money for each consecutive text we send! Let's write a function that calculates how many messages we can send in a given batch given a costMultiplier and a maxCostInPennies.

In a nutshell, the first message costs a penny, and each message after that costs the same as the previous message multiplied by the costMultiplier.

There is a bug in the code! Add a condition to the for loop to fix the bug. The loop should stop when balance is equal to or less than 0. So what condition should the for loop evaluate?

Solution

We want to calculate how many SMS messages we can send given:

  • costMultiplier (float64): How much more expensive each next message is compared to the previous one.

  • maxCostInPennies (int): The maximum total cost we are allowed to spend.

Cost pattern:

  • 1st message costs 1 penny.

  • 2nd message costs 1 * costMultiplier.

  • 3rd message costs previousCost * costMultiplier, and so on.

We must stop sending messages when we run out of balance (when the total cost reaches or exceeds maxCostInPennies).

1

Variable Setup

  • actualCostInPennies starts at 1.0 for the first message.

  • We assume we’ve sent the first message, so maxMessagesToSend starts at 1.

  • balance is how much money is left after sending the first message.

2

The Loop (Go’s while-style for)

This for loop is acting like a while loop:

  • Condition: balance > 0

    • While we still have money left, we keep sending messages.

  • Each iteration:

    1. Increase actualCostInPennies by multiplying by costMultiplier (next message is more expensive).

    2. Subtract that cost from balance.

    3. Increment maxMessagesToSend because we sent another message.

When balance becomes 0 or negative, the loop stops.

3

Fixing Overshoot

Because we subtract the cost after deciding to send the message, we might go slightly over budget on the last one:

  • If balance is negative, it means the last message pushed us over the limit.

  • To fix that, we decrement maxMessagesToSend by 1 to exclude that last, too-expensive message.

If balance is exactly 0, we spent the budget perfectly, and no adjustment is needed.

4

Return Value

We return the maximum number of messages that can be sent without exceeding maxCostInPennies.