Difference between revisions of "Intro Week 5"

esse quam videri
Jump to: navigation, search
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
 
Line 5: Line 5:
 
enum
 
enum
 
:enumerations allow us to define data that can take a fixed set of results.
 
:enumerations allow us to define data that can take a fixed set of results.
<syntaxhighlight lang="csharp" line="1" >
+
<syntaxhighlight lang="csharp">
 
enum direction {north, east, south, west};
 
enum direction {north, east, south, west};
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="csharp" line="1" >
+
<syntaxhighlight lang="csharp">
 
enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};
 
enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 14: Line 14:
 
Or we can use months
 
Or we can use months
  
<syntaxhighlight lang="csharp" line="1" >//another more useful example
+
<syntaxhighlight lang="csharp">//another more useful example
 
// forced sequence to start   
 
// forced sequence to start   
 
// from 1 instead of 0 (default)
 
// from 1 instead of 0 (default)
Line 25: Line 25:
  
 
To write out the name of month we can use the enumerator
 
To write out the name of month we can use the enumerator
<syntaxhighlight lang="csharp" line="1" >
+
<syntaxhighlight lang="csharp">
 
Console.WriteLine(Months.February);
 
Console.WriteLine(Months.February);
 
Console.WriteLine( (Months)3 );
 
Console.WriteLine( (Months)3 );
Line 37: Line 37:
 
A group of strings
 
A group of strings
  
<syntaxhighlight lang="csharp" line="1" >
+
<syntaxhighlight lang="csharp">
 
string[] names; //delcare an array of strings
 
string[] names; //delcare an array of strings
 
names = new string[4]; //initialize the array
 
names = new string[4]; //initialize the array
Line 52: Line 52:
 
A for each loop requires and iterator.. more on this iterator thing later...
 
A for each loop requires and iterator.. more on this iterator thing later...
  
<syntaxhighlight lang="csharp" line="1" >string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
+
<syntaxhighlight lang="csharp">string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
 
foreach (string person in names)
 
foreach (string person in names)
 
{
 
{

Latest revision as of 03:21, 9 February 2016

Enum

More complex variable types but very useful ways of defining data.

enum

enumerations allow us to define data that can take a fixed set of results.
enum direction {north, east, south, west};
enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};

Or we can use months

//another more useful example
// forced sequence to start  
// from 1 instead of 0 (default)
enum Months 
  {
    January = 1, February , March, April ,
    May , June , July , August , Sept , Oct , Nov , Dec 
  }


To write out the name of month we can use the enumerator

Console.WriteLine(Months.February);
Console.WriteLine( (Months)3 );

String Methods

Array

Groups of that same type of variable

A group of strings

string[] names; //delcare an array of strings
names = new string[4]; //initialize the array

//set the values in the array
names[0] = "Cheryl";
names[1] = "Joe";
names[2] = "Matt";
names[3] = "Robert";

for each

A for each loop requires and iterator.. more on this iterator thing later...

string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
foreach (string person in names)
{
    Console.Write (" " + person);
}

produces

 Cheryl Joe Matt Robert

Homework

Write a program that averages 10 numbers