Ternary Operations outside of coding?

Click For Summary
SUMMARY

The discussion centers on the use of ternary operations in web development, specifically the syntax x = y > z ? a : b. This operation serves as a shorthand for traditional if...else statements, allowing for concise conditional assignments. Participants clarify that there are no mathematical properties associated with the ternary operator, emphasizing that it cannot be bypassed in favor of direct assignments without performing the comparison.

PREREQUISITES
  • Understanding of ternary operations in programming languages
  • Familiarity with conditional statements (if...else)
  • Basic knowledge of inequalities and their properties
  • Experience with web development concepts
NEXT STEPS
  • Explore the use of ternary operations in JavaScript and other programming languages
  • Study the implications of conditional statements on code readability and performance
  • Investigate the mathematical properties of inequalities in programming contexts
  • Learn about best practices for using shorthand syntax in code
USEFUL FOR

Web developers, software engineers, and anyone interested in optimizing code readability and understanding conditional logic in programming.

OMGCarlos
Messages
28
Reaction score
0
I use the ternary operation in web development all the time, eg:
x = y > z ? a : b

Which reads, if x > y then a, otherwise b

I realize that it is just a shorthand for if...else statements, but even still, are there any mathematical properties associated with it? I know inequalities have special properties - for example, dividing both sides by a negative.

I recently picked up an elementary book on set theory and I keep thinking about this. It would be interesting to know that in certain cases, I can just skip the operation and automatically assign x to a-something
 
Mathematics news on Phys.org
OMGCarlos said:
I use the ternary operation in web development all the time, eg:
x = y > z ? a : b

Which reads, if x > y then a, otherwise b

I realize that it is just a shorthand for if...else statements, but even still, are there any mathematical properties associated with it? I know inequalities have special properties - for example, dividing both sides by a negative.

I recently picked up an elementary book on set theory and I keep thinking about this. It would be interesting to know that in certain cases, I can just skip the operation and automatically assign x to a-something

There aren't any mathematical properties associated with this operator. As you note, it's just a short hand way of writing if ( ... ) then (...).

Your explanation of your example is not quite right.

x = y > z ? a : b

means if y > z, then set x to a. Otherwise, set x to b.

This is equivalent to the following C code:
Code:
if (y > z)
{
   x = a;
}
else
{
   x = b;
}

In answer to your question, no, you can't skip the comparison.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
18
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K