Programming Chapter 4

From Unify Community Wiki
Revision as of 13:11, 31 May 2008 by Lgoss007 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Enum-ination

Enumerations are custom data types with a predefined set of values. To create one:

<csharp> public enum Temperatures {

   Unknown,
   Freezing,
   Cold,
   Mild,
   Warm,
   Hot

}

public Temperatures temperature = Temperatures.Unknown; </csharp>

Here we've created a type called Temperatures, then create a variable and set it's initial value to Unknown from the Temperatures type. The value of our variable can only be one of the Temperatures.

Select Your Path

The if statement is similar to the ternary operator as it tests a condition, but instead of returning a result it executes the next statement if it evaluates to true (so if you use a block {} you can execute multiple statements). else if can be used to test more conditions before using an else which is a catch all to execute if all the if statements were false.

<csharp> float temperature = 39.0;

if(temperature > 60.0) {

  // temperature above 60
  Debug.Log("It is warm out.");

} else if(temperature > 32.0) {

  // temperature between 32 and 60
  Debug.Log("It is cold out.");

} else {

  // temperature below 32
  Debug.Log("It is freezing!");

} </csharp>

"if" works in sequence. So the first if will be checked to see if it evaluates to true, and if true it goes into the block statement following the if while the rest of the statements are ignored. So in this case the first statement evaluates to false as temperature is less than 60, so it skips to the next statement. The second statement "else if..." evaluates to true, so the phrase "It is cold out." will be put in the log. The third statement is ignored as we've already got a statement that evaluated to true.

Iterate Iterate Iterate...

The For loop is an iteration loop that allows you to repeat a statement over and over a certain number of times.

<csharp> int sum = 0;

for(int i=0; i<10; i++) {

  sum += i;

} </csharp>

At the end of this loop the value of sum will be 45 (0+1+2+3+4+5+6+7+8+9).

There are three statements required by a for loop (reminder, statements are separated by a semi-colon):

  • The initial value, in this case it is i=0 (you can initialize variable earlier).
  • The condition to continue the loop, in this case while i<10 we continue.
  • The step the index takes, in this case increase i by 1 i++ (you can also do decrease).

But be careful of a never ending loop (it just keeps going, and going, and going...) <csharp> int i;

// i will always be less than 10 (theoretically), since we just keep decreasing it's value for(i=0; i<10; i--) {

  Debug.Log("This is the song that never ends, it just goes on and on my friend...");

} </csharp>

Jump n'

There are two main jump statements:

  • continue: jump back to the control statement.
  • break: break out of the control statement.

<csharp> int sum = 0; int i; for(i=0; i<20; i++) {

  // if i is 5 then don't add to sum
  if(i>4 && i<6) continue;
  
  // if i is 9 quit this loop
  if(i>9) break;
  sum += i;

}

Debug.Log(sum.ToString()); </csharp>

In this example sum will be 40 (0+1+2+3+4+6+7+8+9) since we skipped 5 and quit when i got to 10.

Example-4

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox