## Thursday, December 21, 2017

### Variables, IF statements, and Magic Boots

A variable represents a location in the computer’s memory where you can store a single piece of data. You can use the variable’s name to look up, set, or modify the value stored in that location.

Ann jolted in surprise as a loud tone cut through the silence. She quickly tapped her boots together, resetting them before they could blare their pre-recorded message. She mumbled a few insults at her footwear. Did they have to alert her after every mile?

Ann realized this was the eighth warning chime today. Had she walked eight miles already? She’d been so lost in her thoughts, that she hadn’t been paying much attention to the journey itself. She was now three days into her quest to save the kingdom and she still had no idea what to do. Why couldn’t the seers have been more specific than, “There is a darkness coming. Princess Ann must travel forth alone to save the kingdom.” Maybe they could have told her what the darkness was or, at least, hinted at which direction to travel. Stupid vague prophecies.

Ann looked down at her boots and debated for the hundredth time whether they were worth the annoyance. The boots had been a birthday gift from Marcus, the royal wizard. They represented his first foray into variable magic—a form of magic that allowed an object to store a single piece of information. In this case, her boots’ variable (helpfully called dist) stored the distance she traveled. After each step, dist increased by the length of the step.

dist = dist + step_length

Initially the boots had provided wonderful entertainment. Ann measured everything. She measured the length and width of her room, the distance to the kitchen, the distance to all ten of the castle’s bathrooms, the circumference of the castle wall, and the diameter of Fido’s turtle pond (the boots were, of course, water proof). She’d even annoyed the castle architect by noting the courtyard was six inches shorter than advertised.

After every trip she’d read the variable’s value from her left heel. Then she’d click the heels together to set the distance back to zero.

IF heels clicked: set dist = 0

If she also used her watch to track the time, she could even compute her speed:

Average speed = dist / time

Variable magic provides such wonderful power.

Unfortunately, Marcus had gone overboard. In addition to the variable itself, he added his own IF-statement based enhancements. After walking a mile the boots would loudly recommend that she take a break:

IF dist > 1 mile: Alert the wearer to take a break

The message, which sounded like the castle herald with hiccups, grated on her nerves. Worse, Marcus hadn’t properly thought through the details of his IF statement. The statement only checked if dist was greater than a mile. So once she’d walked a mile, the boots would “helpfully” alert her to this fact after every single step. Ann quickly learned to reset the distance to zero the moment she heard the chime.

With a sigh, Ann decided to leave on the boots. While they were extremely annoying, at least they tracked useful information. Marcus had once confided that his original design enabled the boots to track their smelliness. She shuddered as she considered what that alert might say.

-------------

There are now three books out in the Computational Fairy Tales universe!  See the books page for more information.

#### Post a Comment

Note: Only a member of this blog may post a comment.