Programming Chapter 2

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Should always be JavaScript, never JScript)
Line 1: Line 1:
 
Author: Lucas Goss
 
Author: Lucas Goss
 +
 +
== An Array of Information ==
 +
 +
An Array is simply a static list of variables, meaning it's a fixed size and doesn't change automatically. If you have an array of size 5, it stays at size 5 unless you create a new one. So you can't add items dynamically (that's a dynamic list). An array is declared with square brackets '[]' and the index is 0 based (meaning 0 is the first number, not 1). So a size of 5 would be numbers 0 to 4.
 +
 +
====JScript - Array ====
 +
<javascript>
 +
var weapons : String[] = new String[5]; // Declare an array of 5 strings
 +
weapons[0] = "Sword";                  // Set item 0 to Sword
 +
weapons[3] = "Knife";                  // Set item 3 to Knife
 +
weapons[5] = "Gun";                    // Error: invalid index (only 0-4 exists)
 +
 +
weapons = new String[6];                // Create a new array of size 6
 +
 +
var weapon1 = weapons[0];              // weapon1 will be "" not "Sword" because of new list
 +
weapons[5] = "Gun";                    // OK now, since the size is 6
 +
</javascript>
 +
 +
====C# - Array ====
 +
<csharp>
 +
string[] weapons = new string[5]; // Declare an array of 5 strings
 +
weapons[0] = "Sword";            // Set item 0 to Sword
 +
weapons[3] = "Knife";            // Set item 3 to Knife
 +
weapons[5] = "Gun";              // Error: invalid index (only 0-4 exists)
 +
 +
weapons = new string[6];          // Create a new array of size 6
 +
 +
string weapon1 = weapons[0];      // weapon1 will be "" not "Sword" because of new list
 +
weapons[5] = "Gun";              // OK now, since the size is 6
 +
</csharp>
 +
