Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false. The result of the comparison can be used to control the flow of the program.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Comparison Operators

Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false.

operatorcomparison
>greater than
<less than
==equal to
!=not equal to
<=less than or equal to
>=greater than or equal to

The result of the comparison can be used to control the flow of the program. The if/else structure is the most commonly used control structure.

var nPrice = 29.00;
var nHighest = 32.00;

if(nPrice < nHighest)
{
  document.write("buy it!");
}
else
{
  document.write("don\'t buy it.");
}

The code shown above would print "buy it." because the value of nPrice is lower than the value of nHighest. The comparison operator (<) returns true, and the code inside the if block is executed.

If the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop down to the else statement. The program would print "don't buy it."

The if structure is frequently used without the else block as shown below. In this case, if the value of nPrice was higher than the value of nHighest, the result of the comparison would be false and the program flow would drop through, not printing anything.

var nPrice = 29.00;
var nHighest = 32.00;

if(nPrice < nHighest)
{
  document.write("buy it!");
}

Another control structure is the do/while loop as shown below.

var nPrice = 64.00;
var nHighest = 32.00;

do
{
  --nPrice;
} while (nPrice > nHighest);

This structure loops through the code inside the curly brackets, which decrements the value of nPrice on each loop. Then it performs the comparison in side the brackets following the while statement to see if the value of nPrice is greater than the value of nHighest. If the result of the comparison is true, the program executes the statement inside the brackets, decrementing the value of nPrice again. This continues until the result of the comparison is false. Then the program flow breaks out of the loop.

Comparison operators compare the values of two variables or statements and return the result of the comparison as either true or false. The result of the comparison can be used to control the flow of the program.

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268