Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
richardgary1223 Avatar
Apr 30, 2008 8:42:37 GMT @richardgary1223 said:
..I am sort of confused when it comes to looping..

This tutorial is very good exp. to me.. :)


There's two main kinds of loops, for and while. (There's also a for-each and do-while, but don't worry about them.)

A for loop has this format:

for(initial statement; continue condition; extra condition){
Executed statement
}

Initial statement - this is executed once before the for loop is ever ran. This is typically a variable declaration like "a = 0"

Continue condition - before each loop in the for, this is checked. The for only continues while this is true.

Extra condition - this is executed at the very end of the loop. It's typically an increment (x++) or a decrement (x--)

Executed statement - This is the code that is executed for each loop through the for loop.


Now for a while loop. A while loop is straight forward. While the continue condition is true, it continues.

while(continue condition){
Executed statement
}

We can express a for loop in terms of a while loop

initial statement
while(continue condition){
Executed statement
extra condition
}

So in essence, they're both very similar.

That help any? :)

richardgary1223
Guest
..What comes under Initial Statement then?

Like what goes into it?..

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
There's two main kinds of loops, for and while. (There's also a for-each and do-while, but don't worry about them.)

You mean a for-in? :p

And just because I like to explain things a bit differently,

for(statement; condition; operation) {
exec code
}

statement: can pretty much be anything.
condition: whatever needs to be true for exec code to run
operation: something done so that the condition eventually returns false (stops the loop).

Again, statement can be anything. In fact, so can operation. These parameters are really just for convenience.

Simple example:

for(var x = 0; x < 6; x ++) {
alert(x)
}

The above will declare variable x, run the loop, increment x, and repeat the process of running and incrementing so long as x is less than 6. For the above, we recieve 6 alerts (0,1,2,3,4,5).

Same example without the "convenience"

var x = 0;
for(;x < 6;) {
alert(x)
x ++
}

richardgary1223
Guest
..Oh yes.. I seem to understand it..

--The tuts from here are great,I didn't seem to understand looping in W3Schools.. ;)

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
It just takes a bit to understand stuff. Getting different explanations can help greatly.

Also, notice Aaron's last example

var x = 0;
for(;x < 6;) {
alert(x)
x ++
}

This is just like a while also.

var x = 0;
while(x < 6) {
alert(x)
x ++
}

Exact same amount of characters used too. :)



Aaron: Yeah, for-in. :P Whoops. All other languages is a for-each.

PHP:
foreach($arraystuff as $key => $value){
// Blah
}

Java:
int[] arrayHolder = new int[5];
for(int row : arrayHolder){
// Code
}


Still called a for-each there... so I just used that. :P Oh well.

richardgary1223
Guest
SO for 'for' looping:

<script type="text/Javascript">
for (i=1; i <= 8; i++)
{
document.write(This is " +i);
}
</script>



Chris

Chris Avatar

******
Head Coder

19,519


June 2005
richardgary1223 Avatar
May 9, 2008 3:17:20 GMT @richardgary1223 said:
SO for 'for' looping:

<script type="text/Javascript">
for (i=1; i <= 8; i++)
{
document.write(This is " +i);
}
</script>



Besides the syntax error, yes, that is correct.

The error is this: (You forgot the red)
document.write([red]"[/red]This is " +i);

Mini Coder

Mini Coder Avatar

*
New Member

13


July 2011
Are all of the different code types used in one code? Like CSS, HTML, and other codes all used in one code?
My Forum
I came here to learn how to code for my forum and to share with others[/b]

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Mini Coder Avatar
Are all of the different code types used in one code? Like CSS, HTML, and other codes all used in one code?


They can be. HTML, CSS, and JavaScript are three separate things that integrate seamlessly. HTML is the markup that places objects and text on the page. CSS is the styling used to pretty things up (colors, positioning, etc.) JavaScript is used to make things dynamic (i.e. if I click "Expand" on the textarea while posting, it expands downward.)

For the sake of elaboration, CSS is usually placed either as part of an HTML tag or inside of one. So:
<span style="font-weight: bold">Text</span>
<style type="text/CSS">
.someSpan {
font-weight: bold;
}
</style>


In these cases, CSS is an attribute of an HTML tag or the content of another. So, CSS integrates with HTML. JavaScript is similar...

<script type="text/Javascript">
alert("Hello world!");
</script>


In this case, JavaScript's code is placed inside an HTML tag again. This one doesn't modify HTML, but it does produce a popup window. It can however directly interact with HTML, much like how we use it on ProBoards.

Does that help explain it? :)

Joe Kerr

Joe Kerr Avatar
Why So Serious?



769


June 2010
I needed to read this a few years ago. You plan on writing any tuts for V5 Chris?
Building Your Proboard, a Basic Guide to Getting Your Forum Started



Need somewhere to host files?[/url]

~Memzak~

~Memzak~ Avatar
Inquire never, so always need elephants.

****
Senior Member

408


May 2009
I needed to read this period. I've done plenty in Java, so when I started Javascript I already knew basic logic structures and variables... what I didn't know was how to grab anything from the webpage that wasn't a form input or something.

Getting the elements by tag... should have looked up something like that / spent more time with javascript before concluding I'll never be able to make something useful for proboard stuff...




Joe Kerr

Joe Kerr Avatar
Why So Serious?



769


June 2010
w3schools.com has saved my life a few times...and google in general :P
Building Your Proboard, a Basic Guide to Getting Your Forum Started



Need somewhere to host files?[/url]

~Memzak~

~Memzak~ Avatar
Inquire never, so always need elephants.

****
Senior Member

408


May 2009
Yea, google is my main problem solver for... everything.

Python spazzing out? Google.
jQuery being retared? Google.
Bash problems? Google.
PHP errors? Google.
Java compiling erro-

I think you get the idea... (of course some websites are more effective at small, specific problems)





Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Joe Kerr Avatar
w3schools.com has saved my life a few times...and google in general :P


w3fools.com/

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Joe Kerr Avatar
I needed to read this a few years ago. You plan on writing any tuts for V5 Chris?


I may. I want to see what their developer docs cover, once those get published... but those might be quite a ways out still. Probably not until the April deadline, actually.

Definitely feel free to ask questions though. I may record myself doing a skin conversion, because that might be helpful.

newBookmarkLockedFalling