Added additional section and update previous section (#23998)

* Added additional section and update previous section

Added an additional section for complex ternary operations and updated previous section to have csharp flavored code rendering.

* Paulywill requested changes

Paulywill requested changes: removed any "I" terminology and my personal REPL link.

* Update index.md

Changed the subheading markdown.

* Update index.md
This commit is contained in:
Marcus Parsons
2019-03-21 10:58:01 -04:00
committed by Paul Gamble
parent d0f66141ef
commit 69a4eb67fe

View File

@ -21,38 +21,51 @@ Returned if `condition_expression` is true.
Returned if `condition_expression` is false. Returned if `condition_expression` is false.
## Example ## Example
``` ```csharp
// initialize - set true or false here to view different result
bool hasFreeSweets = false;
string str = hasFreeSweets ? "Free sweets!" : "No free sweets.";
//output in console
Console.WriteLine(str);
```
## Equivalent if...else Statement
```
// initialize - set true or false here to view different result // initialize - set true or false here to view different result
bool hasFreeSweet = false; bool hasFreeSweet = false;
string str; string str = hasFreeSweet ? "Free sweet!" : "No free sweet.";
if(hasFreeSweet){
str = "Free sweet!";
} else {
str = "No free sweet.";
}
//output in console //output in console
Console.WriteLine(str); Console.WriteLine(str);
``` ```
## Output ### Output
``` ```csharp
if hasFreeSweets == true if hasFreeSweet == true
> Free sweets! > Free sweet!
if hasFreeSweet == false
> No free sweet.
if hasFreeSweets == false
> No free sweets.
``` ```
## Complex Ternary Operations
Ternary operations can also be embedded inside each other to create one line of execution rather than several lines. *It is important to take into account that this may affect the readability of your code.*
To embed more ternary operations within another ternary operation, you only have to follow the same pattern as above with each new ternary operation added. You will start with a primary ternary operation, which will consequently lead to your other ternary operations based on boolean expressions. You can branch each ternary operation based off of the `true` and/or `false` paths of the ternary operations, but we are going to focus on going off the `false` path for the ternary operation for the example code below, as most complicated ternary operations will be embedded in the `false` path.
Let's say we have a result `myResult` that is an `int` and can be 0, 1, or greater than 1. And we would like to output `"Just Starting"` if `myResult` is 0, `"First Place"` if `myResult` is 1, and `"If you're not first, you're last."` if `myResult` is 2 or greater. So, we need 3 outcomes for this ternary operation.
## Example
```csharp
//Initialize myResult with a 4
int myResult = 4;
//Output to the console the results of having a 4 in myResult based on what is expected.
//The extra parenthesis after "Just Starting" is for grouping
//and is not necessary for the code to execute as expected
Console.WriteLine((myResult == 0) ? "Just Starting" :
//second ternary operation which is a part of the first operation (when myResult is not equal to 0)
((myResult == 1) ? "First Place" :
//the last part of the last ternary operation (when myResult is not equal to 1)
"If you're not first, you're last."));
//Output:
>If you're not first, you're last.
```
You can continue this pattern to embed as many ternary operations as you wish, although it is **not recommended** to use more than a couple ternary operations in your code as it will drastically reduce the readability if you need to have someone else read it, contribute to it, etc.