C#: Conditionals & Control Flow #2

Till now in Conditionals & Control Flow we have learn't Operators,Operands,Expression, the if statement, if...else statement and
also about if...elseif...else statements, we've learnt about syntax also and also know how to use these conditionals. Now, its time to move on. Today in this this lesson we learn about the switch statements, more about operators and its types and other related stuff as well. Now lets get started!!

In the editor you see a simple switch statement. A switch statement comes in handy when you have a series of if/elseif/else statements with multiple expressions that depend on the same value.The switch statement also provides a bit of efficiency and readability. Switches work like if statements, if a condition is true, it executes a block of code.

using System;
​class prg
{
 public static void Main(String []args)
 {
        int day; // variable for day
        Console.WriteLine("Please enter day (1-7) which you want to search");
        day=Convert.ToInt32(Console.ReadLine());
        switch (day)
        {
           case 1 : Console.WriteLine("Sunday");
                    break;
           case 2 : Console.WriteLine("Monday");
                    break;
           case 3 : Console.WriteLine("Tuesday");
                    break;
           case 4 : Console.WriteLine("Wednesday");
                    break;
           case 5 : Console.WriteLine("Thursday");
                    break;
           case 6 : Console.WriteLine("Friday");
                    break;
           case 7 : Console.WriteLine("Saturday");
                    break;
           default :Console.WriteLine("Not an allowable day number");
                    break;
        }
 }
}

Here in above example you'll see a simple switch statement which executes according to the input given by user and the execute and display the result according to the input give. Also, switch statements are much easy to use as compared with that of if...else statements, switch statements compile in less amount of time as compared with if...else statement. Here in above example we've taken variable "day" for users input, it asks user to enter any number between 1-7. So you see in 7th line it is asking with user to enter any number between 1-7. In line 8th the number which is entered by the user is stored in variable day. Also, in C# the input which you gave is considered as string. So to convert that string to integer for our numbers, in line 8th you'll see the "Convert.ToInt32" statement which converts that string to integer and then store number entered by the user in variable "day". Now, after that the control will shift to line 9 , switch gets the choice entered by the user. Now, suppose if user enters "7" then as you see clearly case 7 will execute and it will display "Saturday". That is how Switch works like.
Now what if when user enters "8" ?? Don't worry there is also a solution, if user enters 8 then control will shift to "case default" and it will display "Not an allowable day". I think you all understand how switch works?? But if not then move further and lets understand step by step using switch.

The Switch Statement

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for. In switch you can check multiple conditions. Here is what it looks like.

using System;
​class prg
{
 public static void Main(String []args)
 {
        int num=2;
        
        switch (num)
        {
           case 1 : Console.WriteLine("1");
                    break;
           case 2 : Console.WriteLine("2");
                    break;
           case 3 : Console.WriteLine("3");
                    break;
           case 4 : Console.WriteLine("4");
                    break;
           default :Console.WriteLine("None of the above");
                    break;
        }
 }
}

01. A switch statement is made up of the switch keyword, a variable to check, and a pair of curly braces { }. Here we check the value of num.
02. Then we have a case block for each comparison. For example case 1 : Console.WriteLine("1");break;checks whether num is equal to 1. if yes, it display "1", and uses break to exit the switch statement.
03. Otherwise, the next case block runs.
04. If all cases return false, the default case gets executed.

Multiple Cases In Switch Statement

You sometimes want to make multiple expressions, all of which have the same result. Consider the following if statement:

int num=2;
        
        if(num==1 || num==2 || num==3)
        {
                Console.WriteLine("num is between 2 and 3");
        }

With a switch statement, you can do this by adding cases right after another without a break. This is called falling through. The following code works exactly like the above if statement:

int num=2;
        
switch(num)
  {
     case 1 : 
     case 2 : Console.WriteLine("number is somewhere between 2 and 3");
                         break;
     case 3 : 
     default: Console.WriteLine("I don't know how much "+ num +" is...");
                         break;

  }

It's time to show what you have learned about switches so far! Now, its all for today I hope you all like this post and understand about switches.In our next session we move further in Control Flow and learn about Operators cause i think after learning stuff in this session you all get tired. Don't forget to share this post, and also do mention in comment if you liked this post. Thank You!!
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment