Short if Else Statements (#28225)
* Short if Else Statements * Update index.md
This commit is contained in:
committed by
The Coding Aviator
parent
5d0cea176f
commit
fc58f59710
@ -2,12 +2,13 @@
|
|||||||
title: If Else Statement
|
title: If Else Statement
|
||||||
---
|
---
|
||||||
|
|
||||||
# If Else Statement
|
# If Else Statement
|
||||||
|
|
||||||
The If-Else statement executes a block of code depending on whether your precondition is fullfilled or not.
|
The If-Else statement executes a block of code depending on whether your precondition is fullfilled or not.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
```
|
|
||||||
|
```c#
|
||||||
|
|
||||||
if(boolean expression)
|
if(boolean expression)
|
||||||
{
|
{
|
||||||
@ -21,7 +22,7 @@ else
|
|||||||
|
|
||||||
int Price = 30;
|
int Price = 30;
|
||||||
|
|
||||||
If (Price = 30)
|
If (Price == 30)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Price is equal to 30.");
|
Console.WriteLine("Price is equal to 30.");
|
||||||
}
|
}
|
||||||
@ -32,9 +33,19 @@ Else
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Since we already declared our int Price to be 30, this will be the expected output.
|
Since we already declared our int Price to be 30, this will be the expected output.
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
```
|
```
|
||||||
Price is equal to 30.
|
Price is equal to 30.
|
||||||
```
|
```
|
||||||
|
## Shorten If Else Statement
|
||||||
|
|
||||||
|
We can use the ternary :? which is great for short if else statements.
|
||||||
|
For example:
|
||||||
|
```C#
|
||||||
|
int Price=30;
|
||||||
|
(Price==30)?Console.WriteLine("Price is Equal to 30."):Console.WriteLine("Price is Not Equal to 30.")
|
||||||
|
```
|
||||||
|
Reference in New Issue
Block a user