WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Javascript - Conditional Statements Wed Mar 14, 2012 11:19 pm

Mr.Joker

Mr.Joker
Mr. WebArtz
Mr. WebArtz
Javascript - Conditional Statements
If & Else and If Else Statements in JavaScript


What is this tutorial about? Well, in javascript you can make some condition before something execute. For example if number 5 is larger then number 3 do this peace of code and so on. Here is how it look like:

Code:
<script type ="text/javascript">
var x = 5;
var y = 4;
if (x>y) {
document.write("Number " + x + " is larger then " + y);
} else {
document.write("The statement isnt true.");
}
</script>
Ok. I write two variables as you learned allready. One have value 5(x) and one have value 4(y). After that i made condition. If variable x is larger then variable y then do that. Thats how it work. Inside bracket we write condition we need. If that condition is true then code below will be executed, if not, well then there is that else. Else as you know mean something else, so if our first condition is not true then always do code below first one, which is that else. Here is an example when first condition isnt true:

Code:
<script type="text/javascript">
var x = 2;
var y = 4;
if (x>y) {
document.write("Number " + x + " is larger then " + y);
} else {
document.write("The statement isnt true.");
}
</script>
Now i just changed value of variable x from 5 to 2 because I want variable y be higher then x. When we run code now, our web browser start ececuting script and he start like this:
Ok, we have statement that say, if x grater then y do this code. Lets check that. Variable x have value 2, and variable have value 4, so that statement isnt true. Dont do that. What now? Well there is else. Lets do that code if first isnt true , and thats it.
But I want to show you some cool trick with boolean value.

Code:
<script type="text/javascript">
if (1) {
document.write("This statement is true.");
} else {
document.write("The statement isnt true.");
}
</script>
What you see now is just number 1 as condition and you tink what is that? How can number one be condition. Well, 1 is boolean type of data so 1 means true, and 0 means false. Thats it. 1- True, 0 -False. So we say instant that statement is true and first peace of code must be executed. If i changed 1 to 0 second one would be executed because its false.
Lets do some advanced thing now. There is else if add that can be used to make more conditions. Lets see example.
Code:
<script type="text/javascript">
var x = 5;
if (x>7) {
document.write("This statement isnt true.");
} else if(x>3){
document.write("The statement is true.");
} else {
document.write("Some random text");
}
</script>
So we have one variable with value 5. I made condition if 5 is higher then 7 do this (its not higher so it will never be executed. Then i made another condition else if and say if x higher then 3 do this code and this code is true and will be executed. So thats how I can make another condition and not just one. This is good, but we can do it also on this way:

Code:
<script type="text/javascript">
var x = 5;
if ((x>7) || (x>3)) {
document.write("One of the statements is true.");
} else {
document.write("Some random text");
}
</script>

We have two conditions in start now. We say in brackets again if x larger then 7 or x larger then 3 do this code. So this || is translated on english OR. I say, if something, or if something do this. Another example:
Code:
<script type="text/javascript">
var x = 5;
if ((x>7) && (x>3)) {
document.write("One of the statements is true.");
} else {
document.write("Some random text");
}
</script>
Now we have && which mean AND. So if first condition is true and if second condition is true do code , and if one of them is not true as realy isnt then do else code. So we will have on screen "Some random text". Thats all I have to tell you about If & Else statements, hope you like this tutorial. If there is something you cant understand just ask in same topic.

Tutorial for WebArtz.

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: Javascript - Conditional Statements Sun Mar 18, 2012 12:00 am

ankillien

ankillien
Administrator
Administrator
Accepted

Thanks Mr.Joker Very Happy

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum