Programming Chapter 2

From Unify Community Wiki
Revision as of 17:32, 15 June 2007 by KeliHlodversson (Talk | contribs)

Jump to: navigation, search

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

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

JavaScript Variables

<javascript> var playerName; var playerLives; var playerStrength; var isPlayerAlive; var weapons = 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


Scope Modifiers


Are we Functional?



Things we learned:

  • An Array is a list of items, declared and indexed with []
  • Operators
    • Index: [index] where index is the 0 based number of an item
    • New: new creates a new object and invokes it's constructor
    • Cast: (type) where the object has a conversion operator for type
    • Arithmetic (and Arithmetic Assignment)
      • Addition: + (+=)
      • Subtraction: - (-=)
      • Multiplication: * (*=)
      • Division: / (/=)
      • Modulus: % (%=)
    • Increment: ++, increment by one
    • Decrement: --, decrement by one
    • Logical
      • And: &&
      • Or: ||
      • Not: !
    • Relational
      • Equal: ==
      • Not Equal: !=
      • Less Than: <
      • Greater Than: >
      • Less Than or Equal: <=
      • Greater Than or Equal: >=
    • Ternary: ?
    • Concatenation: +, for strings
    • Member Access: .



Programming Index : Previous Chapter : Next Chapter

Personal tools