ptg7068951
92 HOUR 7:Using Conditional Tests to Make Decisions
Q&A
Q. Theifstatement seems like the one that’s most useful. Is it possible
to use only ifstatements in programs and never use the others?
A. It’s possibleto do without elseorswitch, and many programmers
never use the ternary operator ?. However,elseandswitchoften are
beneficial to use in your programs because they make the programs
easier to understand. A set of ifstatements chained together can
become unwieldy.
Q. In the Clockprogram,why is 1 added to Calendar.MONTHto get the
current month value?
A. This is necessary because of a quirk in the way that the Calendar class
represents months. Instead of numbering them from 1 to 12 as you
might expect,Calendar numbers months beginning with 0 in January
and ending with 11 in December. Adding 1 causes months to be repre-
sented numerically in a more understandable manner.
Q. During this hour,opening and closing brackets {and}are not used
with an ifstatement if it is used in conjunction with only one state-
ment. Isn’t it mandatory to use brackets?
A. No. Brackets canbe used as part of any ifstatement to surround the
part of the program that’s dependent on the conditional test. Using
brackets is a good practice to get into because it prevents a common
error that might take place when you revise the program. If you add a
second statement after an ifconditional and don’t add brackets,unex-
pected errors occur when the program is run.
Q. Doesbreakhave to be used in each section of statements that follow
acase?
A. Yo u d o n ’t h av e t o u s e break. If you do not use it at the end of a group
of statements,all the remaining statements inside the switch block
statement are handled,regardless of the case value they are being
tested with. However,in most cases you’re likely to want a breakstate-
ment at the end of each group.
Q. Why did the Thompson Twins get that name when they were a trio,
they were not related,and none of them was named Thompson?
A. Band members Tom Bailey,Alannah Currie,and Joe Leeway called them-
selves the Thompson Twins in honor of Thomson and Thompson,a pair
of bumbling detectives featured in the Belgian comic books The
Adventures of Tintin.