Here is the if and if else statement example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IfStatementExamples
{
class Program
{
///
/// This program just shows how to use if
/// and if else statements
/// it also shows how to use tryParse
/// to catch errors in entry
///
///
static void Main(string[] args)
{
//initialize the program
Program p = new Program();
//call the GetTemperature method
p.GetTemperature();
Console.ReadKey();
}
void GetTemperature()
{
//set the temp variable
int temp=0;
//prompt the user
Console.WriteLine("Enter the Temperature.");
//this is the try parse.
//if the value entered on the console is a
//valid integer it returns true to isInt and assigns
//the value to the "out" parameter temp
//if not, it returns false and ignores the out parameter
bool isInt = int.TryParse(Console.ReadLine(), out temp);
//so if it isn't true we want to tell them
//and give them one more chance to enter
//a good value
if (isInt != true)
{
Console.WriteLine("Enter a valid integer");
temp = int.Parse(Console.ReadLine());
}
//call the ReactToTemp method and pass it the temperature
ReactToTemp(temp);
}
void ReactToTemp(int temperature)
{
//this method uses if and else if to evaluate the temperature
//and change the console background based on the temperature
if (temperature >= 100 )
{
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
Console.WriteLine("Hot");
}
else if (temperature >= 80)
{
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.Clear();
Console.WriteLine("Warm");
}
else if (temperature >= 60)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("Pleasent");
}
else if (temperature >= 30)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();
Console.WriteLine("Cool");
}
else
{
Console.BackgroundColor = ConsoleColor.Cyan;
Console.Clear();
Console.WriteLine("cold");
}
}
}
}
Here is the switch example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SwitchExample
{
class Program
{
///
/// this program shows an example of a switch
/// structure. It takes a grade as an integer
/// and returns a letter grade
///
static void Main(string[] args)
{
//initialize the program
Program p = new Program();
//call the method GetGrade();
p.GetGrade();
Console.ReadKey();
}
void GetGrade()
{
//Declare the variable grade
int grade;
//prompt the user
Console.WriteLine("Enter your grade");
//use the try parse to test the input--see
//the explanation on the if example
bool goodGrade = int.TryParse(Console.ReadLine(), out grade);
if (!goodGrade)
{
//in this case we don't give them a second chance
//we just tell them what's wrong and exit
Console.WriteLine("Next time do a valid grade as an integer");
return;
}
//call translate grade
TranslateGrade(grade);
}
void TranslateGrade(int grade)
{
//create a string variable for the letter grade
//and give it a default value
string letterGrade ="Unavailable";
//set up the switch. Cases have to be
//exact values. No ranges or > <
switch (grade)
{
//case 4 means literally the grade must be 4 to match
case 4: //note the colon here.
letterGrade="A";
break;
case 3:
letterGrade = "B";
break;
case 2:
letterGrade = "C";
break;
case 1:
letterGrade = "D";
break;
case 0:
letterGrade = "F";
break;
default:
letterGrade = "Undefined";
break;
}
Console.WriteLine("Your Letter grade is {0}", letterGrade);
}
}
}
No comments:
Post a Comment