Programming Chapter 2
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
weapons = new String; // Create a new array of size 6
C# - Array
<csharp> string weapons = new string; // Declare an array of 5 strings weapons = "Sword"; // Set item 0 to Sword weapons = "Knife"; // Set item 3 to Knife weapons = "Gun"; // Error: invalid index (only 0-4 exists)
weapons = new string; // Create a new array of size 6
string weapon1 = weapons; // weapon1 will be "" not "Sword" because of new list weapons = "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:
<csharp> string playerName; int playerLives; float playerStrength; bool isPlayerAlive; string weapons = new string; 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
Code - New Operator
Code - Cast Operator
Code - Arithmetic Operators
Code - Parenthesis
Code - Arithmetic Assignment Operators
Code - Increment/Decrement Operators
Code - Logical Operators
isPlayerAlive = true || true; // = true isPlayerAlive = true || false; // = true isPlayerAlive = false || false; // = false
Code - Relational Operators
Code - Ternary Operator
Code - Concat Operators
Code - Member Access Operator
Are we Functional?
Things we learned:
- An Array is a list of items, declared and indexed with 
- 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
- And: &&
- Or: ||
- Not: !
- Equal: ==
- Not Equal: !=
- Less Than: <
- Greater Than: >
- Less Than or Equal: <=
- Greater Than or Equal: >=
- Ternary: ?
- Concatenation: +, for strings
- Member Access: .