Go · Chapter 3 of 26

if, for and switch

By the end of this chapter you can make a Go program decide with if and else, repeat work with for in all its shapes, stop or skip a round with break and continue, and choose between many cases with switch. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The language
  • 4 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go are free. This one comes with the all labs plan.

Why you need this

Every program so far ran its lines once, top to bottom. BijliGo's code rarely gets to do that. A charger that has been silent for five minutes is offline, and one that missed a single heartbeat is only late: the ingest service must decide. A session that ran for 47 minutes has 47 meter readings to add up: the code must repeat. The billing code gets a charger's kind, one of AC7, DC30, DC60 or LEV3, and must choose its tariff.

Tenzin's 2 am scripts do all three, and on Wednesday one of them paged him for a charger that was only 61 seconds late, because a limit was off by one. With only what you know from chapters 1 and 2, you could print the state of one charger, but not pick it. This chapter gives Go programs their three ways of controlling what runs: if, for and switch.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    state.go: a charger's state from its last heartbeat

    A charger sends a heartbeat every 30 seconds, and the variable age is how many seconds ago charger C1042 sent its last one. Keep the line age := 45 exactly as it is. The starter prints the first line; after it print the state:

    C1042: last heartbeat 45 s ago

    State: online

    The state is online when age is 60 or less, late when age is from 61 to 300, and offline when age is more than 300. When it is offline, print one more line after the state: Action: page the on-call engineer.

    The Check also runs state.go with age := 60 and age := 0 (State: online), age := 61 and age := 300 (State: late), and age := 301 (State: offline, then the Action line). The first line always shows the age.

  2. Task 2

    tariff.go: a kind's tariff, with switch

    Keep the line kind := "DC30" exactly as it is, and use a switch to print the kind, its tariff and whether it is a fast charger:

    Kind: DC30

    Tariff: 2000 paise a kWh

    Fast: true

    The tariffs: AC7 1400, DC30 2000, DC60 2400, LEV3 900 paise a kWh. DC30 and DC60 are fast (Fast: true); AC7 and LEV3 are not (Fast: false). Any other kind prints Tariff: unknown and Fast: false, so kind := "AC22" prints Kind: AC22, Tariff: unknown, Fast: false.

    The Check looks for a switch in tariff.go, and also runs it with kind := "AC7", "DC60", "LEV3" and "AC22".

  3. Task 3

    energy.go: a session's energy, minute by minute

    A charger gives powerW watts for minutes minutes, and each minute adds powerW / 60 watt-hours (a whole number) to its meter, which starts at 0. Keep the lines powerW := 24000 and minutes := 5 exactly as they are, and print the meter after every minute, then the total:

    Minute 1: 400 Wh

    Minute 2: 800 Wh

    Minute 3: 1200 Wh

    Minute 4: 1600 Wh

    Minute 5: 2000 Wh

    Total: 2000 Wh in 5 minutes

    The Check also runs energy.go with powerW := 7200 and minutes := 3 (Minute 1: 120 Wh, Minute 2: 240 Wh, Minute 3: 360 Wh, Total: 360 Wh in 3 minutes) and with minutes := 0, when the only line is Total: 0 Wh in 0 minutes. Remove the starter's own line that prints powerW and minutes.

  4. Task 4 · interview question

    Interview: loops.go, Go has no while

    Tenzin wrote loops.go with while, out of habit, and it does not compile. Fix it, keeping what it does, so that it prints exactly:

    Sessions paid: 3

    Balance left: 2400

    Full after 13 minutes

    The first loop pays for sessions of cost paise while the balance covers one: write it as a for loop with only a condition. The second adds 3 percent a minute until the battery is at 100 percent or more: write it as a for loop with nothing after for, which checks the battery at the start of each round and ends with break. Keep the lines balance := 15000 and percent := 62 exactly as they are: the Check also runs loops.go with balance := 9000 (Sessions paid: 2, Balance left: 600), with percent := 97 (Full after 1 minutes) and with percent := 100 (Full after 0 minutes), and looks for both kinds of loop.

    Joseph will ask: Go has no while keyword. How do you write a loop that runs while a condition holds, and one that runs until something inside it says stop?

What you will learn

  • if and else: run a block only when a condition is true.
  • if with a short statement: a variable that lives only inside the if.
  • for: Go's only loop, in three shapes, and for i := range 10.
  • break and continue: leave a loop, or skip to its next round.
  • switch: many cases, no fall-through, several values in a case.
  • switch with no value: a cleaner chain of ifs.

More in The language

Build "if, for and switch" yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go are free. This one comes with the all labs plan.