Thursday, July 31, 2008

Week 03 Lab3 -- WBP

This lab was tricky at first, then I just had to think about it.
First of all, you need to understand what exactly a "for loop does.

heres the syntax of a for loop

for(initialize the for loop; give it a problem; tell it what it needs to do to fix its problem){

}

initialize the for loop -
You need to turn it on basically. For beginning, its always awesome to create a variable there. Something that usually is a number, that equals 1 or 0.

var aNum=1;

next you need to set a condition this is best when telling this aNum, that its smaller than another number.

aNum <=5;

Next how do we fix this problem, well we just need to + to aNum until it reaches 5
aNum++

This is how it would look.

for(aNum = 1; aNum <= 5; aNum++){
alert("we have looped " + aNum + " time.");
}

All we created was a loop that will run 5 times. See???
For loops are awesome if you want something to repeat several times, hence "loop".
Try it out.


The assignment calls for a document write that outputs.
01234

Basically I just showed you the exact way to do that

for (var aNum = 0; aNum<6; aNum++){
document.write(aNum);
}
This is less than 6 because the number is indexed. 012345??!!

Next we need to document write
1
2
3
4
5


This is trickier but a no brainer. Since we are document writing to an HTML page, we are allowed to use HTML tags as strings. This is awesome, because you could basically build a web page by just using javascript. WTFUNK!!!

for (var aNum = 1; aNum<=5; aNum ++){
document.write(aNum + "?");
}
what html tag do you use to create a break?


Last part
2, 4, 6, 8, 10


Basically this is the exact same thing. except,
we don't want to ++, we probably just want to add 2.
and We don't want to start out with 0 or 1. We probably just want to start with 2.

for (var aNum = 2; aNum <=10: aNum += 2){
//that was easy
// next we Document write
document.write(aNum + "? ");

//what would go there???
// 2, 4, 6, 8, 10



Thats it. For loops are dope! makes things much easier. as for While loops, and DoWhile loops???
Pfft. I have no idea how those work!

:D

1 comment:

kateF said...

do-while loops:

syntax:

do {

//code

} while(condition);

very very similar to the regular while-loop (do you know how that works?) but in this case, you check the condition for the loop at the END, so no matter what, the code within your do-while curly brackets will execute atleast ONCE.