Clip Art if statements
Pierce Home
Computer Science 555 Website Development using JavaScript and AJAX - 3 Units
Section:
3179
Instructor:
Brad Gilbert
Pierce Mailbox:
612
Email:
pierce@wavethunder.com
Wednesday Office Hours:
5:00 -   5:40 PM COSC 1507
Wednesday Lab:
5:45 -   7:50 PM COSC 1507
Wednesday Lecture:
7:55 - 10:00 PM MATH 1511
Other classes:
CS-575
if statements
else if · else · conditional « More Info
Question:

What is an if statement?

Answer:

An if statement is a method for your code to decide to perform, or not, certain statements. For example: If customer is a senior citizen, discount the price.

Question:

How do I write an if statement?

Answer:

The syntax for writing an if statement is the keyword if, followed by parentheses, followed by open and close curly-braces. Inside the parentheses is an expression that evaluates to true or false - basically: zero is false, and anything else is true.

    if (true)
    {
        // insert code here
    }
    

There is no semicolon ; after the parentheses or closing curly-brace. Putting a ; after the parentheses will break the if statement. The reason is because a ; after the parentheses would detach the code inside the curly-braces from if statement - in other words, the ; would signify the end of the if statement, without including the curly braces.

Question:

How does an if statement work?

Answer:

If the expression inside the parentheses is true, the code inside the curly-braces will execute. If false, the code inside the curly-braces will not execute. In either case, all code after the closing curly-brace will execute.

Question:

How do I write a true/false expression to place inside the parentheses?

Answer:

Use the comparison operators:

    ==   !=   <   >   <=   >=
    

Example:

    // discount price by $2 for senior citizen
    if (CustomerAge >= 55)
    {
        Price -= 2.00;
    }
    document.write("This code will always execute.");
    
Question:

Can I write an if statement inside another if statement?

Answer:

Yes, that is called nesting. The inner if statement is a nested if statement. For example:

    // discount price by $2 for senior citizen
    if (CustomerAge >= 55)
    {
        Price -= 2.00;
        // discount price by $2.50 is customer also has a coupon
        if (HasCoupon == true)
        {
            Price -= 2.50;
        }
    }
    
Caution:

BE VERY CAREFUL TO USE == AND NOT =.

Question:

Can I nest more than one if statement?

Answer:

Yes, you can have multiple levels of nesting, as well as multiple nested if statements. For example:

    // discount price by $2 for senior citizen
    if (CustomerAge >= 55)
    {
        Price -= 2.00;
        // discount price by $2.50 is customer also has a coupon
        if (HasCoupon == true)
        {
            Price -= 2.50;
        }
        // discount price by $3 if customer is club member
        if (ClubMember == true)
        {
            Price -= 3.00;
            // discount price additional $2 if Silver membership
            if (SilverMember == true)
            {
                Price -= 2.00;
            }
            // discount price additional $3 if Gold membership
            if (GoldMember == true)
            {
                Price -= 3.00;
            }
        }
    }
    

Next if: else if »

Brad Gilbert · Fall 2011
Computer Science · Pierce College · Woodland Hills CA · 818-719-6401