Understanding Decision Making Statements in C# | Learn Now Tutorial
C Decision Making Tutorial ACTE

Understanding Decision Making Statements in C# | Learn Now Tutorial

Last updated on 28th Jan 2022, Blog, Tutorials

About author

Manish Kiran (C# Developer )

Manish Kiran, C# Developer Manish Kiran is a C# developer expert and subject specialist who has experience with Git, WPF, WinForms, C#,.Net, SQL, .NET Development, VB, .NET Framework,.NET Core, SVN, and Mercurial. His articles help the learners get insights into the domain.

(5.0) | 18562 Ratings 1741
    • Introduction
    • C # decision statement
    • If statement
    • If assertion
    • If Else If statement or ladder
    • Switch statement
    • Conclusion

    Subscribe For Free Demo

    [custom_views_post_title]

      Introduction :-

      In a decision-making structure, the programmer sees one or more conditions that the program evaluates or tests, one or more statements that are executed if the condition is determined to be true, and if the condition is true. You need to specify other statements to be executed. Satisfied is determined to be false. Below is a common form of typical decision-making structure found in most programming languages.


      C # decision statement :-

      C # provides the following types of decision statements: Click the links below to find out more. Senior No. Statement and Description


    Decision Making Statements in c#
    Decision Making Statements in c#

      If statement :-

      An if statement consists of a Boolean expression followed by one or more statements.

      if else statement : The if statement can be followed by an optional else statement that is executed if the Boolean expression is false.

      Nested if statement : You can use an if or else if statement within another if or else if statement.

      switch statement : You can use the switch statement to test whether a variable is equal against a list of values.

      Nested switch statement : You can use the switch statement within another switch statement.

      Decision making statements assist you to make selection primarily based totally on positive situations. These situations are particular with the aid of using a fixed of selection making statements having boolean expressions which might be evaluated to a boolean price genuine or false. There are following sorts of selection making statements in C#.


      If assertion :-

      An if assertion includes a boolean expression that is evaluated to a boolean price. If the price is genuine then if block is achieved in any other case subsequent assertion(s) might be achieved. You could have a couple of if assertion as proven below-

      Public elegance Example :


      • is two”);
      • }
      • if (result == 3)
      • }
      • }
      • Output:
      • Result is two
      • */

      You also can do nesting of if assertion method an if assertion inner any other if this is known as nested if assertion.

    Course Curriculum

    Learn Advanced C and C Plus Plus Certification Training Course to Build Your Skills

    Weekday / Weekend BatchesSee Batch Details

      If-Else assertion

      An if-else assertion includes statements – if assertion and else assertion. When the expression in an if-assertion is evaluated to genuine then if block is achieved in any other case the else block might be achieved.


      Public elegance Example

      • more than 0″);
      • }
      • else
      • identical to 0″);
      • }
      • }
      • }
      • /*
      • Output:
      • Result is smaller than or identical to 0
      • */

      If Else If statement or ladder :-

      The IfElseIf ladder is a set of statements used to test a set of conditions. If the first if statement satisfies the result, the code inside the if block is executed. Otherwise, control is passed to an else statement that contains a second “if” statement. If the second one meets the result, the code inside the if block is executed. This is continued with a series of elseif statements. If the condition does not evaluate to true, you can run the code in a standard Else block. The IfElseIf ladder contains the more specific cases above and should be generalized below.


     If Else If statement or ladder
    If Else If statement or ladder

      Public class example :

      • {
      • static void Main (string [] args)
      • {
      • char grade = `B’;
      • if (note ==’ A’)
      • {
      • Console.WriteLine (“Excellent! “);
      • }
      • else if ( note ==’B’)
      • {
      • Console.WriteLine (“Well done”);
      • }
      • else if (note ==’D’)
      • {
      • Console.WriteLine (“You pass”);
      • }
      • else if (note ==’F’)
      • {
      • Console.WriteLine (“Better try again”);
      • }
      • else
      • {
      • Console.WriteLine (“You failed!”);
      • }
      • }
      • }
      • / *
      • Output:
      • Well done
      • * /

      Switch statement :-

    • The Switch statement acts as an alternative to the long IfElseIf ladder used to test various conditions. The switch statement contains one or more case flags that are tested against the switch expression.
    • If the case matches the value with the switch result, the control continues to execute 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.

    Course Curriculum

    Get JOB Oriented C and C Plus Plus Training for Beginners By MNC Experts

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

      The accompanying principles apply to a switch explanation −

    • The articulation utilized in a switch proclamation should have a vital or identified sort, or be of a class type in which the class has a solitary change capacity to an essential or counted type.
    • You can include quite a few case proclamations inside a switch. Each case is trailed by the worth to be contrasted with and a colon.
    • The steady articulation for a case should be similar information type as the variable in the switch, and it should be a consistent or an exacting.
    • Whenever the variable being turned on is equivalent to a case, the assertions following that case will execute until a break proclamation is reached.
    • Whenever a break articulation is reached, the switch ends, and the progression of control leaps to the following line following the switch explanation.
    • Few out of every odd case needs to contain a break. Assuming that no break shows up, the progression of control will fall through to ensuing cases until a break is reached.
    • A switch explanation can have a discretionary default case, which should show up toward the finish of the switch. The default case can be utilized for playing out an errand when none of the cases is valid. No break is required in the default case.

      C # decision statement The decision statement contains the conditions that are evaluated by the program. If the condition is true, one set of instructions is executed, and if the condition is false, another set of instructions is executed. The C # has many decision statements, including if statements, if… else statements, switch statements, and so on.


      The program that points to the if statement is:

      • Source code: A program that uses the system to generate if-instruction
      • implemented in C #.
      • Namespace Demo {
      • Public Class Example {
      • public static void Main (string [] args) {
      • int a = 5;
      • if (a == 5)
      • Console.WriteLine (“value 5”);
      • }
      • }
      • }
      • The output:
      • The value of a is 5.
      • If… else statement (C #)
      • if… else statement is a modification of the if statement. The following figure shows the control flow for the if… else statement.
      • if… else statement (C #
      • )
      • if (formula)
      • {
      • // These statements are executed if the expression is true
      • }
      • different
      • {
      • // These statements will be executed if the expression evaluates to false
      • }

      A program that demonstrates the if else…if statement is as follows:

      • Source Code: Program to implement if else…else if statement in C#
      • using System;
      • namespace Constants {
      • public class Example {
      • public static void Main(string[] args) {
      • int a = 50;
      • if (a == 5)
      • Console.WriteLine(“Value of a is 5”);
      • else if (a == 10)
      • Console.WriteLine(“Value of a is 10”);
      • else
      • Console.WriteLine(“Value of a is not 5 or 10”);
      • }
      • }
      • }
      • The output:
      • Value of a is not 5 or 10

      switch statement in C#

    • The switch statement is required to test a variable value against a list of values. Each of these values is defined with a case and if none of the values match against the variable value, then the default clause is used.
    • Switch Statement Rules :

    • The steady articulations should be unique
    • Switch Statement articulations should be crude information types (int, string, bool, burn); assuming you need twofold or float you really want to utilize an If Statement
    • You should continuously incorporate the break
    • Assuming that you really want to work out your case you should utilize an If Statement
    • In contrast to Visual Basic, in C# you can’t utilize social administrators, you should utilize an If Statement

      A program that demonstrates the use of the switch statement is as follows:

      • Source Code: Program to implement switch statement in C#
      • namespace Constants {
      • public class Example {
      • public static void Main(string[] args) {
      • char grade = `A’;
      • Console.WriteLine(“The grade you obtained is {0} “, grade);
      • switch (grade) {
      • case ‘A’:
      • Console.WriteLine(“Wonderful!!”);
      • break;
      • case ‘B’:
      • Console.WriteLine(“Good Enough”);
      • break;
      • case ‘C’:
      • Console.WriteLine(“Perform Better”);
      • break;
      • case ‘D’:
      • Console.WriteLine(“Not good enough”);
      • break;
      • case ‘E’:
      • Console.WriteLine(“You Failed”);
      • break;
      • default:
      • Console.WriteLine(“Invalid Choice”);
      • break;
      • }
      • }
      • }
      • }
      • The output:
      • The grade you obtained is
      • Wonderful!

    C and C Plus Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Conclusion :-

      C# incorporates a rich arrangement of explanations that are utilized to control the progression of execution in your code. These assertions fall into three classes: Choice explanations (if and switch) Used to choose an assertion or proclamation square to be executed. These assertions are utilized to choose from among numerous ways of execution. The assuming assertion is normally used to execute explanations on the off chance that a particular condition assesses as evident. The in the event that assertion is alternatively combined with the else proclamation to compel a choice from one of two potential execution ways. Albeit the switch proclamation can be utilized to choose from a couple of conceivable execution ways, it’s most normal utilized when there are multiple conceivable execution branches.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free