top
April flash sale

Search

C# Tutorial

Decision making statements contain conditions that are evaluated by the program. If the condition is true, then a set of statements are executed and if the condition is false then another set of statements is executed.There are many decision making statements in C# such as:if statement,if…else statement,switch statement etc.Details about these are given as follows:if Statement in C#The if statement is the main decision making statement used in C#. The flow of control in the if statement is specified using the following diagram:Figure 21: if statement in C#As seen from the above diagram, statements in the body of if are executed if the if statement evaluates to true. Otherwise, these statements are skipped and control passes to the next statement after the if construct.The syntax of the if statement is given below:if (expression) {    // These statements will be executed if the expression evaluates to true }A program that demonstrates the if statement is as follows:Source Code: Program to implement if statement in C#using System; namespace Demo {   public class Example {      public static void Main(string[] args) {        int a=5;        if(a==5) Console.WriteLine("Value of a is 5");      }   } }The output of the above program is as follows:Value of a is 5If…else Statement in C#The if…else statement is a modification on the if statement. The flow of control in the if…else statement is specified using the following diagram:Figure 22: if…else statement in C#As seen from the above diagram, statements in the “body of if” are executed, if the statement evaluates to true. Otherwise, statements in the body of else are executed. After that control passes to the next statement after the if…else construct.The syntax of the if…else statement is given below:if (expression) {    // These statements will be executed if the expression evaluates to true } else {     // These statements will be executed if the expression evaluates to false }A program that demonstrates the if…else statement is as follows:Source Code: Program to implement if…else statement in C#using System; namespace Demo { public class Example {  public static void Main(string[] args) {   int a = 10;   if (a == 5)    Console.WriteLine("Value of a is 5");   else    Console.WriteLine("Value of a is not 5");  } } }The output of the above program is as follows:Value of a is not 5The if statement can also be followed by else if...else construct. This is very useful in testing multiple conditions. The syntax for this is given as follows:if (expression1) {    // These statements will be executed if the expression 1 evaluates to true } else if (expression2) {     // These statements will be executed if the expression 2 evaluates to true } else if (expression3) {     // These statements will be executed if the expression 3 evaluates to true } else {     // These statements will be executed if all the above expressions evaluate 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 of the above program is as follows:Value of a is not 5 or 10switch 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.The syntax of the switch statement is as follows:switch(expression) {    case ConstantValue  : statement(s);                                         break;    case ConstantValue  : statement(s);                                         break;    case ConstantValue  : statement(s);                                         break;    default : statement(s); }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 of the above program is as follows:The grade you obtained is A Wonderful!!
logo

C# Tutorial

Decision Making Statements in C#

Decision making statements contain conditions that are evaluated by the program. If the condition is true, then a set of statements are executed and if the condition is false then another set of statements is executed.

There are many decision making statements in C# such as:

  • if statement,
  • if…else statement,
  • switch statement etc.

Details about these are given as follows:

if Statement in C#

The if statement is the main decision making statement used in C#. The flow of control in the if statement is specified using the following diagram:

 if statement in C#

Figure 21: if statement in C#

As seen from the above diagram, statements in the body of if are executed if the if statement evaluates to true. Otherwise, these statements are skipped and control passes to the next statement after the if construct.

The syntax of the if statement is given below:

if (expression)
{
   // These statements will be executed if the expression evaluates to true
}

A program that demonstrates the if statement is as follows:

Source Code: Program to implement if statement in C#

using System;
namespace Demo {
  public class Example {
     public static void Main(string[] args) {
       int a=5;
       if(a==5)
Console.WriteLine("Value of a is 5");
     }
  }
}

The output of the above program is as follows:

Value of a is 5

If…else Statement in C#

The if…else statement is a modification on the if statement. The flow of control in the if…else statement is specified using the following diagram:

 if…else statement in C#

Figure 22: if…else statement in C#

As seen from the above diagram, statements in the “body of if” are executed, if the statement evaluates to true. Otherwise, statements in the body of else are executed. After that control passes to the next statement after the if…else construct.

The syntax of the if…else statement is given below:

if (expression)
{
   // These statements will be executed if the expression evaluates to true
}
else
{
    // These statements will be executed if the expression evaluates to false
}

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

Source Code: Program to implement if…else statement in C#

using System;
namespace Demo {
public class Example {
 public static void Main(string[] args) {
  int a = 10;
  if (a == 5)
   Console.WriteLine("Value of a is 5");
  else
   Console.WriteLine("Value of a is not 5");
 }
}
}

The output of the above program is as follows:

Value of a is not 5

The if statement can also be followed by else if...else construct. This is very useful in testing multiple conditions. The syntax for this is given as follows:

if (expression1)
{
   // These statements will be executed if the expression 1 evaluates to true
}
else if (expression2)
{
    // These statements will be executed if the expression 2 evaluates to true
}
else if (expression3)
{
    // These statements will be executed if the expression 3 evaluates to true
}
else
{
    // These statements will be executed if all the above expressions evaluate 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 of the above program is as follows:

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.

The syntax of the switch statement is as follows:

switch(expression)
{
   case ConstantValue  : statement(s);
                                        break;
   case ConstantValue  : statement(s);
                                        break;
   case ConstantValue  : statement(s);
                                        break;
   default : statement(s);
}

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 of the above program is as follows:

The grade you obtained is A
Wonderful!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial