- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decision Making in C#
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
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
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
Advertisements