C# - The switch statement
Another important statement is the switch statement. Now we will learn it :)
The switch statement is like a set of if statements. It's a list of possibilities, with an action for each possibility, and an optional default action, in case nothing else evaluates to true. A simple switch statement looks like this:
int number = 1;
switch(number)
{
case 0:
Console.WriteLine("The number is zero!");
break;
case 1:
Console.WriteLine("The number is one!");
break;
}
The identifier to check is put after the switch keyword, and then there's the list of case statements, where we check the identifier against a given value. You will notice that we have a break statement at the end of each case. C# simply requires that we leave the block before it ends. In case you were writing a function, you could use a return statement instead of the break statement.
In this case, we use an integer, but it could be a string too, or any other simple type. Also, you can specify the same action for multiple cases. We will do that in the next example too, where we take a piece of input from the user and use it in our switch statement:
Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
case "yes":
case "maybe":
Console.WriteLine("Great!");
break;
case "no":
Console.WriteLine("Too bad!");
break;
}
In this example, we ask the user a question, and suggest that they enter either yes, no or maybe. We then read the user input, and create a switch statement for it. To help the user, we convert the input to lowercase before we check it against our lowercase strings, so that there is no difference between lowercase and uppercase letters. Still, the user might make a typo or try writing something completely different, and in that case, no output will be generated by this specific switch statement. Enter the default keyword!
Console.WriteLine("Do you enjoy C# ? (yes/no/maybe)");
string input = Console.ReadLine();
switch(input.ToLower())
{
case "yes":
case "maybe":
Console.WriteLine("Great!");
break;
case "no":
Console.WriteLine("Too bad!");
break;
default:
Console.WriteLine("I'm sorry, I don't understand that!");
break;
}
If none of the case statements has evaluated to true, then the default statement, if any, will be executed. It is optional, as we saw in the previous examples.
-
How to Remove Projects from Visual Studio
| By 5min | in General
Visual Studio Remove Projects. How to remove projects from Visual Studio...
-
C# - Hello world explained
| By Victorius | in Programming
I will show you what's the life after Hello Word :)...
-
C# - Variable types
| By Victorius | in Programming
I will teach you what kind of variables can you use in C#...
-
C# - Variables in higher level
| By Victorius | in Programming
In this article i will teach you how you can use variables in most of your program....
-
C# - The if statement
| By Victorius | in Programming
Now we will learn the most important statement....
-
What to look for when choosing a web host | By MaxwellPayne | in Programming
When searching for a good website hosting service your website, you'll need to consider a few things....
-
What are User Agents? | By MaxwellPayne | in Programming
Find out what user agents are and what they do....
-
What is website cloaking? | By MaxwellPayne | in Programming
Learn about website cloaking and how to use cloaking....
-
The Pros and Cons of Using Website Splash Pages | By MaxwellPayne | in Programming
Splash pages can serve many useful purposes as well as add a level of professionalism and nice design to a page....
-
Basic Spring Transactions | By davistechyinfo | in Programming
Spring offers a few options when it comes to transaction management. I used the Programmatic Transaction option thi...
-
C# - Looping in statements | By Victorius | in Programming
We must learn how we can loop in statements. Now here we go....
-
C# - The if statement | By Victorius | in Programming
Now we will learn the most important statement....
-
C# - Variables in higher level | By Victorius | in Programming
In this article i will teach you how you can use variables in most of your program....
-
C# - Variable types | By Victorius | in Programming
I will teach you what kind of variables can you use in C#...
-
C# - Hello world explained | By Victorius | in Programming
I will show you what's the life after Hello Word :)...








No comments yet.