Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Lesson #2 - Switch Statement In C#, Using the Switch Statement in C#
oval
post May 1 2006, 07:20 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 3
Joined: 26-April 06
From: Texas
Member No.: 13,037



In the previous lesson we demonstrated did a basic input output application which asked your your name and replied with a general greeting using the name you entered.

In this lesson we are going to learn about a conditional statement called the switch statement. Usually if and if else [color=#3333FF]statements are taught before switch statements. Although these are very useful, I find the switch to be easier and more fun to use.

The program written below is very similar to the previous program where the user is asked to enter their name, however now the program will look for four distinct string values and ti will generate output according to the string entered. Here are the things to observe when reading and replicating this program.

1.) 2 string variables are declared (greeting and fullName)
2.) A value is assigned using our Console.ReadLine() method. (Point where we insert the value.)
3.) the value of the variable greeting depends on the value entered for the variable fullName
4.) The program is case sensitive. Basically this is because different case letters have different values. So a lower case t will not evaluate the same as an upper case T. So ulitmately what does this mean. Well let's if you entered george bush as opposed to George Bush, you would get the default greeting because the program would not recognize your lower case entry of george bush as George Bush. If it does not make sense, run the program and change the case. You will see what I mean.
5.) Lastly, the default option is included in the case statement for the instances where neither of the output meets any of the criteria of the case statement.

With that said let's look at the code, replicate it and modify it to your liking.

CODE


using System;

namespace ConsoleApplication
{
    /// <summary>
    /// Testing out the case Statement
    /// </summary>
    class switchStatement
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string fullName, greeting;
            

            Console.Write("Please enter your name: ");
            fullName = Console.ReadLine();

            switch (fullName)
            {
                case "YOUR NAME HERE":
                    greeting = "You are the best!!";
                    break;
                case "Bill Gates":
                    greeting = "You are super wealthy!";
                    break;
                case "George Bush":
                    greeting = "hmmm????";
                    break;
                case "Kenneth Lay":
                    greeting = "How is Court Going?";
                    break;
                default:
                    greeting = "I'm sorry I did not recognize you!";
                    break;
            }//end switch statement

            Console.WriteLine();
            Console.WriteLine(greeting);
            Console.WriteLine();
            Console.WriteLine("Press [ENTER] to Exit");
            Console.ReadLine();


        }//end Main
    }//end class "switchStatement
}//end namespace





As I might have mentioned on my previous post I am using Visual Studio. I believe the Express version is out on the web for free. However you might need to install NetFramework before you proceed to install VS.

Also, just like the previous program, an adobe file will be attached with the source code. variables and comments are all color coded.

oval
Attached File(s)
Attached File  SwtichStatement.pdf ( 9.42k ) Number of downloads: 20
 
Go to the top of the page
 
+Quote Post
iGuest
post Aug 6 2008, 12:30 AM
Post #2


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Computer Programming
Lesson #2 - Switch Statement In C#

Why might a programmer choose one of these two types of statements(if else and switch)over the other? When is one better than the other?

-reply by Alishia Johnson
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. VB.NET: Switch Regional Language Automatically(1)
  2. Lesson #1 - Introduction To C#(1)
  3. C# Tutorial : Lesson 2 - Variables & Operators(0)
  4. C# Tutorial : Lesson 3 - Programming Constructs(1)
  5. C# Tutorial : Lesson 4 - Object Oriented Programming(2)
  6. C# Tutorial : Lesson 5 - Encapsulation & Abstraction(0)
  7. C# Tutorial : Lesson 6 - Creating Value Types & Reference Types - Part I(0)
  8. C# Tutorial : Lesson 7 - Creating Value Types & Reference Types - Part II(1)
  9. C# Tutorial : Lesson 8 - Functions/methods(0)
  10. C# Tutorial : Lesson 9 - Constructors & Destructors(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 03:04 PM