Beruflich Dokumente
Kultur Dokumente
1/15
2/15
WE WILL LOOK AT
for
for ... in
for ... of
3/15
FOR LOOPS
for clearly shows the start and end values
for is especially good for handling a series of data
(data_structure.length
tells you how many items data_structure contains)
4/15
<html><head>
<script>
varcontinents=["Australia","Africa",
"Antarctica","Eurasia","America"]
varresponse,count=0
for(varindex=0index<continents.length
index++){
response=confirm("Haveyoubeento"+
continents[index]+"?")
if(response)count++
}
alert("Youhavebeento"+count+
"continents!")
</script>
</head></html>
5/15
6/15
7/15
<!doctypehtml>
<html><head>
<script>
varcontinents=["Australia","Africa",
"Antarctica","Eurasia","America"]
varresponse,count=0
for(varindexincontinents){
response=confirm("Haveyoubeento"
+continents[index]+"?")
if(response)count++
}
alert("Youhavebeento"+count+
"continents!")
</script>
</head></html>
8/15
9/15
<!doctypehtml>
<html><head>
<title>Exampleofforin</title>
<script>
varresponse,count=0
varonePerson={initials:"DR",age:40,
job:"Professor"}
for(varpropertyinonePerson){
alert(property+"="+onePerson[property])
}
</script>
</head></html>
10/15
11/15
12/15
<!doctypehtml>
<html><head>
<title>Exampleofforof</title>
<script>
varcontinents=["Australia","Africa",
"Antarctica","Eurasia","America"]
varresponse,count=0
for(varcontinentofcontinents){
response=confirm("Haveyoubeento"+
continent+"?")
if(response)count++
}
alert("Youhavebeento"+count+"continents!")
</script>
</head></html>
13/15
OMITTING PARTS
The 3 parts of the for can be omitted
E.g. this will make an infinite loop:
for(){
alert("Welcome!")
}
14/15
This is OK:
varnumber=1
for(number<=12number++){
alert(number+"times9=",number*9)
}
So is this:
for(varrabbits=2,generation=1
generation<=12
generation++,rabbits*=2){
alert("gen:"+generation+
"total:"+rabbits)
}
15/15