Jump to content
xisto Community
Sign in to follow this  
oval1405241520

Lesson #2 - Switch Statement In C# Using the Switch Statement in C#

Recommended Posts

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 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.

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Computer ProgrammingLesson #2 - Switch Statement In C#

Apart from the cleaner construct of the switch, in C#, the if, else if, else if construct is actually embedded if statements which is less optimal especially if you are dealing with very deep nests.

On the other hand the if else construct gives you greater flexibility on each of your conditional phrase.

-reply by frostbite

Share this post


Link to post
Share on other sites
what does the following program print?Lesson #2 - Switch Statement In C#

#include <stdio.H>

int main (void)

{

int x=97;

int I; 

for(I=8, I<10; I++)

{

if(I%2==0)

  x--;

else

  x++;

}

switch(x)

{

case 94: printf("94and");

case 95: printf("95and");

case 96: printf("96and");

case 97: printf("97and");

case 98: printf("98and");

case 99: printf("99and");

}

}

-reply by kushal

Share this post


Link to post
Share on other sites

How to use Expression in Case with Switch statement

 

Lesson #2 - Switch Statement In C#

int MyNumber  = int.Parse(TextBox1.Text)

switch (MyNumber) { case (MyNumber > 0): greeting = "Bigger Than 0"; break; case (MyNumber = 0): greeting = "Equal To 0"; break; case (MyNumber < 0): greeting = "Less Than 0"; break;   }

 

Please Help, how to use switch statement for condition above, Thank you


 

-question by abyan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.