Programming Chapter 3 Old

From Unify Community Wiki
Jump to: navigation, search

Author: Lucas Goss

Contents

Game Time

What? I know we just started the chapter, but now we know enough about programming that we should be able to make games while we learn, which will hopefully make things more interesting. Now creating a game is no simple task, so we'll also go step by step. And since this is just a tutorial about programming, we'll make it a simple text game to make it as simple as possible.

TODO

Enum-ination

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

JavaScript - Enum

enum WeaponType{dagger = 0,sword = 1,hammer = 2};
 
var weaponType = WeaponType.sword;

C# - Enum

enum WeaponType { dagger = 0, sword = 1, hammer = 2 };
 
int weaponType = (int)WeaponType.sword;

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.

JavaScript - if

var 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!");
}

C# - if

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!");
}

"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.

JavaScript - For

var sum = 0;
 
for(var i=0; i<10; i++)
{
   sum += i;
}

C# - For

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

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...)

// i is already declared as one of the following
// var i : int; (javascript)
// int i; (c#)
 
// 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...");
}

Jump n'

There are two main jump statements:

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

JavaScript - Jump

var sum = 0;
var i : int;
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());

C# - Jump

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());

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-3


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools