Intro Week 5

esse quam videri
Revision as of 18:29, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "</csharp>" to "</syntaxhighlight>")
Jump to: navigation, search

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.
1 enum direction {north, east, south, west};
1 enum difficulty {novice =1, easy =5, normal =10, hard =15, unbeatable =25};

Or we can use months

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


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

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

String Methods

Array

Groups of that same type of variable

A group of strings

1 string[] names; //delcare an array of strings
2 names = new string[4]; //initialize the array
3 
4 //set the values in the array
5 names[0] = "Cheryl";
6 names[1] = "Joe";
7 names[2] = "Matt";
8 names[3] = "Robert";

for each

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

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

produces

 Cheryl Joe Matt Robert

Homework

Write a program that averages 10 numbers