- 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
How do we use nested if statements in C#?
Use one if or else if statement inside another if or else if statement(s). The syntax for a nested if statement is as follows −
if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
The following is an example showing the usage of nested if statements in C#. Here, we have two if statement that checks two conditions.
if (a == 5) { /* if condition is true then check the following */ if (b == 10) { /* if condition is true then print the following */ Console.WriteLine("Value of a is 5 and b is 10"); } }
Here is the complete example.
Example
using System; namespace Demo { class Program { static void Main(string[] args) { //* local variable definition */ int a = 5; int b = 10; /* check the boolean condition */ if (a == 5) { /* if condition is true then check the following */ if (b == 10) { /* if condition is true then print the following */ Console.WriteLine("Value of a is 5 and b is 10"); } } Console.WriteLine("Exact value of a is : {0}", a); Console.WriteLine("Exact value of b is : {0}", b); Console.ReadLine(); } } }
Output
Value of a is 5 and b is 10 Exact value of a is : 5 Exact value of b is : 10
Advertisements