Decision Making: Switch Statements and tertiary Operator

Decision Making: Switch Statements and tertiary Operator
Switch Statements
Switch statements are another way of handling decision making in your program. Switch statements work just like if-else statements. The reason when they are used is when you have a bunch of if else if statements and it becomes difficult to read then you it change your code to use Switch statements.

How Switch statements work
Syntax
switch (variable) {
	case value1:
		//code for this decision
	break;
case value2:
		//code for this decision
	break;
	default:
		//code for default case
	break;
}
The idea here is that you use switch keyword than pass variable whose value you want to check for whole bunch of cases. When the value of variable match with the given case value, the code in that case executes until the break statement occur, which will just finish the switch statement.

What is default
If no value is matched with all the available cases then the flow of program fall into default case which will execute. It is optional you can skip it in which case nothing will happen if no case is matched.

Note:  If you are familiar with any other programming language like java you might know that you can skip break statement in any case which will cause the program to fall into succeeding case and it will keep happening until break statement occur. Well you can’t skip break statement in C#. The people who created C# thinks that this technique might create errors so they don’t let you skip any break statement. If you try to do so the C# compiler will give you error and program won’t compile.

Limitation to Switch Statement and when to use it
You might have noticed that unlike if statements in switch statement only equality is checked. You can’t use less than or greater than symbols in it. You should use switch statement when you have multiple fixed option. If you have range then you should not use it.

Example

Problem 1: Take input from user with only option of pressing “w,a,s,d”. If user presses w then print “up”. If user presses s then print “down”. If user presses a then print “left”. If user presses d then print “right”.

Solution: In this case you have multiple fixed values so you should use switch statements to solve this problem.

Problem2: Take input from user as number from 0 to 100. If user gives number less than 50 then print “fail”. If number is greater than 50 but less than 60 then print “D”. If number is greater than 60 but less than 70 then print “C”. If number is greater than 70 but less than 80 then print “B”. If number is greater than 80 then print “A”.

Solution: in this case you have multiple ranges of values. So you should use if else statements rather than switch cases.
Coding Example
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter direction ( w->up a->left s->down d->right) = ");
            char direction = Console.ReadKey().KeyChar;
            Console.ReadLine();
            switch (direction)
            {
                case 'w':
                    Console.WriteLine("Up");
                    break;
                case 'a':
                    Console.WriteLine("Left");
                    break;
                case 's':
                    Console.WriteLine("Down");
                    break;
                case 'd':
                    Console.WriteLine("Right");
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
        }
    }
Output

<< Descision making: if else statements
Tags: C-sharp
comments powered by Disqus