UNDERSTANDING DECISION MAKING STATEMENT IN C#

Today in Engineer's World, we are discussing a very important topic of C# - Decision Statements in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs

Previous - First C# Program

Decision Statement

Decision making statement is very useful in cases when we find ourselves stuck in some really bad situation. In general scenarios, we find out possible causes for such scenarios and on behalf of that we make our decision.

Let's understand this by taking a general example
Suppose we decide to buy a guitar. But, the problem here is how we would pay the cost?
 
Figure-1
 
Similarly, we programmers on the basis of some test cases and conditions find out a way to solve such problem. 

If you are new to .NET and C# try to understand by coding. And ask us.

In C# there are following 4 types of decision making statements: -

1. if statement 
An if statement consists of a Boolean expression followed by one or more statements. If the value is true, then if block is executed otherwise next statement would be executed. 
 
Syntax: The Syntax for if statement is
 
Figure-2

Practical Implementation:
Try this example to understand better
 
Figure-3
 
This is not an efficient way of writing statement.

2. if-else statement 
An if-else statement means you can use one if accompanied with else if statement when the condition fails. If the value is true, then if block is executed otherwise next statement would be executed. 

Syntax: The Syntax for if-else statement is
 
Figure-4

Practical Implementation:
Try this example to understand better
 
Figure-5

3. nested if statement
Nested if means you can use one if or else if statement inside another if or else if statement. If the value is true, then if block is executed otherwise next statement would be executed. 

Syntax: The Syntax for nested if statement is
 
Figure-6

Practical Implementation:
Try this example to understand better
 
Figure-7

4. switch
A switch statement allows a variable to be tested for equality against a list of values.
Note: switch accept constant value in cases, otherwise it may result in error. 

Syntax: The Syntax for switch statement is
 
Figure-8

Practical Implementation:
Try this example to understand better
 
Figure-9


For any query, comment us below.


Related Questions: -


Q-1 Select the output for following set of code:
using System;

namespace AspnetSolutions
{
    class Switch
   {
         public static void Main()
         {
              int i = 2, k = 3;
              switch (i - k)
              {
                    case -1:
                                   ++i;
                                   ++k;
                                   break;
                    case 2:
                                   --i;
                                  ++k;
                                  break;
                    default:
                                  i += 3;
                                  k += i;
                                  break;
              }
             Console.WriteLine(i + "\n" + k);
             Console.ReadLine();
        }
     }
}

A) 2 3
B) 3 2
C) 3 4
D) 5 10
Ans. Option C) is correct.
Explanation: (2-3)= -1, so case -1 will be selected. And inside this case the value of i and k will increment to i=3 and k=4.

Q-2 Does switch case work properly if we remove the default statement from the code?
Ans. Yes, removing default will not cause any error, code will run successfully. Actually, we use default statement for our own convenience. Suppose if neither of the cases will support then it may execute the statement for default. That's why we use default cases and try to make habit to use them.

Click imagination hunt to see latest Blogs

Keep learning and sharing…

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions