Switch/Case is a computational construct in many modern programming languages that allows the program to choose one of multiple execution options. The option is chosen by "switching" on the value of an input parameter.
King Fredrick loved performing surprise inspections of the palace guards. Naturally, he felt that these inspections kept everyone prepared. But, more importantly, he found that wandering the grounds for an inspection was the best way to keep the castle steward from finding him. If Fredrick stayed in the throne room, the steward would constantly bring problems to his attention. Sometimes, the king just needed some quiet.
"You there. Guard." called the king.
The guard groaned quietly. Unlike the king, the guards were not fond of surprise inspections.
"Yes, sir!" the guard responded in his best military voice.
"Tell me, are you always at this gate?" inquired the king.
"Yes, sir!" The guard could sense that it was going to be a long day.
"Wonderful. Wonderful. I have some questions about security. If someone comes down the path, how do you know whether to let them in?"
The guard reached into his little guard booth and pulled out a thirty page instruction manual. He quickly opened to the first page, hoping that the king did not see the coffee rings on the cover. It was considered disrespectful to use the official castle manuals as coasters. But, truthfully, that was often their most useful function.
The guard started to read from the manual in a loud military voice:
"Check the entrant's ID and examine their entrance ID class.
IF the person is a member of the royal family (ID class 0): let them pass,
ELSE IF the person is a merchant of a registered guild (ID class 2, 3, or 6): let them pass,
ELSE IF the person is from an opposing army (IF class 107): stop them,
ELSE IF the person is a registered juggler (IF class 71): let them pass,
ELSE IF the person is a registered mime (IF class 72): stop them,
"Enough!" interrupted the king. The manual was in the familiar IF-ELSE series of conditions of which the past king, Fredrick's father, had been so fond. Why any king would prefer that style was beyond Fredrick. Fredrick was certain that he had updated all the manuals by now.
"It looks as though we need to update your manual." the king stated.
The guard was confused. He also looked scared. "But Sir, I have not made any mistakes. No intruders have been let in and no VIPs made to wait. I have not done anything wrong!"
The king gave the guard a reassuring smile. It did nothing to assure the guard. "No. That is not it. The manual uses a complex format that is simply harder to understand. We switched everything else to switch/case a few years ago. For a long list of conditions like that, it can be cleaner."
"Switch/case?" asked the guard. He did not remember that from his training.
"Yes." the king looked around for a prop to illustrate. Finding none, he started gesturing with his hands.
"Here is how that would work in switch/case." the king began.
"Check the entrant's ID and find the first matching case.
Case Royal Family: let in and done,
Case Enemy: Keep out and done,
Case Merchant of a registered guild: let in and done
And so forth!"
"You just go down the list and find a match. It is the same logic as the long list of nested IF-ELSE conditions. It is just easier to read... Sometimes." finished the king. He loved explaining computational constructs and was debating launching into a description of unrolling loops, when he realized that he could be boring the guard. He had a habit of boring people sometimes. Then again, the guard was nodding intently as the king spoke. Maybe the guard would want to hear about unrolling loops.
"Oh..." responded the guard, clearly thinking this over. "Why do you keep saying 'and done' after each command?"
"Well technically, the way it works is that you go to the first case that matches and keep reading instructions down until you hit a break. That way you can use complex blocks of commands for multiple cases without repeating them. When I say done, I just mean break out of the switch statement."
"Oh... But what if the appropriate condition is not there?" asked the guard.
"You can always have a default case to cover everything else. For you, I would make that 'Keep the person out and call for clarification.'"
The guarded nodded. "Yes. That would seem like a good default. I would hate to have to make a judgement call on my own. But, it seems that you are not actually changing any of the procedures?"
"That is correct." answered the king. "Switch/case can be thought of as a sort of shorthand for a long series of if statements. We will simply clean up the manual… and maybe put something in there about marching bands. Yes. We should certainly have a case about marching bands."
The king trailed off and stared out at the castle wall. Although he could not explain it, he knew that marching bands, mimes, and clowns all needed their own cases. Something about each of them made him profoundly uneasy.
"Well, I shall return to the castle and set to work on a new manual right away." proclaimed king Fredrick. He was excited by the opportunity to update more castle documentation. Given the recent peace in the kingdom, he had not had much real work to do for a while.
This was the best news of the guard's day: an early ending to the surprise inspection.
For more information on IF-ELSE statements, see Learning IF-ELSE the Hard Way.
Or for more adventures of King Fredrick, see Hunting Dragons with Binary Search or Stacks, Queues, Priority Queues and the Prince's Complaint Line.