— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet126/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   122   123   124   125   126   127   128   129   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

 int x = 0; int z = ++x;
produces: x is 1, z is 1
But putting the ++ after the x give you a different result:
int x = 0; int z = x++;
produces: x is 1,  but z is 0! z gets the value of x and then x is 
incremented.
Difference bet ween 
for
and 
while
Trips through a loop
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
System.out.println(“done”);
output:
File Edit Window Help Repeat
%java Test
0
1
2
3
4
5
6
7
done
declare int 
i
set 
to 0
is 
< 8?
(the boolean 
test)
true
enter loop 
body
false
print “done”
(jump below loop)
print the value 
of 
i
increment 
i
(the iteration 
expression)
while loop has only the boolean test; it doesn’t have 
a built-in initialization or iteration expression. A while 
loop is good when you don’t know how many times to 
loop and just want to keep going while some condi-
tion is true. But if you know how many times to loop 
(e.g. the length of an array, 7 times, etc.), a for loop is 
cleaner. Here’s the loop above rewritten using while:
int i = 0;
while (i < 8) {
System.out.println(i);
i++;
}
System.out.println(“done”);
we have to increment 
the counter 
we have to declare and 
initialize the counter


116
 
chapter 5
for (String name : nameArray) { }
The elements in the 
array MUST be 
compatible with the 
declared variable type.
With each iteration, 
a different element 
in the array will 
be assigned to the 
variable “name”.
What it means in plain English: “For each element in nameArray, assign the 
element to the ‘name’ variable, and run the body of the loop.”
How the compiler sees it:
*
Create a String variable called name and set it to null.
*
Assign the first value in nameArray to name.
*
Run the body of the loop (the code block bounded by curly braces).
*
Assign the next value in nameArray to name.

Repeat while there are still elements in the array.
Part One: iteration variable declaration
Use this part to declare and initialize a variable to use within the loop body. With each 
iteration of the loop, this variable will hold a different element from the collection. The 
type of this variable must be compatible with the elements in the array! For example, 
you can’t declare an int iteration variable to use with a String[] array.
Part Two: the actual collection
This must be a reference to an array or other collection. Again, don’t worry about the 
other non-array kinds of collections yet—you’ll see them in the next chapter.
Declare an iteration variable 
that will hold a single element 
in the array. 
The enhanced for loop
Beginning with Java 5.0 (Tiger), the Java language has a second kind of for loop 
called the enhanced for, that makes it easier to iterate over all the elements in an 
array or other kinds of collections (you’ll learn about other collections in the next 
chapter). That’s really all that the enhanced for gives you—a simpler way to walk 
through all the elements in the collection, but since it’s the most common use of a 
for loop, it was worth adding it to the language. We’ll revisit the enhanced for loop in 
the next chapter, when we talk about collections that aren’t arrays.
The code to 
repeat goes here 
(the body).

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   122   123   124   125   126   127   128   129   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish