Lec7 - Lecture 7: Classes in C# PIC 10C Todd Wittman Basic...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 7: Classes in C# PIC 10C Todd Wittman Basic Flow Control: Loops c The basic loops in C# are the same as in C++. while ( x >0) { . .. } do { ... } while (x>0); for (int i=0; i<myArray.Length; i++) { . .. } c There's also a command foreach that is useful for moving through an array. To use it, create a variable that is the same type as stored in the array. foreach (type varName in arrayName) c The variable takes on each value array[i]. string[] myStrings = {"Bart", "Homer", "Lisa", "Maggie"}; foreach (string s in myStrings) Console.WriteLine(s);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Basic Flow Control: Conditionals c Basic if statements are the same as in C++. if (x>0) { . .. } else {. ..} c Unlike in C++, switch statements work on strings. Console.Write("Enter name: "); string name = Console.ReadLine(); switch (name.ToUpper()) { case "HOMER": Console.WriteLine("Hi Homer!"); break; default: Console.WriteLine("D'oh!"); break; } Parsing Text c All input and output in C# is in strings. c The Write function automatically converts numbers to strings: Console.Write(x); c When we take input, the result is always a string: string s = Console.ReadLine(); c If we want to use the number equivalent, we need to convert it: int i = int.Parse(s); double d = double.Parse(s);
Background image of page 2
Parsing Text c What if we want to read in a line of numbers? Enter numbers: 10 20 30 40 50 c ReadLine will get the string "10 20 30 40 50". We need to parse this string into individual numbers. c The s.Split(c) member function divides up the string s into a string array, divided at the specified char c. string[] words = s.Split(' '); c The Split method can also take a char array as input. This allows you to handle multiple separators. c Ex Read in a list of integers, ignoring all punctuation. string words = Console.ReadLine(); string[] wordArray = words.Split( new char[]{' ',',','.'} ); int[] intArray = new int[wordArray.Length]; for (int i=0; i<wordArray.Length; i++) intArray[i] = int.Parse(wordArray[i]); Classes in C# c When creating a new class, you can put it underneath your Program class in Program.cs class Program { static void Main() { ... } } class NewClass {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course PIC 10C taught by Professor Wittman during the Spring '10 term at UCLA.

Page1 / 10

Lec7 - Lecture 7: Classes in C# PIC 10C Todd Wittman Basic...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online