Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Logical Operators and Conditional Statements

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Download FileShield free from PermissionResearch!
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

ivenms
Administrator
*******


Posts: 2,177
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #1

Shy Logical Operators and Conditional Statements


Yes, this section is for learning a bunch of operators plus the if/else statement. The first thing we'll do is go through some of the mathematical operators. Here they are:

+ addition
- subtraction
* multiplication
/ division
% modulus (this is the remainder when you divide something)

Now, let's take a look at comparison operators:

> Greater Than
< Less Than
>= Greater Than or equal to
<= Less Than or equal to
== Equal to (remember, the "=" sign assigns a value to a variable, the "==" compares two values for equality.)
!= Not equal to

And now, the logical operators:

&& AND
|| OR
! NOT

So, what good does this do you? Well, it'll come in handy for later scripts, as well as for the next thing we'll look at: The if/else statement.

if (conditional statement)
{
JavaScript statements...
}
else
{
JavaScript Statements.....
}

This statement is useful if you want to check for a variable value or a number of values. If the statement in side the ( ) symbols is true, the commands that follow the if command inside the { } symbols will be executed. Otherwise, the commands after the else statement are executed. Here is an example:

var number=3;
var mymoney=0;
if (number > 2)
{
mymoney=100;
}
else
{
mymoney= -100;
}


This set of code decides the fate of the variable mymoney, based on the value of the variable number. If number is greater than 2, then the value of mymoney will be changed to 100. If number is not greater than 2, the mymoney is -100, and I'm in some trouble with my bank.

Now, suppose you wanted to perform a set of commands a number of times, and end when you have what you need. To do this, you can use a for loop or a while loop to repeat things. First, the for loop:

for ( condition1; condition2; command)
{
JavaScript Statements....
}

OK, what this does is begin the loop with condition 1, end the loop with condition 2, and perform the command each time through. So, if you want to change a variable value 10 times, you would do something like this:

var count=1;
var mymoney=0;
for (count=1; count<11; count=count+1)
{
mymoney=mymoney+100;
}

The loop begins with count=1. Then the statement mymoney=mymoney+100 is executed the first time. Then the command count=count+1 is executed, making count equal to 2. Once that is done, the value of count is checked to be sure it is less than 11 (condition 2). Since count is 2, which is less than 11, the loop statement is executed again, and count goes up to 3. This goes on and on until count is equal to 11, thus not being less than 11. The loop will end before going through another time. What did I get out of this? Well, mymoney went from 0 to 1,000 really quickly!

Now for the while loop. It does pretty much the same thing as the for loop, but is written out differently:

var count=1;
var mymoney=0;
while (count<11)
{
mymoney=mymoney+100;
count=count+1;
}


This example does the same thing as my last one. The only thing that is checked is whether count is less than 11 or not. If it is, the statements are executed. If not, the loop ends. Remember to add one to count within the statement section here, or you'll get the ever popular "infinite loop".


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
15-09-2006 03:14 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Shy Logical Operators and Conditional Statements - Java Script ivenms 0 752 15-09-2006 03:17 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 06-01-2009, 10:37 PM


Copyright © 2002-2009 MyBB Group
Powered By MyBB