Decision Making in C#

CsharpProgrammingServer Side Programming

Decision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Decision Making in C# includes if statement, if-else statement, switch statement, etc.

Let us see an example of if statement in C#.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         int x = 5;
         // if statement
         if (x < 20) {
            /* if condition is true then print the following */
            Console.WriteLine("x is less than 20");
         }
         Console.WriteLine("value of x is : {0}", x);
         Console.ReadLine();
      }
   }
}

Output

x is less than 20
value of x is : 5

Let us see an example of if-else statement in C#. In this, if the first condition is false, the condition in the else statement will be checked.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         int x = 100;
         /* check the boolean condition */
         if (x < 20) {
            /* if condition is true then print the following */
            Console.WriteLine("x is less than 20");
         } else {
            /* if condition is false then print the following */
            Cohttp://tpcg.io/HoaKexnsole.WriteLine("x is not less than 20");
         }
         Console.WriteLine("value of a is : {0}", x);
         Console.ReadLine();
      }
   }
}

Output

x is not less than 20
value of a is : 100
raja
Published on 17-Sep-2018 08:15:25
Advertisements