Wednesday, October 1, 2014

Selection Examples (evening Class)

Here is the first example: a mess but with examples of different forms of the if statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelectionExamples
{
    class Program
    {
        /// 
        /// this program just contains examples
        /// of if statements--it doesn't actually
        /// constitute an actual program
        /// Steve Conger Evening Class 10/1/2014
        /// 
        /// 
        static void Main(string[] args)
        {
            //first example shows a password
            //never put the password in the code
            //in real life
            string pass = "P@ssw0rd1";
            Console.WriteLine("Enter your password");
            string userPass = Console.ReadLine();

            //for string the "Equals" keyword 
            //provides better comparisons than "=="
            if(pass.Equals(userPass))
            {
                Console.WriteLine("thank You");
            }
            else //if the passwords don't match
            {
                Console.WriteLine("Sorry");
                Console.ReadKey();
                return;
            }

            int number;
            Console.WriteLine("enter a number");
            //TryParse returns a bool (true or false). 
            //It returns true if the string can be parsed
            //false if it cannot
            //if it is true it also assigns the value to the
            //variable named as an "out" parameter
           
            bool goodNumber = int.TryParse(Console.ReadLine(),out number);
            if (goodNumber==false) //or if (!goodNumber)
            {
                Console.WriteLine("Restart and enter a valid integer");
                Console.ReadKey();
                return;
            }

            // Or is represented as || 
            //you must repeat the variable name on both sides
            //of the || operator
            if(number < 20 || number > 60)
            {
            }

            // && is for and
            if (number > 20 && number < 60)
            {

            }
            //you can use these comparison operators
            // >, <, <=, >=, !=, ==

            //this is an else, else if, else block
            //it checks each condition
            //if the condition is true executes the commands
            //in the block. It then exits the block and exectutes
            //the commands after the if else bloc
            //If it is false, it skips the block
            //and checks the next condition
            //if none of the conditions are true
            //it does what is in the else block
            if (number == 21)
            {
                Console.WriteLine("Drinks too much");
            }//end if
            else if (number > 21)//resolve to boolean
            { 
                Console.WriteLine("Old enough to drink");
            }//end if
            else 
            {
                Console.WriteLine("Still too young");
            }

            Console.ReadKey();
        }
    }
}

Here is the second example using the birth year

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgeGame
{
    class Program
    {
        /// 
        /// This class is just an example of if  and if else
        /// statements. The if statements are nested
        /// first it tests if the entry is the right length
        /// if it is, then it tests if it is a valid integer,
        /// if it is, then it calculates the age of the user
        /// by subtracting the users birth year from the 
        /// current year. When it has the age it ouputs 
        /// a different message based on the age
        /// Steve conger Evening Class 10/1/2014
        /// 
      
        static void Main(string[] args)
        {
            int year;
            //prompt the user for a birth year
            Console.WriteLine("Enter the year of your birth (a 4 digit integer)");
            //read the string from console
            string yearString = Console.ReadLine();
            //make sure the string is 4 characters
            if(yearString.Length == 4)
            {
                //if it is 4 characters make sure it is an integer
                bool goodYear = int.TryParse(yearString, out year);
                if(goodYear)
                {
                    //if it is a good integer
                     //get the current year
                    int currentYear = DateTime.Now.Year;
                    //subract the user's birth year from the current year
                    int yearsOld = currentYear - year;
                    //output the result
                    Console.WriteLine("You are {0} years old",yearsOld);

                    //declare a string variable with no content
                    string message = null;

                    //test the age and assign a different text
                    //to the message string depending on the age
                    if (yearsOld > 50)
                    {
                        message = "Well done";
                    }
                    else if (yearsOld > 40)
                    {
                        message="Age perfection";
                    }
                    else if(yearsOld > 30)
                    {
                        message = "Just getting going";
                    }
                    else if(yearsOld > 20)
                    {
                        message = "Youth is wasted on the young";
                    }
                    else
                    {
                        message = "just getting started";
                    }
                //output the message
                    Console.WriteLine(message);
                }//end inner if
                else
                {
                    //if the value isn't a good integer
                    Console.WriteLine("Start over and enter a four digit integer for a year");
                }
      
            }//end outer if
            else
            {
                //if the value isn't four characters long
                Console.WriteLine("Start over and enter a four digit integer for a year");
            }
            Console.ReadKey();
        }//end main
    }
}

No comments:

Post a Comment