Fixed example (#32827)
Replaced assignment operator with the equality operator. Fixed example structure. Improved language.
This commit is contained in:
committed by
Randell Dawson
parent
2874baa0c6
commit
f743f1e732
@ -2,14 +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 different blocks of code depending on the truthfulness of the specified condition.
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
```c#
|
|
||||||
|
|
||||||
|
## Syntax
|
||||||
|
```C#
|
||||||
if (boolean expression)
|
if (boolean expression)
|
||||||
{
|
{
|
||||||
// execute this code block if expression evalutes to true
|
// execute this code block if expression evalutes to true
|
||||||
@ -18,32 +17,33 @@ else
|
|||||||
{
|
{
|
||||||
// always execute this code block when above if expression is false
|
// always execute this code block when above if expression is false
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
```C#
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
Else
|
|
||||||
{
|
{
|
||||||
Console.WriteLine("Price is not equal to 30.");
|
Console.WriteLine("Price is not equal to 30.");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Since we already declared our integer `Price` to be 30, this will be the expected output.
|
||||||
|
|
||||||
|
### Output
|
||||||
Since we already declared our int Price to be 30, this will be the expected 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.
|
## Shortened If...Else Statement
|
||||||
|
|
||||||
|
We can use the ternary `:?` which is great for short if...else statements.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
```C#
|
```C#
|
||||||
int Price = 30;
|
int Price = 30;
|
||||||
|
Reference in New Issue
Block a user