04 - If Statement

Navigation - Site Root :: Tutorial Home
Basics - Hello World :: Text IO :: Calculator :: If Statement
Games - Mouse Tracker :: Ball Bounce :: Maze :: Pong :: Ajax Style Demo
Techniques - Container Objects

  1. Start up Flash CS3 and load your Calculator example.

    Save the fla file with a new file name such as 04If.fla

    This lesson checks the calculator input and generates sensible error messages if the user enters text or negative numbers instead of valid positive numbers. The error checking is incomplete so your task is to fix this.
     
  2. Modify the code in your calculator example to look like this.

    myButton.addEventListener(MouseEvent.CLICK, doMyCalculation);

    function doMyCalculation(e:MouseEvent):void
    {
        var myAnswer : Number;

        // Test for non numeric input (Is Not a Number isNaN)
        if (isNaN(Number(N1.text)))
        {
            myOutput.text = "The first number you entered is not valid.";
        }
        else
        // Test for negative input
        if (Number(N1.text) < 0)
        {
            myOutput.text = "The first number you entered must be positive.";
        }
        else
        // Carry out the addition (only if there were no errors).
        {
            myAnswer = Number(N1.text) + Number(N2.text);

            myOutput.text = String(myAnswer);
        }
    }

     
  3. You can download 04If.fla
     
  4. Here is the partially finished movie. Try entering text or negative numbers instead of normal positive numbers.



  5. TASK: Complete the code above so you get sensible error messages for both the input fields.

Notes

The comparison operators are ...

>

Greater Than

>=

Greater Than or Equal

<

Less Than

<=

Less Than or Equal

==

Equal (data types may differ)

===

Equal (data types also equal)

!=

Not Equal

<>

Not Equal

!==

Not Equal (data types are equal)

 

The logical operators are ...

&&

And

||

Or

!

Not

&

Bitwise And

|

Bitwise Or

~

Bitwise Not

^

Bitwise Exclusive Or

 

Examples

if (counter == 0)
{
  doThis();
}
if (counter == 3)
{
  doThis();
}
else if (counter == 2)
{
  doThat();
}
else
{
  doTheOther();
}