Programming Chapter 2

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
(Should always be JavaScript, never JScript)
Line 5: Line 5:
 
'''Note:''' The examples in this section use the following variables:
 
'''Note:''' The examples in this section use the following variables:
  
====JScript Variables====
+
====JavaScript Variables====
 
<javascript>
 
<javascript>
 
var playerName;
 
var playerName;

Revision as of 10:03, 14 June 2007

Author: Lucas Goss

Contents

Call the Operator

Note: The examples in this section use the following variables:

JavaScript Variables

<javascript> var playerName; var playerLives; var isPlayerAlive; </javascript>

C# Variables

<csharp> string playerName; int playerLives; bool isPlayerAlive; </csharp>

An operator is a symbol used to operate on constants and variables. We already learned one operator, the assignment operator, but there are many others. I won't cover them all (use the language references), but a few common ones.

Code - Arithmetic Operators

<javascript> playerLives = 3 + 2; // addition playerLives = 3 - 2; // subtraction playerLives = 3 * 2; // multiplication playerLives = 3 / 2; // division playerLives = 3 % 2; // modulus (division remainder) </javascript> Those are the arithmetic operators (+, -, *, /, %). The arithmetic operators are operated on first because there is an order of operation, called the operator precedence. The assign operator has the lowest precedence, so it always goes last. So for example, the first thing the compiler does with the statement 'playerLives = 3 + 2', is add the 3 and 2 together, then that value is assigned to playerLives. You can also use parenthesis to specify order:

Code - Parenthesis

<javascript> playerLives = 3 * (3 + 2 - (1 + 1)); </javascript> For every open parenthesis there must be a closing one as well, and they work just like they do in mathematics (the inner most parenthesis is executed first, then the next, and so on).

We've already covered the assignment operator, but there are arithmetic assignment operators as well (+=, -=, *=, /=, %=). They are a shorthand way to do an arithmetic operation on the current value. For example:

Code - Arithmetic Assignment Operators

<javascript> playerLives += 1; // Same as "playerLives = playerLives + 1;" playerLives -= 1; // Same as "playerLives = playerLives - 1;" </javascript> This takes the current value of playerLives and in the first case adds one to it. The second case subtracts one from playerLives. The others work in a similar manner.

The '++' and '--' are increment and decrement operators, that increment and decrement a variable by 1.

Code - Increment/Decrement Operators

<javascript> playerLives++; // player got an extra life playerLives--; // oh, but now he lost a life </javascript>

Conditional operators are a boolean type of operation. They're really important for conditional statements which we'll cover later. First there is the logical operators, AND (&&), OR (||) and NOT (!). Here's some truth tables using the logical operators:

Code - Logical Operators

<javascript> isPlayerAlive = true && true; // = true isPlayerAlive = true && false; // = false isPlayerAlive = false && false; // = false

isPlayerAlive = true || true; // = true isPlayerAlive = true || false; // = true isPlayerAlive = false || false; // = false

isPlayerAlive = !true; // = false </javascript>

Another type of conditional operators are relational operators. These are EQUAL (==), NOT EQUAL (!=), LESS THAN (<), GREATER THAN(>), LESS THAN OR EQUAL (<=), and GREATER THAN OR EQUAL (>=):

Code - Relational Operators

<javascript> // Different ways to say player is still alive if they have lives left isPlayerAlive = playerLives > 0; isPlayerAlive = playerLives >= 1; isPlayerAlive = playerLives != 0; isPlayerAlive = !(playerLives == 0); </javascript>

And a third type of conditional operator is the ternary operator (?). It returns the first expression if true and returns the second expression if false as in "condition ? 1st expression : 2nd expression". For example:

Code - Ternary Operator

<javascript> // If player is alive, name will be LivingMan, otherwise DeadMan playerName = isPlayerAlive ? "LivingMan" : "DeadMan"; </javascript>

The '+' and '+=' operators are also used as shorthand for concatenation of strings.

Code - Concat Operators

<javascript> playerName = "Super" + "Player"; // = SuperPlayer playerName += "One"; // = SuperPlayerOne </javascript>

TODO: index operator [] TODO: cast operator ()

Another common operator is 'new'.

Code - New Operator

<javascript> playerLives = new int(); // playerLives = 0; </javascript> The 'new' operator creates objects and invokes constructors.

TODO: member access operator .

I Object


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox