Programming Chapter 3 Old

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
(removed UnityScript code and marked for deletion)
 
Line 1: Line 1:
 +
{{delete}}
 +
Note: this page has been replaced by [[Programming Chapter 3]]
 +
 
Author: Lucas Goss
 
Author: Lucas Goss
  
Line 4: Line 7:
  
 
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.
 
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 ==
 
== Enum-ination ==
  
Enumerations are custom data types with a predefined set of values. To create one:
+
Enumerations are custom data types with a predefined set of values. To create one use the keyword '''enum''' followed by a name for the enumerator followed by a a list of strings between curly brackets separated by commons.
  
====JavaScript - Enum====
 
<syntaxhighlight lang="javascript">
 
enum WeaponType{dagger = 0,sword = 1,hammer = 2};
 
 
var weaponType = WeaponType.sword;
 
 
</syntaxhighlight>
 
 
====C# - Enum====
 
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
enum WeaponType { dagger = 0, sword = 1, hammer = 2 };
+
enum WeaponType { dagger, sword, hammer };
 
+
int weaponType = (int)WeaponType.sword;
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Select Your Path ==
+
== If Statement ==
  
 
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.
 
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====
 
<syntaxhighlight lang="javascript">
 
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!");
 
}
 
</syntaxhighlight>
 
 
====C# - if====
 
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
float temperature = 39.0;
 
float temperature = 39.0;
Line 74: Line 42:
 
"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.
 
"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... ==
+
== For Loop ==
  
 
The For loop is an iteration loop that allows you to repeat a statement over and over a certain number of times.
 
The For loop is an iteration loop that allows you to repeat a statement over and over a certain number of times.
  
====JavaScript - For====
 
<syntaxhighlight lang="javascript">
 
var sum = 0;
 
 
for(var i=0; i<10; i++)
 
{
 
  sum += i;
 
}
 
</syntaxhighlight>
 
 
====C# - For====
 
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
int sum = 0;
 
int sum = 0;
Line 118: Line 75:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Jump n' ==
+
== Jump ==
  
 
There are two main jump statements:
 
There are two main jump statements:
Line 124: Line 81:
 
* '''break''': break out of the control statement.
 
* '''break''': break out of the control statement.
  
====JavaScript - Jump====
 
<syntaxhighlight lang="javascript">
 
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());
 
</syntaxhighlight>
 
 
====C# - Jump====
 
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
int sum = 0;
 
int sum = 0;
Line 161: Line 99:
  
 
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.
 
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|Programming Index]] : [[Programming_Chapter_2_Old|Previous Chapter]] : Next Chapter
 
[[Programming|Programming Index]] : [[Programming_Chapter_2_Old|Previous Chapter]] : Next Chapter
 +
[[category: legacy]]

Latest revision as of 01:29, 22 November 2018

This page is marked for deletion

Note: this page has been replaced by Programming Chapter 3

Author: Lucas Goss

Contents

[edit] 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.

[edit] Enum-ination

Enumerations are custom data types with a predefined set of values. To create one use the keyword enum followed by a name for the enumerator followed by a a list of strings between curly brackets separated by commons.

enum WeaponType { dagger, sword, hammer };

[edit] If Statement

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.

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.

[edit] For Loop

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

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

[edit] Jump

There are two main jump statements:

  • continue: jump back to the control statement.
  • break: break out of the control statement.
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.


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox