Making Decisions in your program – C# Decision Making

 

Introduction:

Sometimes ,We have a many situations where we may have to change the order  of execution of statements based on user input . Based on some kind of condition, the program can choose a different set of instructions to run. In C#, various types of decision making statements are available such as if,if..else, else..if, switch etc.But if Condition is most widely used in C# .Know about the Basics of C# before further reading.

What is Decision Making ?

Decision making statements help you to make decision based on certain conditions. Making Decisions in your progrm is required by  the programmer to specify one or many conditions to be evaluated or tested , along with a statement or statements to be executed. If the condition is find to be true, and optionally, other statements to be executed if the condition is determined to be false.

If Statement:

 If Statement is used for all kinds of things in your program. In this statement you write on if statement the  keyword  if is used. If Statement followed by an Boolean expression.

syntax For  if statement

if(condition expression)

{

statement;

}

Flow Diagram:

if_statement

If – else Statement:

if-else statement consists of two statements . if statement and else statement. the code should execute only one of these two different paths.When specified condition fail the else statement will get execute.

Syntax  for  if-else statement

if(condition expression 2)

{

statement;

}

else

{

statement;

}

Flow Diagram:

if_else_statement

Switch Statement:

A switch statement allows a code to be tested for equality against a list of cases. The Switch Keyword is used by the different cases. When one case matches the value with the result of switch expression, the control continues executing the code from that label.

Syntax for Switch Statement

switch(expression)

{

case 1:

statement;

case 2:

statement;

case 3:

statement;

default:

statement;

}

Flow Diagram:

switch_statement

Conclusion:

Decision Making are a key part of programming. They let you make decisions about how things are going to work. No programming language is complete without some form of if-statement or else if statement.