TEP , The Engineering Projects , Author Welcome! Syed Zain Nasir Mechatronics Engineer TEP , The ENgineering Projects , TEP Badge TEP , The Engineering Projects , Ratting Background 7.5 7.5/10
TEP , The Engineering Projects , Rating Percentage Background Reputation Score 55 / 100
How to use switch Statement in C#, switch statement in c#, switch case statement, switch case in c#, switch c#, switch case c#
TEP , The Engineering Projects , Boxes

How to use switch Statement in C#

TEP , THe Engineering Projects , Shares 2.5K Views
TEP , The ENgineering Projects , Reaction 40
TEP , The ENgineering Projects , Reaction 700
TEP , The ENgineering Projects , Reaction 60
TEP , The ENgineering Projects , Reaction 25
TEP , The ENgineering Projects , Reaction 60
TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest
Shares: 691
TEP , The Engineering Projects , PCBWAY
How to use switch Statement in C#, switch statement in c#, switch case statement, switch case in c#, switch c#, switch case c#
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at How to use switch Statement in C# and its our 9th tutorial in C# series. In our previous tutorial, we have seen IF Loop in C# and this switch statement is also a loop but works slightly different than IF loop and it totally depends on your application, which one you want to use. First we will have a look at How to use switch statement in C# and after that we will also study goto statement, because switch and goto statements are normally used together. So, let's get started with it:

How to use switch Statement in C#

  • Switch Statement is a loop in C# which takes a variable as a condition, and then creates its different cases and we can deal each case separately.
  • We need to use break Statement after every case Statement to get out of the switch Statement.
  • We can also use default Statement at the end of switch Statement, which will execute if none of the cases are true.
  • So let's have a look at the syntax of switch statement in C#:
switch (variable) { case value1: // This case will execute, if variable = value1 break; case value2: // This case will execute, if variable = value2 break; . . . case valueN: // This case will execute, if variable = valueN break; default: // default will execute, if all the above cases are false. break; }
  • As you can see in above syntax, we have first used switch keyword and then took a variable, this variable can have any datatype.
  • Then we have created different cases, we can create as many cases as we want and each case has a value so if the variable is equal to that value then respective case will execute.
  • Let's understand it with an example: Let's design a project where we display information of class students using their roll number, so we will take their roll numbers as an input and will display the respective student's data using switch case statement:
How to use switch Statement in C#, switch statement in c#, switch case statement, switch case in c#, switch c#, switch case c#
  • As you can see in the above figure that I have first asked for the student's Roll Number and then placed a switch statement and provided RollNo as a conditional variable.
  • After that in each case I have placed the roll no value and for each roll number I have added the name and age of the user.
  • In execution, I have entered 1 and the code has provided me data placed in case 1 and have ignored all the other cases as well as the default case.
  • But if you run this code then you will realize that it asks for the Roll number only once but what if we want it to run again & again.
  • We can use goto Statement for that purpose, although it's not the best way to create loop but one of the ways and you should know about it.
How to use switch Statement in C#, switch statement in c#, switch case statement, switch case in c#, switch c#, switch case c#
  • In above code, I have just added two lines of code, first I have placed a tag Start: at the top of the code and then at the end I have used goTo Start;
  • So, at the goto Statement, the compiler will find the tag mentioned and will move on to that position and will start executing the code, so it's kind of an infinite loop which is never gonna stop.
  • Here's the final code, which you can test in visual studio:
using System;

namespace TEPProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Start:
            Console.Write("Please enter student's roll number: ");
            int RollNo = int.Parse(Console.ReadLine());
            
            switch(RollNo)
            {
                case 1:
                    Console.WriteLine("Name: StudentA");
                    Console.WriteLine("Age: 18");
                    break;
                case 2:
                    Console.WriteLine("Name: StudentB");
                    Console.WriteLine("Age: 17");
                    break;
                case 3:
                    Console.WriteLine("Name: StudentC");
                    Console.WriteLine("Age: 16");
                    break;
                case 4:
                    Console.WriteLine("Name: StudentD");
                    Console.WriteLine("Age: 20");
                    break;
                case 5:
                    Console.WriteLine("Name: StudentE");
                    Console.WriteLine("Age: 21");
                    break;
                default:
                    Console.WriteLine("No student found.");
                    break;
            }
            Console.WriteLine("\n\n");
            goto Start;
        }
    }
}

So, that was all about How to use switch Statement in C#. If you need any help then ask in comments. Will meet you in next tutorial. Till then take care !!! :)
TEP , The Engineering Projects , Tags
How to use switch Statement in C#
switch statement in c#
switch case statement
switch case in c#
switch c#
switch case c#
TEP , The Engineering Projects , Calender Sunday, November 17, 2019
TEP , The Engineering Projects , Calender Sunday, November 17, 2019
TEP , The Engineering Projects , Comments

Write a Comment

TEP , The Engineering Projects , WordPress TEP , The Engineering Projects , Google TEP , The Engineering Projects , Twitter TEP , The Engineering Projects , Facebook TEP , The Engineering Projects , User
TEP , The Engineering Projects , Robot
TEP , The Engineering Projects , Comments Comments on ‘’ How to use switch Statement in C# ‘’ (0)
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Top PCB Design Service

PCBA
TEP , The Engineering Projects , JLCPCB
PCB
TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Embedded Tools

ARDINO TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Subscribe Now !!!

Learn Free Pro Tricks

TEP , The Engineering Projects , Mail Receive Quality Tutorials Straight in your Inbox by Submitting your Email ID Below
TEP , The Engineering Projects , Mail TEP , The Engineering Projects , Mail Shadow
TEP , The Engineering Projects , Mail Robot TEP , The Engineering Projects , Mail Robot Shadow TEP , The Engineering Projects , Mail Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Engineering Books

TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (3.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (4.7)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , TEP Robot TEP , The Engineering Projects , TEP Robot Shadow TEP , The Engineering Projects , TEP Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Categories

TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K