Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding decision making statements in C#

  Author : Shailendra Chauhan
Posted On : 28 Apr 2014
Total Views : 98,040   
Updated On : 26 Sep 2016
 

Decision making statements help you to make decision based on certain conditions. These conditions are specified by a set of decision making statements having boolean expressions which are evaluated to a boolean value true or false. There are following types of decision making statements in C#.

  1. If statement

    An if statement consists of a boolean expression which is evaluated to a boolean value. If the value is true then if block is executed otherwise next statement(s) would be executed.

    You can have multiple if statement as shown below-

    public class Example
    {
     static void Main()
     {
     int a = 5, b = 2;
     int result = a / b;
    
     if (result == 2)
     {
     Console.WriteLine("Result is 2");
     }
     if (result == 3)
     {
     Console.WriteLine("Result is 3");
     }
     }
    }
    /* Output
     Result is 2
     */
    

    You can also do nesting of if statement means an if statement inside another if that is called nested if statement.

  2. If-Else statement

    An if-else statement consists of two statements – if statement and else statement. When the expression in an if-statement is evaluated to true then if block is executed otherwise the else block would be executed.

    public class Example
    {
     static void Main()
     {
     int a = 5, b = 6;
     int result = a - b;
     if (result > 0)
     {
     Console.WriteLine("Result is greater than zero");
     }
     else
     {
     Console.WriteLine("Result is smaller than or equal to zero");
     }
     }
    }
    /* Output 
    Result is smaller than or equal to zero
     */
    
  3. If-Else-If statement or ladder

    The If-Else-If ladder is a set of statements that is used to test a series of conditions. If the first if statement meet the result then code within the if block executes. If not, control passes to the else statement, which contains a second "if" statement. If second one meet the result then code within the if block executes. This continues as a series of else if statements. A default else code block may execute when no condition has been evaluated to true.

    If-Else-If ladder must contain more specific case at the top and generalize case at the bottom.

    public class Example
    {
     static void Main(string[] args)
     {
     char grade = 'B';
    
     if (grade == 'A')
     {
     Console.WriteLine("Excellent!");
     }
     else if (grade == 'B')
     {
     Console.WriteLine("Well done");
     }
     else if (grade == 'D')
     {
     Console.WriteLine("You passed");
     }
     else if (grade == 'F')
     {
     Console.WriteLine("Better try again");
     }
     else
     {
     Console.WriteLine("You Failed!");
     }
     }
    }
    /* Output
     Well done 
    */
    
  4. Switch statement

    Switch statement acts as a substitute for long If-Else-If ladder that is used to test a series of conditions. A switch statement contains one or more case labels which are tested against the switch expression.

    When one case matches the value with the result of switch expression, the control continues executing the code from that label. When no case label contains a matching value, control is transferred to the default section, if it exists. If there is no default section, no action is taken and control is transferred outside the switch statement.

    public class Example
    {
     static void Main(string[] args)
     {
     char grade = 'B';
    
     switch (grade)
     {
     case 'A':
     Console.WriteLine("Excellent!");
     break;
     case 'B':
     case 'C':
     Console.WriteLine("Well done");
     break;
     case 'D':
     Console.WriteLine("You passed");
     break;
     case 'F':
     Console.WriteLine("Better try again");
     break;
     default:
     Console.WriteLine("You Failed!");
     break;
     }
     }
    }
    /* Output
     Well done 
    */
    

    Key points about Switch statement

    1. Each case label specifies a constant value.

    2. A switch statement can have multiple switch sections, and each section can have one or more case labels.

    3. Unlike C and C++, C# does not allow continuing execution from one switch section to the next. It means each switch section must be separated by a break or other jump statement such as goto, return and throw.

    4. Unlike If-Else-If ladder, it is not mandatory to put more specific case at the top and generalize case at the bottom since all the switch cases have equal precedence.

What do you think?

I hope you will enjoy the decision making statements while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
9 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

Know More
6 MAR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
6 MAR
Angular 2 Master Class Fast Track Online Development Training (Online)

10:30 AM- 01:30 PM IST

Know More
27 FEB
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

Know More
25 FEB
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30 AM

Know More
25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

3:00 PM-4:30 PM

Know More
14 FEB
.NET Development (Classroom)

11:00 AM-12:00 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (Classroom)

04:00 PM-05:30 PM

10 JAN
ASP.NET MVC with AngularJS Development (Online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

5 NOV
ASP.NET MVC with AngularJS Development (Classroom)

08:00 AM-09:30 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+