Logical Operators and Conditional Statements

How to say and, or, if, else, for, and while all over again

 

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".

So, why all the technical stuff? Well, it will help you better understand some more complicated scripts.....and you could just go off and write some stuff! The next section uses a script with an if/else statement....so let's check it out! JavaScript Prompts.

BreBru.com Information Technology HTML