If-Else Statement In C#
Decision making is one of the most important concept in any programming language. Since programming is all about making softwares that makes our lives easier and in real life we make many decisions. So we require something in programming that mimics this event. We have if-else and loops to handle scenarios like this.
You must have faced these kinds of situations or similar situations where you have taken some decision for yourself or for others. We normally say these things in our daily life -
1. Check if it's raining outside, I will take the umbrella with me. (If-Then)
2. Check if pizza is available, I will have pizza otherwise I will eat sandwich. (If-Else)
3. I will keep practising till I succeed. (Loop)
Above examples of if-else statements can be implemented in C#. Let us make it more clear and learn how to implement if statement in C#.
This is how to implement if-then condition in C#. These are also known as Conditional Statement in C#. Boolean expression returns either true or false. If condition is true, then the code following it will be executed. In C#, block of statements is enclosed using parenthesis { }. In the above scenario, code is only executed when the condition is true, if you want to execute some code when the condition is false, use if-else statement as shown below -
We can implement the above mentioned scenarios in C# as shown below. pizzaIsAvailable is a boolean variable. If it is true then you can eat pizza - eatPizza() or if it is false then you can eat sandwich - eatSandwich()
You can use if-else statement within if-else statement. This is called as Nested If Else Statement. Let us take one example to make it more clear - Program to check the number is greater than 10 and less than 100 and number should also be divisible by 2 and 3.
Suppose you have to check the relation between 2 numbers A and B - whether A > B or A < B or A == B. You can implement this condition using nested if else
But for this you can use else-if statement as well. This statement is used where you have multiple conditions and either of them is true. Above program can be implemented like this using else-if statement -
Both programs achieve similar results but else-if statement enhances the readability of the program. There is no difference in these statements - use whatever you like. Generally programmers like to use else-if where it is appropriate i.e. if there are multiple conditions to check for and only one is true, try using else if statement.
Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)
You must have faced these kinds of situations or similar situations where you have taken some decision for yourself or for others. We normally say these things in our daily life -
1. Check if it's raining outside, I will take the umbrella with me. (If-Then)
2. Check if pizza is available, I will have pizza otherwise I will eat sandwich. (If-Else)
3. I will keep practising till I succeed. (Loop)
Above examples of if-else statements can be implemented in C#. Let us make it more clear and learn how to implement if statement in C#.
In English | In C# |
If this condition is true, I will do this. | if (boolean expression) { //execute what you want to do } |
This is how to implement if-then condition in C#. These are also known as Conditional Statement in C#. Boolean expression returns either true or false. If condition is true, then the code following it will be executed. In C#, block of statements is enclosed using parenthesis { }. In the above scenario, code is only executed when the condition is true, if you want to execute some code when the condition is false, use if-else statement as shown below -
if(boolean expression) { // this code will be executed when the boolean expression is true } else { // this code will be executed when the boolean expression is false }
Example of if-else statement -
We can implement the above mentioned scenarios in C# as shown below. pizzaIsAvailable is a boolean variable. If it is true then you can eat pizza - eatPizza() or if it is false then you can eat sandwich - eatSandwich()
if(pizzaIsAvailable) { eatPizza(); } else { eatSandwich(); }
Nested if Else Statement
You can use if-else statement within if-else statement. This is called as Nested If Else Statement. Let us take one example to make it more clear - Program to check the number is greater than 10 and less than 100 and number should also be divisible by 2 and 3.
if(number > 10 && number < 100) { if(number % 2 == 0 && number % 3 == 0) { Console.WriteLine("Congratulations! You have entered the right number"); } else { Console.WriteLine("Entered number is between 10 and 100 but it is not divisible by either 2 or 3"); } } else { Console.WriteLine("Please enter number between 10 and 100"); }
Else If Statement
Suppose you have to check the relation between 2 numbers A and B - whether A > B or A < B or A == B. You can implement this condition using nested if else
if( A == B ) { Console.WriteLine ("A is equal to B"); } else { if( A > B ) { Console.WriteLine ("A is greater than B"); } else { Console.WriteLine ("A is smaller than B"); } }
But for this you can use else-if statement as well. This statement is used where you have multiple conditions and either of them is true. Above program can be implemented like this using else-if statement -
if(A == B) { Console.WriteLine ("A is equal to B"); } else if (A > B) { Console.WriteLine ("A is greater than B"); } else { Console.WriteLine ("A is smaller than B"); }
Both programs achieve similar results but else-if statement enhances the readability of the program. There is no difference in these statements - use whatever you like. Generally programmers like to use else-if where it is appropriate i.e. if there are multiple conditions to check for and only one is true, try using else if statement.
Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)
Comments
Post a Comment
Hey there, liked our post. Let us know.
Please don't put promotional links. It doesn't look nice :)