Difference between revisions of "Intro Week 4"

esse quam videri
Jump to: navigation, search
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
Line 33: Line 33:
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
if you type 'monkey' for your favorite number the program crashes with the folling message
 
if you type 'monkey' for your favorite number the program crashes with the folling message
Line 55: Line 55:
 
{
 
{
 
     string strError = e.ToString();
 
     string strError = e.ToString();
}</csharp>
+
}</syntaxhighlight>
  
 
==Looping Statements==
 
==Looping Statements==
Line 74: Line 74:
 
{
 
{
 
     Console.WriteLine("For Loop " + i + "<br />");
 
     Console.WriteLine("For Loop " + i + "<br />");
}</csharp>
+
}</syntaxhighlight>
  
 
Produces
 
Produces
Line 98: Line 98:
  
 
     Console.WriteLine( "For Loop " + i + "<br />");
 
     Console.WriteLine( "For Loop " + i + "<br />");
}</csharp>
+
}</syntaxhighlight>
  
 
Produces
 
Produces
Line 125: Line 125:
 
     Console.WriteLine("While Loop " + MyInt + "\n");
 
     Console.WriteLine("While Loop " + MyInt + "\n");
 
     MyInt++;
 
     MyInt++;
}</csharp>
+
}</syntaxhighlight>
  
 
produces
 
produces
Line 154: Line 154:
 
     Console.WriteLine("do Loop " + myIntDo + "\n");
 
     Console.WriteLine("do Loop " + myIntDo + "\n");
 
     myIntDo++;
 
     myIntDo++;
}      while (myIntDo < 10);  //Check interator</csharp>  
+
}      while (myIntDo < 10);  //Check interator</syntaxhighlight>  
  
 
produces
 
produces
Line 177: Line 177:
 
         doLoopResult2 +="do Loop " + myInt + "\n";
 
         doLoopResult2 +="do Loop " + myInt + "\n";
 
         myIntDo2++;
 
         myIntDo2++;
} while (myIntDo2 < 10);</csharp>
+
} while (myIntDo2 < 10);</syntaxhighlight>
  
 
produces
 
produces
Line 192: Line 192:
 
//Write the random number
 
//Write the random number
 
Console.WriteLine(randNunberBetweenOneandTen);
 
Console.WriteLine(randNunberBetweenOneandTen);
</csharp>
+
</syntaxhighlight>
  
 
==Home Work==
 
==Home Work==

Revision as of 18:28, 25 January 2016

Try and Catch

Another branching statement used to catch runtime errors.

Try and catch is useful when you are going to do something that may cause a run time error. These errors may occur when

  • Parsing user input
  • Converting Datatypes
  • Trying to connect to a remote server
  • Connecting to a database

For example consider a program that asks the user what their favorite number is.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace FavoriteNumber
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             int intFavNumber;
12             string strFavNumber;
13             
14             Console.Write("What is you favorite number?");
15             strFavNumber = Console.ReadLine();
16 
17             intFavNumber = Int16.Parse(strFavNumber);
18 
19         }
20     }
21 }

if you type 'monkey' for your favorite number the program crashes with the folling message


Unhandled Exception: System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolea
n parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   at FavoriteNumber.Program.Main(String[] args)

This program can be fixed be using a try and catch

1 try
2 {
3 	//some crazy code that may cause errors
4 }
5 catch (Exception e)
6 {
7     string strError = e.ToString();
8 }

Looping Statements

  • for
  • while
  • do... while
  • foreach (we'll do this after we do arrays)

for

syntax

    for ([initializers]; [expression]; [iterators]) statement
    
1 for (int i=0; i < 10; i++)
2 {
3     Console.WriteLine("For Loop " + i + "<br />");
4 }

Produces

For Loop 0
For Loop 1
For Loop 2
For Loop 3
For Loop 4
For Loop 5
For Loop 6
For Loop 7
For Loop 8
For Loop 9
 1 for (int i=0; i < 20; i++)
 2 {
 3     if (i == 10)
 4             break;
 5 
 6     if (i % 2 == 0)
 7             continue;
 8 
 9     Console.WriteLine( "For Loop " + i + "<br />");
10 }

Produces

For Loop 1
For Loop 3
For Loop 5
For Loop 7
For Loop 9

For loops can also be good to dynamically add or alter the contents or web controls. For example If I wanted a web control to have 100 items in it like this . I could use a for loop to add them here's an example

/infod/jeff/classSource/class3/DynamicAddDropdown.aspx - source

while

while syntax

  while (expression) statement
1 int MyInt = 0;   //iterator for while loop
2 
3 while (MyInt < 10)
4 {
5     Console.WriteLine("While Loop " + MyInt + "\n");
6     MyInt++;
7 }

produces

While Loop 0
While Loop 1
While Loop 2
While Loop 3
While Loop 4
While Loop 5
While Loop 6
While Loop 7
While Loop 8
While Loop 9

do

do... while syntax

   do statement while (boolean-expression);
   
1 int myIntDo = 0;  //iterator for do loop
2 
3 do
4 {
5     Console.WriteLine("do Loop " + myIntDo + "\n");
6     myIntDo++;
7 }       while (myIntDo < 10);   //Check interator

produces

do Loop 0
do Loop 1
do Loop 2
do Loop 3
do Loop 4
do Loop 5
do Loop 6
do Loop 7
do Loop 8
do Loop 9


1 string doLoopResult2 = "";
2 int myIntDo2 = 30;
3 do
4 {
5         doLoopResult2 +="do Loop " + myInt + "\n";
6         myIntDo2++;
7 }	while (myIntDo2 < 10);

produces do Loop 30

Notice the do loop always executes once as it doesn't check the iterator until after execution.

1 Random r = new Random();    //Get a new Random Object
2             
3 //Choose a number between 1 and 10
4 int randNunberBetweenOneandTen = r.Next(10) + 1;
5 
6 //Write the random number
7 Console.WriteLine(randNunberBetweenOneandTen);

Home Work

Guessing Game with a while loop where the program guesses a random number.

It should then ask the user to guess the number. If the number is correct say you win. If the guess is incorrect tell the user if the random number is higher or lower and allow then to guess again.

1 extra point for counting and displaying the number of guesses.