Sams Teach Yourself C in 21 Days

(singke) #1
The variable used in the loop can be one declared earlier, or it can be declared as part of
the loop. In this example, the loop counts down from 100 to 50 by twos:
for (int i = 100; i >= 50; i -= 2)
{
}
You can count by fractional values too, as long as the data type of the variable is appro-
priate:
for (double d = 0.0; d < .99; d += 0.01)
{

Summary ............................................................................................................


You learned a lot about the basics of Java today. You encountered a lot of material, but it
should have been easy to understand because Java is so similar to C and C++ in the areas
we covered today. Now that you know the fundamentals of Java program structure, key-
words, identifiers, variables, arrays, operators, and flow control, you are ready to move
on to the really interesting parts of Java covered in the next two days’ lessons.

Q&A ....................................................................................................................


Q Is Java identical to C and C++ in the basics of data types, operators, and flow
control?
AAlmost, but not quite. There are a few differences, but knowing something about C
and/or C++ will certainly give you a “leg up” when learning Java.
Q How does Java differ from C and C++ in the way text data is stored?
ABoth C and C++ use an array of characters to store text (strings). In contrast Java
uses the Stringclass. Because it is a class rather than a simple array,Stringoffers
significantly more power and flexibility for working with text.
Q What is a console program? Is this all Java can do?
AA console program is one whose input and output involve only text displayed on
the screen or typed in by the user. Java is also capable of very sophisticated win-
dows and graphics, but console I/O is sufficient for showing you the fundamentals
of the language.

Workshop ............................................................................................................


The Workshop provides quiz questions to help you solidify your understanding of the
material covered, and exercises to provide you with experience in using what you’ve
learned. Answers to the quiz are provided in Appendix F.

720 Bonus Day 4

39 448201x-Bonus4 8/13/02 11:19 AM Page 720

Free download pdf