Skip to content

Terenary--operators#10

Open
Butcher3Years wants to merge 11 commits intomainfrom
Terenary--operators
Open

Terenary--operators#10
Butcher3Years wants to merge 11 commits intomainfrom
Terenary--operators

Conversation

@Butcher3Years
Copy link
Copy Markdown
Owner

C++ Ternary Operator (Conditional Operator) ? :

The ternary operator is the shortest way to write a simple if-else in C++.
It's called "ternary" because it takes three operands.

Syntax

condition ? value_if_true : value_if_false;

Nested Ternary (Chained) – Use with Caution!

// OK for simple cases
int grade = (marks >= 90) ? 'A' 
          : (marks >= 80) ? 'B' 
          : (marks >= 70) ? 'C' 
          : 'F';

// AVOID this mess – becomes unreadable very fast
int result = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);  // hard to read!



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant