စံကား

11Example programs

FizzBuzz

A classic counting game, built from functions, booleans and if/else.

FizzBuzz is a counting game. Count up from 1. Call out Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number itself for all the rest.

စံကား

Expected output

၁
၂
Fizz
၄
Buzz
Fizz
၇
၈
Fizz
Buzz
၁၁
Fizz
၁၃
၁၄
FizzBuzz

How it works

  • ပြတ်သလား ("does it divide?") returns a boolean: whether န % ဆခွဲ is zero. Its name is a question, which keeps the code readable.
  • FizzBuzz stores the two answers in variables သုံးပြတ် ("3 divides it") and ငါးပြတ် ("5 divides it"), then picks one of four outputs with an if / else-if / else chain.
  • The test order matters. "Both" must come first. 3 divides 15, so if the Fizz test came first, 15 would print Fizz.
  • The main program is two lines: a constant for where to stop, and a counted loop that calls FizzBuzz once per number.

Hands on

  • Change အဆုံး to 100.
  • Add a rule: print Bazz for multiples of 7.