Chapter 4 Example 7

by actionscriptbible
♥0 | Line 19 | Modified 2009-08-14 06:52:47 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lF7Y
 */

package {
  import com.actionscriptbible.Example;
  
  public class ch4ex7 extends Example {
    public function ch4ex7() {
      var array:Array = ["hello", "world"];
      var obj:Object;
      //Pull a value off the front of the array, set obj to that value,
      //and check if obj converts to true or false. Stop the loop when
      //the value converts to false. Null is returned by shift() when
      //there are no more values in the array. Null converts to false. 
      while (obj = array.shift()) {
        trace(obj);
      }
      //hello
      //world
      
      var str:String = "";
      //Set a default string if the string doesn’t exist or is empty.
      //Both empty strings and null values convert to false.
      //Negating false gives you true so the conditional runs if
      //the string is empty, giving it a default value instead.
      if (!str) str = "Default";
      trace(str); //Default
      
      for (var i:int = 1; i < 4; i++)
      {
        //Trace out the number (i) and whether it is odd or even.
        //i % 2 is the remainder when you divide it by two, always
        //a whole number. Any number converts to true, except 0
        //which converts to false. So when the remainder after
        //dividing by two is zero, the conditional is false.
        trace(i, (i % 2)? "odd" : "even");
      }
      //1 odd
      //2 even
      //3 odd
    }
  }
}