JS -- Lesson 2


Lesson 2


This lesson we will be going over if-else statements, a very useful and highly used way to perform various actions.

Before i teach you if-else statements first i must teach you the function, functions are used to place code that can be brought up later whenever it is needed.

function var_loop()
{

}

The above code shows how to write a function.

The first part "function" tells javascript we are going to make a new function. After that is the name of your function, and like a variable is given a name for its use. Put parantheses after the name. Then enclose what you want in your function with 2 brackets "{" and "}".


if-else statements

if-else statements give you a way to execute code when a certain condition has occured, for instance:

function var_loop()
{

if (condition)
{
execute;
}
else if (condition)
{
execute;
}
else
{
execute;
}

}

Inside the function "var_loop()" is an if-else statement. if-else statements run through all the conditions until one of them is true, then it stops. It will only execute a condition that is true, i will explain how each part works.

The first part of an if-else statement is the if part.
if (condition)
{
execute;
}

This tells us when a condition between the parantheses is true then the execution statement, that is placed between the 2 brackets, then is executed. The execution code is ended with a semi-colon.

Now is the "else if" statement, it isnt required for an if-else statement, but can be useful for more parameters.

else if (condition)
{
execute;
}

The "else if" statement works exactly like the "if" statement, if the first "if" statement isnt true it will go down to this "else if" statement afterwards and see if it is true. You can have as many "else if" statements as you like.

Last is the "else" statement, it also isnt required like the "else if" statement, but it is used when you want something executed when your "if" or "else if" statements arent true.
else
{
execute;
}

The "else" statement does not have a condition because its condition is anything other than the "if" and "else if" statements, but it works just the same.


Now its time to put it in action:

<SCRIPT LANGUAGE="Javascript">

var w = 1;

function var_loop()
{

if (w == 1)
{
document.write("Hey, w = " + w + ".<br>");
}
else if (w == 5)
{
document.write("Hey, w = " + w + ".<br>");
}
else
{
document.write("Hey, w doesnt equal 1 or 5 it equals " + w + ".");
}

}


var_loop()
w = 5
var_loop()
w = 10
var_loop()


</SCRIPT>

Above is the code, and below is the result of it:





Now look at the above code and the result to see if you can figure out what happened and how it happened.

Did you understand it? If not then i will explain it for you, none of this stuff is new, just written in a way you might not be used to yet.

Let me explain, the first part of our code is the beginning tag, SCRIPT to tell us we are going to start writing in javascript. Then below it i make a variable, w, and i store the value of 1 there. Then i make a new function called var_loop(). Inside the 2 brackets of the function var_lopp() is an if-else statement.

The statement first says, if w is equal to 1, write on my document, "Hey, w = " and then add in the current value stored at w, and then put a period to end the sentence and beak to a new line.

Next the statement tells us, that if the statements before this one were false then check if w is equal to 5, if that is true then write, "Hey, w = " then add the value stored at w, then add a period and break to a new line.

The third statement says if all other statements are false, then write on my document, "Hey, w doesnt equal 1 or 5 it equals " then add the value currently stored at w, then end it with a period.


Now that is just what the function does when we bring the function up, we havent told it to do anything yet, now we tell javascript to run var_loop().


The first var_loop() run gives us the first line of the result, because the first statement was true, w equaled 1 at the time we ran the function.

Then we make the value stored at variable w change to 5.

Now when we run the function again and this time the second statement is true because w is now equivalent to 5, so it writes on the second line.

Then we change the value stored at w again, to 10.

Lastly we call the function one more time and this time w isnt equivalent to 1 or 5, so it does the else statement, and writes out the execution code for the else statement on the third line.
Join Today
 
Join our Website's Jester Forum today!
Its FREE and easy......
Click Here
Advertisement
 
News
 
7-12-07
_____________________
Status: SITE OPENED

WELCOME!

Enjoy the STAY

If you enjoyed this page,
add 2 favorites or tell a friend

Daily Event
 
Fortune of the Day

Quick Search
 
Google:
Yahoo:
Excite:
Ads & News
 
 
39144 visitors
=> Do you also want a homepage for free? Then click here! <=