QB Challenges:

# Challenge: too many IF - THENs

February 18 2011 at 4:45 PM

Imagine that the portion of the keyboard that has the numbers 0 through 9 and all 26 letters of the alphabet is divided into 4 quadrants. Quadrant 1 is in the upper left, and moving clockwise towards Quadrant 4.

The following program works fine - it tells you what Quadrant the key pressed belongs in.
But it takes 28 conditionals in the WHILE/WEND loop to do the job of figuring out which Quadrant the key belongs in.

The Challenge: Find the most compact solution to achieve the same user Input and Output.
The fewest conditionals inside the loop wins (nothing).

Note that the program must also include the Upper Case Letters.

The following is the program to be modified, not an entry:

==============================================================================

PRINT " Type any key a-z (A-Z) or 0-9"
PRINT " ESC to end"
PRINT

WHILE k\$ <> CHR\$(27)

k\$ = INKEY\$

IF k\$ = "a" OR k\$ = "A" THEN quadrant% = 4
IF k\$ = "b" OR k\$ = "B" THEN quadrant% = 3
IF k\$ = "c" OR k\$ = "C" THEN quadrant% = 4
IF k\$ = "d" OR k\$ = "D" THEN quadrant% = 4
IF k\$ = "e" OR k\$ = "E" THEN quadrant% = 1
IF k\$ = "f" OR k\$ = "F" THEN quadrant% = 4
IF k\$ = "g" OR k\$ = "G" THEN quadrant% = 3
IF k\$ = "h" OR k\$ = "H" THEN quadrant% = 3
IF k\$ = "i" OR k\$ = "I" THEN quadrant% = 2
IF k\$ = "j" OR k\$ = "J" THEN quadrant% = 3
IF k\$ = "k" OR k\$ = "K" THEN quadrant% = 3
IF k\$ = "l" OR k\$ = "L" THEN quadrant% = 3
IF k\$ = "m" OR k\$ = "M" THEN quadrant% = 3
IF k\$ = "n" OR k\$ = "N" THEN quadrant% = 3
IF k\$ = "o" OR k\$ = "O" THEN quadrant% = 2
IF k\$ = "p" OR k\$ = "P" THEN quadrant% = 2
IF k\$ = "q" OR k\$ = "Q" THEN quadrant% = 1
IF k\$ = "r" OR k\$ = "R" THEN quadrant% = 1
IF k\$ = "s" OR k\$ = "S" THEN quadrant% = 4
IF k\$ = "t" OR k\$ = "T" THEN quadrant% = 1
IF k\$ = "u" OR k\$ = "U" THEN quadrant% = 2
IF k\$ = "v" OR k\$ = "V" THEN quadrant% = 4
IF k\$ = "w" OR k\$ = "W" THEN quadrant% = 1
IF k\$ = "x" OR k\$ = "X" THEN quadrant% = 4
IF k\$ = "y" OR k\$ = "Y" THEN quadrant% = 2
IF k\$ = "z" OR k\$ = "Z" THEN quadrant% = 4

IF k\$ >= "1" AND k\$ <= "5" THEN quadrant% = 1
IF k\$ >= "6" AND k\$ <= "9" OR k\$ = "0" THEN quadrant% = 2