The IF Statement - 04if.fla

This program builds on the Maths example and validates the input using IF statements.

  • Input -
        Digits typed by the user
        Button press to trigger the processing
  • Process -
        Test for non-numeric input and if necessary generate an error message.
        Test for negative input and if necessary generate an error message.
        If no errors, add the input numbers
  • Output -
        Display the error message or the sum of the numbers if there were no input errors.

Start up Macromedia Flash

  1. Start up Flash.
  2. Make sure you have completed lesson 3.
  3. Open 03maths.fla and re-save it as 04if.fla

This exercise adds to the maths exercise to validate the input data.

 

Modify the code that handles the "Button Release" event.

This code ensures that the first number you enter is really a number and also that it is positive. First you test for error conditions and if there are none, you carry out the intended action.

Here is the code.


on (release)
{
  // Test for non numeric input (Is Not a Number isNaN)
  if (isNaN(_root.inputOne))
  {
    _root.outputText = "The first number you entered is not valid.";	
  }
  else
  // Test for negative input
  if (_root.inputOne < 0)
  {
    _root.outputText = "The first number you entered must be positive.";	
  }
  else
  // Carry out the addition (only if there were no errors).
  {
    _root.outputText = _root.inputOne - - _root.inputTwo;
  }
}

 

Here is the finished program.

You can download the source code.

Test this program by typing text instead of numbers.

To run this again, refresh your page.
(With Internet Explorer, press F5)

Task

  1. Test the second number in the same way as the first.
  2. Adjust this program to divide numbers and validate the input. Ensure the input is numeric and prevent division by zero.

 

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();
}

 

 

 

 

© C N Bauers (2004) - You may use these materials for your self education. Educational institutions may use these materials too.