Lesson 7: If Statement and Comparison Operators - Pace University New York

JavaScript 101 | 7- 1

Lesson 7: If Statement and Comparison Operators

OBJECTIVES: In this lesson you will learn about

Branching or conditional satements

How to use the comparison operators: ==, !=, < , >=

How to use if and if ¡­ else to evaluate conditions and make decisions

Copyright ? August 2002 Pace University

7-2| Lesson 7: If statement and comparison operators

Preparing to Program

It is often useful to take a course of action depending on some circumstance. It does not

always rain, so you do not always take an umbrella with you. But if it is raining, or is

expected to rain soon, you would be wise to take your umbrella.

Programs and Web pages are often faced with a similar situation. For example, we have

already discussed the fact that a Web page with JavaScript works differently depending on

whether you are using Netscape or Internet Explorer. So it is useful for a Web page to

determine if you are using Netscape or Internet Explorer, and to execute different code

depending on the result.

In programming, code that asks a question and executes different paths depending on the

answer is known as a branching or conditional statement. Picture in your mind a branch in

the shape of a fork: you can go either left or right. The term conditional statement implies

that execution depends on a condition. If the condition has a certain value, do this; if it does

not do something else.

This lesson will teach you how to use the if and if .. else statement. The if and if ¡­ else

statements are examples of a conditional statement. Conditional statements work in the

following way: they ask a question, then execute certain code depending on the answer. In

JavaScript, and in most other programming languages, conditional statements ask a question

by using comparison operators. Before we discuss the syntax of the if statement, we need to

explore the topic of comparison operators.

Comparison operators

Comparison operators are used to make comparisons. For example you can compare two

variables to test if they are equal. Other comparisons are available (see the table below). For

each of the comparison operators, the result of the comparison is always either true or false.

True or false values are known as boolean values.

Copyright ? August 2002 Pace University

JavaScript 101 | 7- 3

Here is a table that describes the comparison operators available to you in JavaScript:

Meaning

Comments

Operator

==

equal

True if the two operands are equal; false

otherwise.

!=

not equal

True if the operands are not equal; false

otherwise.

<

less than

True if the left operand is less than the right

operand; false otherwise.

greater than

True if the left operand is greater than the

right operand; false otherwise.

>=

greater than

or equal to

True if the left operand is greater than or

equal to the right operand; false otherwise.

These operators should already be familiar to you. You have used them in math class to make

comparisons.

Here are some simple examples that demonstrate how to use comparison operators:

Assume: a = 7, b = 4 and c= 10.

a < b is false.

a < b+c is true.

Assume: name = "Pace",

state = "NY",

address = "1 Pace Plaza"

a*b >= 2*c is true

name address is true

a+b+c == 21 is true

state == address is false

Copyright ? August 2002 Pace University

7-4| Lesson 7: If statement and comparison operators

The if Statement

The if statement is an example of a conditional or branching statement. The if statement

works in the following way: it asks a question, normally by using comparison operators.

Depending on the answer, it will execute certain code.

Here is the general syntax for the if statement:

if ( condition )

{

JavaScript statements go here

}

If the condition is true, the statements between { ... } will be executed. If the condition is

false, the statements between { ... } will be skipped. The group of statements between { ... }

is called the if block. Any statements may be placed in this if block, including additional if

statements.

Recall that you used curly braces { and } to define the boundaries for a function in the last

lesson. Notice also, that the condition can only be true or false. In programming, you can only

ask yes/no or true/false questions. If you ever played the game 20 Questions, you know it can

be quite a challenge to figure out something by only using yes/no questions. This is one of the

restrictions that can make programming a difficult task.

Example

if (city == "New York")

{

state = "NY"

areacode = "212"

document.write(city,state)

}

If the condition city == "New York" is true, all of the statements in the if block will be

executed. If the city does not match "New York," none of the if block's statements will be

executed.

Copyright ? August 2002 Pace University

JavaScript 101 | 7- 5

The if ¡­ else Statement

An if statement can also have an else clause that executes if the condtion is false. This

enables a two-way branch so that one block of statements can be executed when the condition

is true, and a second block of statements can be executed if the condition is false.

Syntax for if ... else:

if (condition)

{

statementgroup1

}

else

{

statementgroup2

}

If the condition is true, statementgroup1 (the if block) will be executed and statementgroup2

will not be executed; otherwise (when the condition is false), statementgroup1 will not be

executed and statementgroup2 (the else block) will be executed.

Example

if (city == "New York")

{

state = "NY";

areacode = "212";

}

else

{

state = "CT";

areacode = "203";

}

document.write(city,state);

In this example, if city has the value "New York," then the two statements in the if block will

be executed and the statements in the else block will not. If city does not have the value

Copyright ? August 2002 Pace University

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download