Decision Statements LEARNOVITA

Understanding Decision Making Statements in C# | Learn Now Tutorial

Last updated on 12th Aug 2022, Blog, Tutorials

About author

Manish Kiran (C# Developer )

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

(5.0) | 19769 Ratings 2214

Introduction :

In a decision-making structure, the software engineer sees one or a lot of conditions that the program evaluates or tests, one or a lot of statements that are dead if the condition is set to be true, and if the condition is true. you wish to specify alternative statements to be dead. glad is set to be false. Below could be a common variety of typical decision-making structure found in most programming languages.

C # call statement :

C # provides the subsequent sorts of call statements: Click the links below to seek out a lot of. Senior No. Statement and outline

Decision Making Statements in c#

Decision Making Statements in c#

If statement :

If statement

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

if else statement: The if statement may be followed by an associate non obligatory else statement that’s dead if the Boolean expression is fake.

Nested if statement: you’ll be able to use associate if alternatively if statement at intervals another if alternatively if statement.

switch statement: you’ll be able to use the switch statement to check whether or not a variable is equal against an inventory of values.

Nested switch statement: you’ll be able to use the switch statement at intervals another switch statement.

Decision making statements assist you to create choice entirely} totally on positive things. These things are specific with the help of employing a fastener of choice creating statements having Boolean expressions which could be evaluated to a Boolean value real or false. There are following types of choice creating statements in C#.

If assertion :

An if assertion includes a mathematician expression that’s evaluated to a mathematician value. If the worth is real then if block is achieved in the other case resultant assertion(s) can be achieved. you may have a few of if assertion as established below-

Public magnificence Example :

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

You also will do nesting if assertion methodology Associate in Nursing if assertion inner the other if this is often called nested if assertion.

If-Else assertion

An if-else assertion includes statements – if assertion and else assertion. Once the expression in Associate in Nursing if-assertion is evaluated to real then if block is achieved in the other case the else block can be achieved.

Public magnificence Example :

  • more than 0″);
  • }
  • else
  • identical to 0″);
  • }
  • }
  • }
  • /*

Output:

Result is smaller than or the image of zero

*/

If Else If statement or ladder :

If Else If statement

The IfElseIf ladder may be a set of statements wont to check a collection of conditions. If the primary if statement satisfies the result, the code within the if block is dead. Otherwise, management is passed to the Associate in Nursing else statement that contains a second “if” statement. If the other meets the result, the code within the if block is dead. This is often continuing with a series of elseif statements. If the condition doesn’t happen to be true, you’ll run the code in a very normal Else block. The IfElseIf ladder contains a lot of specific cases on top of and will be generalized below.

Public category example :

  • else if ( note ==’B’)
  • else if (note ==’D’)
  • else if (note ==’F’)
  • attempt again”);
  • }
  • else
  • }
  • }
  • / *

Output:

Well done

* /

Switch statement :

The Switch statement acts as another to the long IfElseIf ladder to check numerous conditions. The switch statement contains one or additional case flags that square measure tested against the switch expression. If the case matches the worth with the switch result, the management continues to execute code from that label. Once no case label contains an identical worth, management is transferred to the default section, if it exists. If there’s no default section, no action is taken and management is transferred outside the switch statement.

The attendant principles apply to a switch clarification clarification

The articulation used during a switch proclamation ought to have an important or known kind, or be of a category kind within which the category contains a solitary amendment capability to an important or counted kind.You can embody quite a few case proclamations within a switch. Every case is trailed by the value to be contrasted with a colon.The steady articulation for a case ought to be similar info kind because the variable within the switch, ANd it ought to be the same or an exacting. Whenever the variable being turned on resembles a case, the assertions following that case can execute till an opening proclamation is reached.Whenever an opening articulation is reached, the switch ends, and also the progression of management leaps to the subsequent line following the switch clarification.Few out of each odd case has to contain an opening. forward that no break shows up, the progression of management can go wrong to succeeding cases till an opening is reached.A switch clarification will have a discretionary default case that ought to show up toward the end of the switch. The default case is used for enjoying an AN trip once none of the cases is valid. No break is needed within the default case.

The program that points to the if statement is:

Source code: A program that uses the system to come up with if-instruction implemented in C #.

Namespace Demo category Example

  • }
  • }

The output:

The value of a is five.

  • If… else statement (C #)
  • if… else statement may be a modification of the if statement. The subsequent figure shows the management flow for the if… else statement.
  • if… else statement (C #
  • )
  • if (formula)
  • ar dead if the expression is true
  • }
  • different
  • are going to be dead 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 category Example isn’t five or 10”);
  • }
  • }
  • }

The output:

Value of a isn’t five or ten

switch statement in C#

switch statement

The switch statement is needed to check a variable worth against an inventory of values. every of those values is outlined with a case and if none of the values match against the variable value, then the default clause is employed.

Switch Statement Rules :

  • The steady articulations ought to be distinctive.
  • Switch Statement articulations ought to be crude data sorts (int, string, bool, burn); forward you would like twofold or float you actually need to utilize associate If Statement.
  • You should unendingly incorporate the break.
  • Assuming that you just really need to figure out your case you must utilize associate If Statement.
  • In distinction to Visual Basic, in C# you can’t utilize social directors, you must utilize associate If Statement

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

  • namespace Constants category Example “, grade);
  • switch (grade) smart enough”);
  • break;
  • case ‘E’:
  • Console.WriteLine(“You Failed”);
  • break;
  • default:
  • Console.WriteLine(“Invalid Choice”);
  • break;
  • }
  • }
  • }
  • }

The output:

The grade you obtained is Wonderf

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. 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 normally utilized when there are multiple conceivable execution branches.

Are you looking training with Right Jobs?

Contact Us

Popular Courses