Thursday, July 14, 2011

7/14/2011


using System;
using System.Collections.Generic;
using System.Text;

namespace InitializingVariables
{
class Program
{
static void Main(string[] args)
{
int myInt = 7;
System.Console.WriteLine("Initialized, myInt: {0}", myInt);

myInt = 5;
System.Console.WriteLine("After assignment, myInt: {0}", myInt);
}
}
}


Here is tutorial 3-4


using System;

namespace SymbolicConstants
{
class SymbolicConstants
{
static void Main(string[] args)
{
const int FreezingPoint = 32; //degrees Fahrenheit
const int BoilingPoint = 212;
System.Console.WriteLine("Freezing point of water: {0}", FreezingPoint);
System.Console.WriteLine("Boiling point of water: {0}", BoilingPoint);
}
}
}


Here is tutorial 3-6


//you can create an unconditional branch in one of two ways. the first way is by invoking a method
using System;

namespace CallingAMethod
{
class CallingAMethod
{
static void Main()
{
Console.WriteLine("in Main! Calling SomeMethod()...");
SomeMethod();
Console.WriteLine("Back in Main().");
}

static void SomeMethod()
{
Console.WriteLine("Greetings from SometMethod!");
}
}
}


Lesson 3-7


using System;
class Values
{
static void Main()
{
int valueOne = 10;
int valueTwo = 20;

if (valueOne > valueTwo)
{
Console.WriteLine("ValueOne: {0} larger than ValueTwo: {1}", valueOne, valueTwo);
}
else
{
Console.WriteLine("ValueTwo: {0} larger than ValueOne: {1}", valueTwo, valueOne);
}
valueOne = 30; // set valueOne higher

if (valueOne > valueTwo)
{
valueTwo = valueOne + 1;
Console.WriteLine("\nSetting valueTwo to valueOne value, ");
Console.WriteLine("and incrementing ValueOne.\n");
Console.WriteLine("ValueOne: {0} ValueTwo: {1}", valueOne, valueTwo);
}
else
{
valueOne = valueTwo;
Console.WriteLine("Setting them equal. ");
Console.WriteLine("ValueOne: {0} ValueTwo: {1}", valueOne, valueTwo);
}
}
}


Here is another tutorial about nested ifs


using System;
using System.Collections.Generic;
using System.Text;

namespace NestedIf
{
class NestedIf
{
static void Main()
{
int temp = 32;
if (temp <= 32)
{
Console.WriteLine("Warning! Ice on road!");
if (temp == 32)
{
Console.WriteLine("Temp exactly freezing. Beware of water.");
}
else
{
Console.WriteLine("Watch for black ice! Temp: {0}", temp);
}
}//end if (temp <=32)
}//end main
} //end class
}//end namespace


Here is the final tutorial for the day:


using System;
class SwitchStatement
{
enum Party
{
Democrat,
ConservativeRepublican,
Republican,
Libertarian,
Liberal,
Progressive,
};
static void Main(string[] args)
{
Party myChoice = Party.Libertarian;
switch (myChoice)
{
case Party.Democrat:
Console.WriteLine("You voted Democratic. \n");
break;
case Party.ConservativeRepublican: // fall through
//Console.WriteLine("You voted Conservative.\n");
case Party.Republican:
Console.WriteLine("You voted Republican.\n");
break;
case Party.Liberal:
Console.WriteLine("Liberal is now Progressive");
goto case Party.Progressive;
case Party.Progressive:
Console.WriteLine("You voted Progressive.\n");
break;
case Party.Libertarian:
Console.WriteLine("Libertarians are voting Demcoratic");
goto case Party.Democrat;
default:
Console.WriteLine("You did not pick a valid choice.\n");
break;
}
Console.WriteLine("Thank you for voting.");
}
}

No comments: