Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Ternary operator file under Control Structure #684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Language/Structure/Control Structure/ternary.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: ternary
categories: [ "Structure" ]
subCategories: [ "Control Structure" ]
---





= ?: Ternary Operator


// OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
It takes three arguments rather than the typical one or two that most operators use. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. It is an alternative to shorten an if-else block. It can help increase the readability and reduce the number of lines in your code.
[%hardbreaks]


[float]
=== Syntax
`expression_1 ? expression_2 : expression_3; // if expression_1 evaluates to true then expression_2 is evaluated else expression_3`


[float]
=== Parameters
`expression`. An expression is any legal combination of symbols that represents a value.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`expression`. An expression is any legal combination of symbols that represents a value.
`expression`: An expression is any legal combination of symbols that represents a value.

Use standard format for parameter documentation:

`pin`: the number of the pin to write to. Allowed data types: int.


--
// OVERVIEW SECTION ENDS



// HOW TO USE SECTION STARTS
[#howtouse]
--

[float]
=== Example Code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The statement checks the conditional expression and assigns `z` the value of `x`, the expression `x<y` is `true`.

I think this location is more appropriate for the description, rather than the previous location as a comment at the end of the code block.

[source,arduino]
----
int x = 10, y = 20, z; // declared and defined three variables x, y and z
z = (x < y) ? x : y;
Comment on lines +47 to +48
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int x = 10, y = 20, z; // declared and defined three variables x, y and z
z = (x < y) ? x : y;
int x = 10;
int y = 20;
int z = (x < y) ? x : y;

Beginners are likely to be unfamiliar with the ability to declare multiple variables in one statement. This is not documented in the Arduino Language Reference and they won't understand that the language contains all the capabilities of C++ and that the missing documentation for those capabilities can be found in any C++ reference, since this fact is not documented (#623).

In this case, using a separate statement for each declaration is actually just as good because the function of the code is now so clear that an explanatory comment is unnecessary.

// the statement checks the conditional expression and assigns z the value of x, the expression x<y is true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the statement checks the conditional expression and assigns z the value of x, the expression x<y is true

This accompanies my suggestion above to move the description out of the code block.

----
[%hardbreaks]


--
// HOW TO USE SECTION ENDS




// SEE ALSO SECTION
[#see_also]
--

[float]
=== See also

[role="language"]


--
// SEE ALSO SECTION ENDS