Lesson 5: JavaScript Loops

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

WHILE loop

The While loop is performed as long as a certain condition remains true. While loops can be very useful as verification checks on inputted values. Programmers must be careful to be sure that the control condition that breaks the loop WILL occur sometime or the loop will become an 'infinite loop' and continue until the system crashes.

A While loop checks to see if the condition is true before it doesw any of the operations within the curly brackets associated with it.

while(Boolean expression)
  {
   statement;
   statement;
     etc.
  }
while((hrs<0)||(hrs>60))
  {
   alert("Number entered is outside range of possible answers");
   hrs=prompt("Enter hours worked this week","");
  }

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","");
  }


DO WHILE loop

The Do-While loop is similar to the While loop with one important difference: the condition is checked at the END of the operation rather than the beginning. So a Do-While loop will always run its associated statements at least once, even if the condition is false before the loop is reached.

do
  {
   statement;
   statement;
     etc.
  }while(Boolean expression)
do
  {
   alert("Please be sure the number entered is within the range of 1-60");
   hrs=prompt("Enter hours worked this week","");
  }while((hrs<0)||(hrs>60))

FOR loops

A For loop repeats for a set number of times. It has a built-in variable, which experienced programmers tend to use i,j, and k for. The variable is automatically incremented each loop. The default increment is +1, but the programmer can set it at any value, including negative increments which will let it count DOWN. Even if there are no statements within the loop, the computer will use up computing time going through the loops and thereby slow the speed. This trick has sometimes been used to make a page "pause" for a few seconds before moving on to another action.

for(initial; loop condition; increment)
  {
   statement;
   statement;
     etc.
  }
for(i=1;i<=20;i=i+1)
  {
   document.write(i+"<br>");
  }
for(i=3;i<=21;i=i+3)
  {
   document.write(i+"<br>");
  }


Lesson 6 in JavaScript
Return to main page