using System; using System.Collections.Generic; using System.Text; namespace SelectionExamples { /****************************************** * This program provides examples of selection * structures including several versions of the * if and if else statements and the switch structure * which it uses to create a menu. It also introduces a * while loop which belongs to the next chapter. * Steve Conger 10/22/2008 * *****************************************/ class Program { static void Main(string[] args) { //call the menu Menu(); } static void Menu() { int choice = 0; //the while loop belongs to the next chapter. It makes it so we //don't have to stop each time, but don't worry about it yet while (choice != 6) { //clear the console each time, then write //the menu Console.Clear(); Console.WriteLine("*****************************"); Console.WriteLine("* 1. Simple if"); Console.WriteLine("* 2. Simple if else "); Console.WriteLine("* 3 And Or examples"); Console.WriteLine("* 4 If Else If Examples"); Console.WriteLine("* 5 Nested Example"); Console.WriteLine("* 6 Exit"); Console.WriteLine("*****************************"); Console.WriteLine(); Console.WriteLine("Enter the number for your choice"); choice = int.Parse(Console.ReadLine()); //this is an example of a switch structure //it calls the method you choose switch (choice) { case 1: SimpleIf(); break; case 2: SimpleIfElse(); break; case 3: AndORExamples(); break; case 4: IfElseIfExample(); break; case 5: NestedifExample(); break; case 6: Console.WriteLine("Press any key to exit"); break; default: Console.WriteLine("Not a valid choice"); break; } Console.ReadKey(); } } static void SimpleIf() { //this method uses simple if statements to say whether //a value is greater than, less than or equal to //50 Console.WriteLine("Enter an integer between 1 and 100"); int number = int.Parse(Console.ReadLine()); if (number < 50) Console.WriteLine("The number is less than 50"); if (number > 50) Console.WriteLine("The number is greater than 50"); if (number == 50) Console.WriteLine("The number equals 50"); } static void SimpleIfElse() { //this method uses a simple if else structure //to determine if a give number is even //or odd. Note that equality uses == rather // than just = Console.WriteLine("Enter any integer"); int number = int.Parse(Console.ReadLine()); if (number % 2 == 0) { Console.WriteLine("The number is even"); } else { Console.WriteLine("The number is odd"); } } static void AndORExamples() { //this method uses one or and several //ands to look at a range of ages and //return a comment Console.WriteLine("Enter your age"); int age = int.Parse(Console.ReadLine()); if (age < 3 || age > 120) { Console.WriteLine("I don't think you are telling the truth"); } if (age > 2 && age < 13) { Console.WriteLine("A mere child"); } if (age > 12 && age < 20) { Console.WriteLine("A teenager"); } if (age > 20 && age < 60) { Console.WriteLine("prime of life"); } if (age > 59) { Console.WriteLine("Starting to get a touch of gray"); } } static void IfElseIfExample() { //this method uses an if else if block to //evaluate a temperature //Note that technically a temperature of say //79 is greater than not only 60, but also greater //that 45 or 30, but when you use a block //like this the program stops with the first //true statement. It is important that you //arrange them in the right order Console.WriteLine("Enter the day's Temperature"); int temperature = int.Parse(Console.ReadLine()); if (temperature > 100) { Console.WriteLine("Way too hot"); } else if (temperature > 80) { Console.WriteLine("Hot day"); } else if (temperature > 60) { Console.WriteLine("Just right"); } else if (temperature > 45) { Console.WriteLine("A touch of Autumn"); } else if (temperature > 30) { Console.WriteLine("Cold day"); } else { Console.WriteLine("Just plain cold"); } } static void NestedifExample() { //this method uses a nested if //the outer if makes sure the value is valid //if it is, it passes it to the inner if else if //structure that evaluates it, if not //it passes it to the outer else clause // that lets you know it is an invalid average Console.WriteLine("Enter your GradePoint average"); double gpa = double.Parse(Console.ReadLine()); char grade; //outer if if (gpa >= 0 && gpa <= 4) { //inner if else block if (gpa > 3.5) { grade = 'A'; } else if (gpa > 3) { grade = 'B'; } else if (gpa > 2) { grade = 'C'; } else if (gpa > 1) { grade = 'D'; } else //inner else { grade = 'F'; } } else //outer else { Console.WriteLine("Invalid Grade"); return; //the return keeps it from executing //the last line } Console.WriteLine("Your grade is {0}", grade); } } }