Lesson 2: JavaScript Logic

In programming, there are three basic logic sequences.
Sequential logic has operations that occur in a set order: Step 1 followed by Step 2, followed by Step 3, and so forth.
Decision/branch logic has the program taking one of two paths, dependent upon some kind of condition. If this is true, do this operation; if not, do that one.
Loop logic has the program repeat an operation or operations until told to stop doing so. What stops the loop is a condition that changes, either checking for when it is true or no longer true.
We have done Sequential logic in JavaScript Lesson 1. Define variables, prompt for input, do calculations, output to the webpage. Now we will look at making decisions and controlling loops. Both deal with the programmer defining a controlling condition. This often deals with Boolean literals.
Boolean literals have only two possible values; true or false. Either the condition is true or it is not. And this is where the difference between = and = = comes into play. The = takes whatever is listed to the right of it and places that value into what is to the left. The = = means what is to the right equals what is to the left.
Connecting symbols for Boolean operations
OperatorOperation
<Less than
>Greater than
= =Equal to
<=Less than or equal to
>=Greater than or equal to
!=Not equal to
&&AND; true only if BOTH conditions true
||OR; true if both or EITHER condition true
!Negates the value
Boolean operation results
OperatorOperation
true&&trueTrue
false&&trueFalse
true&&falseFalse
false&&falseFalse
true||trueTrue
false||trueTrue
true||falseTrue
false||falseFalse

IF statements

The most common decision checking statement in third generation languages is the If statement. This code checks to see if a condition is true; and if it is, performs the statements under its control.

if(Boolean expression)
  {
   statement;
   statement;
     etc.
   }
Examples:

if(hrs>40)
  {
   reghrs = 40;
   othrs = hrs - 40;
   }
if((temp<-100)||(temp>220))
  {
   document.write("Temperature out of range");
   }
if((a>0)&&(b>0)))
  {
   document.write("a & b are both above zero");
  }
An extension is the If/then/else statement.

if(Boolean expression)
  {
   statement;
  }
  else
  {
   statement;
  }
if(hrs>40)
  {
   regpay = pay*40;
   otpay = pay*(hrs-40);
  }
  else
  {
   regpay = hrs*40;
   otpay = 0;
  }

Note: The isNaN() function is useful in checks. This is the 'Is Not A Number' function, which checks input and verifies if it is numeric or alphabetic in basic nature.
while(isNaN(hrs))
  {
   alert("Enter a numeric value");
   hrs=prompt("Enter number of hours worked","");
  }




Formula Shortcuts

Shortcuts have been devised for common calculation formulas. Increments of +1 and -1 are the most commonly used of these.

ShortcutEffect
x++x = x+1
x-x = x-1
x+ = yx = x+y
x- = yx = x-y
x* = yx = x*y
x/ = yx = x/y


Math rounding and random numbers

The Math.round() function will truncate a number. The Math.random() will create a random number. The Math function has various other items in it.

x = Math.random(); will place a random number in the variable x.

To actually round a number using the Math.round function, you add 0.5 and then use the function. x = Math.round(x+.5);

Miscellaneous

Comment lines
Comment lines in JavaScript are defined by two slashes at teh beginning.
//Script created by me



Lesson 3 of JavaScript

Return to main page