// ifControlStatement.jsx // Copyright 2007 // Written by Jeffrey Tranberry // Photoshop for Geeks Version 1.0 /* Description: 'If' control statements using Comparison and Logical operators */ // enable double clicking from the // Macintosh Finder or the Windows Explorer #target photoshop // Make Photoshop the frontmost application // in case we double clicked the file app.bringToFront(); /////////////////////// // MAIN // ////////////////////// // Using Comparison operators: // == (equal to) // < (less than) // > (greater than) // <= (less than or equal to) // >=(greater than or equal to) // Create a variable. // In this case we store a number. var x = 10; // An 'if' control statement, boolean test. // Use the == operator to see if the numbers are equal. // In this case it is false so they are equal. if (x == 4) { alert ('x is equal to 4'); } else { alert ('x is not equal to 4'); } // An 'if' control statement, boolean test. // Use the < operator to see if the number is less than. // In this case it is false so the variable is greater than 4. if (x < 4) { alert ('x is less than 4'); } else { alert ('x is greater than 4'); } // Two variables to compare. // In this case, we're evaluating strings var name1 = "Jeffrey Tranberry"; var name2 = "Jeffrey Tranberry"; // An 'if' control statement, boolean test. // Use the == operator to see if the strings are equal. // In this case it is true so they are equal. if (name1 == name2){ alert ( 'name1 and name2 are the same.'); } else { alert ( 'name1 and name2 are not the same.'); } // Two more variables to compare. var name1 = "Jeffrey Tranberry"; var name2 = "Jeffrey Transberry"; // 'if' control statement, boolean test. // Use the == operator to see if the strings are equal. // In this case it is false (because there is a superfluous 's' in the word 'Tranberry' in string2) so they are **not** equal if (name1 == name2){ alert ( 'name1 and name2 are the same.'); } else { alert ( 'name1 and name2 are not the same.'); } // Using Logical operators // && (and) // || (or) // ! (not) // Two variables to evaluate. var sailBoat = false; var wind = false; // Check to see if you have a sail boat *and* wind if ( sailBoat == true && wind == true) { alert ("You have wind in your sails."); // else if tests for a different condition // In this case, see if we have a sail boat *and* no wind. } else if (sailBoat == true && wind == false) { alert ("You're stuck without wind. Better sart rowing."); // Finally, if neither condition is met } else { alert ("You don't have a sail boat or wind. You must be stuck on an island."); }