top

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 *

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