Here is the code we did in class with comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SelectionExamples
{
class Program
{
/*******************
* this program has examples of
* if statements including an embedded if
* and an if else statement
* steve Conger 10/11/2011
* ****************************/
static void Main(string[] args)
{
Program prog = new Program();
//prog.SimpleIf();
prog.IfElseIfExample();
prog.PauseIt();
}
private void SimpleIf()
{
//this is not so simple an if any more
//declare an integer
//this is assigned a value by the "out" parameter
//two lines down
int number = 0;
Console.WriteLine("Enter an integer");
//try parse returns true or false
//true if the number can be converted to an integer
//otherwise false. If it is true it passes the
//interger value "out" to the number variable
//declared above
bool isInteger = int.TryParse(Console.ReadLine(),out number);
//below is an example of an imbedded if
//if it is a good number test it
if (isInteger == true)
{
//never get to this if, if it is not a number
if (number % 2 == 0)
{
Console.WriteLine("the number is even");
}
else
{
Console.WriteLine("the number is odd");
}
}
else
{
Console.WriteLine("Not a valid number");
}
}
private void PauseIt()
{
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}//end PauseIt
private void IfElseIfExample()
{
Console.WriteLine("Enter a test score");
int score = int.Parse(Console.ReadLine());
string grade=null; //initializes the string though to nothing
//below is an if, else if statement. It allows you to test
// a value against a sequence of ranges
//it is important that the sequence be correct
if (score >= 90)
{
grade = "A";
}
else if (score >= 80)
{
grade = "B";
}
else if (score >= 70)
{
grade = "C";
}
else if (score >= 60)
{
grade = "D";
}
else
{
grade = "F";
}
Console.WriteLine("your grade is {0}", grade);
}//end if else if
}//end Program
}
No comments:
Post a Comment