So you probably noticed a '''new''' keyword, it's an operator just like the '''[]'''s are.
  
 
== Call the Operator ==
 
== Call the Operator ==
Line 9: Line 40:
 
var playerName;
 
var playerName;
 
var playerLives;
 
var playerLives;
 +
var playerStrength;
 
var isPlayerAlive;
 
var isPlayerAlive;
 +
var weapons : String = new String[5];
 +
var message = "Hello World!";
 
</javascript>
 
</javascript>
  
Line 16: Line 50:
 
string playerName;
 
string playerName;
 
int playerLives;
 
int playerLives;
 +
float playerStrength;
 
bool isPlayerAlive;
 
bool isPlayerAlive;
 +
string[] weapons = new string[5];
 +
string message = "Hello World!";
 
</csharp>
 
</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.
 
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 - Index Operator====
 +
<javascript>
 +
weapons[0] = "Sword";    // Set item 0 to Sword
 +
playerName = weapons[0]; // playerName to item 0
 +
</javascript>
 +
The index operator "[]", which you just saw earlier, is used to get a specified index in an array.
 +
 +
====Code - New Operator====
 +
<javascript>
 +
playerLives = new int(); // playerLives = 0;
 +
</javascript>
 +
The 'new' operator creates objects and invokes constructors. What? We'll explain this later.
 +
 +
====Code - Cast Operator====
 +
<javascript>
 +
playerStrength = 2.51;            // use "2.51f" in C#
 +
playerLives = (int)playerStrength; // don't need to cast in JScript
 +
</javascript>
 +
A cast simply converts one type of object to another. However, a conversion operator must be defined (which it is for most basic types). This is a straight conversion, so you may lose information if casting to a different type.  C# will error if you don't have an "f" on the end of 2.51, because by default all decimal numbers are double. The f suffix is a shorthand cast to a float. JScript does implicit casting, meaning it will automatically cast to whatever type the object is. C# requires more explicit casting (you have to do it yourself). There are highs and lows to both, as sometimes you can lose information in JScript and not realize it (like if a float is casted to an int and back to a float).
 +
 
====Code - Arithmetic Operators====
 
====Code - Arithmetic Operators====
 
<javascript>
 
<javascript>
Line 28: Line 85:
 
playerLives = 3 % 2;  // modulus (division remainder)
 
playerLives = 3 % 2;  // modulus (division remainder)
 
</javascript>
 
</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:
+
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.
 
====Code - Parenthesis====
 
====Code - Parenthesis====
 
<javascript>
 
<javascript>
 
playerLives = 3 * (3 + 2 - (1 + 1));
 
playerLives = 3 * (3 + 2 - (1 + 1));
 
</javascript>
 
</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).
+
You can also use parenthesis to specify order. 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====
 
====Code - Arithmetic Assignment Operators====
 
<javascript>
 
<javascript>
Line 41: Line 97:
 
playerLives -= 1; // Same as "playerLives = playerLives - 1;"
 
playerLives -= 1; // Same as "playerLives = playerLives - 1;"
 
</javascript>
 
</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.
+
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. This code 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====
 
====Code - Increment/Decrement Operators====
 
<javascript>
 
<javascript>
Line 49: Line 104:
 
playerLives--;  // oh, but now he lost a life
 
playerLives--;  // oh, but now he lost a life
 
</javascript>
 
</javascript>
 +
The '++' and '--' are increment and decrement operators, that increment and decrement a variable by 1.
  
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====
 
====Code - Logical Operators====
 
<javascript>
 
<javascript>
Line 63: Line 118:
 
isPlayerAlive = !true;          // = false
 
isPlayerAlive = !true;          // = false
 
</javascript>
 
</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 (!). The code above shows some truth tables using the logical operators.
  
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====
 
====Code - Relational Operators====
 
<javascript>
 
<javascript>
Line 73: Line 128:
 
isPlayerAlive = !(playerLives == 0);
 
isPlayerAlive = !(playerLives == 0);
 
</javascript>
 
</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 (>=).
  
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====
 
====Code - Ternary Operator====
 
<javascript>
 
<javascript>
Line 80: Line 135:
 
playerName = isPlayerAlive ? "LivingMan" : "DeadMan";
 
playerName = isPlayerAlive ? "LivingMan" : "DeadMan";
 
</javascript>
 
</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". If the expression in the code above "isPlayerAlive" is true, then playerName will be "LivingMan".
  
The '+' and '+=' operators are also used as shorthand for concatenation of strings.
 
 
====Code - Concat Operators====
 
====Code - Concat Operators====
 
<javascript>
 
<javascript>
Line 87: Line 142:
 
playerName += "One";              // = SuperPlayerOne
 
playerName += "One";              // = SuperPlayerOne
 
</javascript>
 
</javascript>
 +
The '+' and '+=' operators are also used as shorthand for concatenation of strings.
  
'''TODO: index operator []'''
+
====Code - Member Access Operator====
'''TODO: cast operator ()'''
+
 
+
Another common operator is 'new'.
+
====Code - New Operator====
+
 
<javascript>
 
<javascript>
playerLives = new int(); // playerLives = 0;
+
Debug.Log(message);
 +
guiText.text = message;
 
</javascript>
 
</javascript>
The 'new' operator creates objects and invokes constructors.
+
Remember those? If you did the chapter one example, "Example-1", they may be familiar to you. The member access operator is used to access the public members of an object.
 
+
'''TODO: member access operator .'''
+
  
 
== I Object ==
 
== I Object ==
 +
 +
'''TODO'''
 +
 +
== Scope Modifiers ==
 +
 +
'''TODO '''
 +
 +
== Are we Functional? ==
 +
 +
'''TODO'''
  
 
----
 
----
 
[[Programming|Programming Index]] : [[Programming_Chapter_1|Previous Chapter]] : Next Chapter
 
[[Programming|Programming Index]] : [[Programming_Chapter_1|Previous Chapter]] : Next Chapter

Revision as of 15:54, 15 June 2007

Author: Lucas Goss

Contents

An Array of Information

An Array is simply a static list of variables, meaning it's a fixed size and doesn't change automatically. If you have an array of size 5, it stays at size 5 unless you create a new one. So you can't add items dynamically (that's a dynamic list). An array is declared with square brackets '[]' and the index is 0 based (meaning 0 is the first number, not 1). So a size of 5 would be numbers 0 to 4.

JScript - Array

<javascript> var weapons : String[] = new String[5]; // Declare an array of 5 strings weapons[0] = "Sword"; // Set item 0 to Sword weapons[3] = "Knife"; // Set item 3 to Knife weapons[5] = "Gun"; // Error: invalid index (only 0-4 exists)

weapons = new String[6]; // Create a new array of size 6

var weapon1 = weapons[0]; // weapon1 will be "" not "Sword" because of new list weapons[5] = "Gun"; // OK now, since the size is 6 </javascript>

C# - Array

<csharp> string[] weapons = new string[5]; // Declare an array of 5 strings weapons[0] = "Sword"; // Set item 0 to Sword weapons[3] = "Knife"; // Set item 3 to Knife weapons[5] = "Gun"; // Error: invalid index (only 0-4 exists)

weapons = new string[6]; // Create a new array of size 6

string weapon1 = weapons[0]; // weapon1 will be "" not "Sword" because of new list weapons[5] = "Gun"; // OK now, since the size is 6 </csharp> So you probably noticed a new keyword, it's an operator just like the []s are.

Call the Operator

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

JavaScript Variables

<javascript> var playerName; var playerLives; var playerStrength; var isPlayerAlive; var weapons : String = new String[5]; var message = "Hello World!"; </javascript>

C# Variables

<csharp> string playerName; int playerLives; float playerStrength; bool isPlayerAlive; string[] weapons = new string[5]; string message = "Hello World!"; </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 - Index Operator

<javascript> weapons[0] = "Sword"; // Set item 0 to Sword playerName = weapons[0]; // playerName to item 0 </javascript> The index operator "[]", which you just saw earlier, is used to get a specified index in an array.

Code - New Operator

<javascript> playerLives = new int(); // playerLives = 0; </javascript> The 'new' operator creates objects and invokes constructors. What? We'll explain this later.

Code - Cast Operator

<javascript> playerStrength = 2.51; // use "2.51f" in C# playerLives = (int)playerStrength; // don't need to cast in JScript </javascript> A cast simply converts one type of object to another. However, a conversion operator must be defined (which it is for most basic types). This is a straight conversion, so you may lose information if casting to a different type. C# will error if you don't have an "f" on the end of 2.51, because by default all decimal numbers are double. The f suffix is a shorthand cast to a float. JScript does implicit casting, meaning it will automatically cast to whatever type the object is. C# requires more explicit casting (you have to do it yourself). There are highs and lows to both, as sometimes you can lose information in JScript and not realize it (like if a float is casted to an int and back to a float).

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.

Code - Parenthesis

<javascript> playerLives = 3 * (3 + 2 - (1 + 1)); </javascript> You can also use parenthesis to specify order. 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).

Code - Arithmetic Assignment Operators

<javascript> playerLives += 1; // Same as "playerLives = playerLives + 1;" playerLives -= 1; // Same as "playerLives = playerLives - 1;" </javascript> 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. This code 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.

Code - Increment/Decrement Operators

<javascript> playerLives++; // player got an extra life playerLives--; // oh, but now he lost a life </javascript> The '++' and '--' are increment and decrement operators, that increment and decrement a variable by 1.

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> 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 (!). The code above shows some truth tables using the logical operators.

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> 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 - Ternary Operator

<javascript> // If player is alive, name will be LivingMan, otherwise DeadMan playerName = isPlayerAlive ? "LivingMan" : "DeadMan"; </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". If the expression in the code above "isPlayerAlive" is true, then playerName will be "LivingMan".

Code - Concat Operators

<javascript> playerName = "Super" + "Player"; // = SuperPlayer playerName += "One"; // = SuperPlayerOne </javascript> The '+' and '+=' operators are also used as shorthand for concatenation of strings.

Code - Member Access Operator

<javascript> Debug.Log(message); guiText.text = message; </javascript> Remember those? If you did the chapter one example, "Example-1", they may be familiar to you. The member access operator is used to access the public members of an object.

I Object

TODO

Scope Modifiers

TODO

Are we Functional?

TODO


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox