3 Replies Latest reply on Sep 14, 2006 9:34 AM by martincou

    if Statement

    martincou Level 3
      Hi everyone,

      I just buy a book to help me understand more advanced AS 2.0 concept and I have a quick question about if statement. Generally when you use if, you have to use a comparaison operator in the parameter. Why in the below exemple they didn’t? Someone knows why? If I use one, the function don’t work properly. I just don’t understand why… Thanks for your help!

      var tvPower:Boolean = false;

      function togglePower() {
      var newChannel:Number;
      if (tvPower) {
      changeTheChannel(0);
      tvPower = false;
      } else {
      changeTheChannel(1);
      tvPower = true;
      }
      }
        • 1. Re: if Statement
          Level 7
          It's just shorthand for a Boolean conditional statement. It could also be
          written:

          if (tvPower==true) {
          changeTheChannel(0);
          tvPower = false;
          } else {
          changeTheChannel(1);
          tvPower = true;
          }
          }


          --
          -------------------------------
          Remove '_spamkiller_' to mail
          -------------------------------


          • 2. Re: if Statement
            Rothrock Level 5
            Because in the end it boils down to:

            if (true) { do something } else { do something else}

            So the stuff inside the parenthesis is evaluated and turned into either a true or false. If the variable tvPower (or any variable) is a boolean (true or false) you can just use it as is.

            It is just a nice little shorthand trick.
            • 3. Re: if Statement
              martincou Level 3
              Thanks a lot for your quick answer !