From e28dcbe393ed51d2f918cdad47e8b81588c7bb75 Mon Sep 17 00:00:00 2001 From: Abdul Karim Date: Sun, 15 Feb 2026 11:34:13 +0500 Subject: [PATCH 1/2] 12.7. Conditional Operator: Introduce chaining ternay operators --- src/branching_logic/conditional_operator.md | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/branching_logic/conditional_operator.md b/src/branching_logic/conditional_operator.md index 91c2b28..82dc74a 100644 --- a/src/branching_logic/conditional_operator.md +++ b/src/branching_logic/conditional_operator.md @@ -23,4 +23,31 @@ and then a value to use when that condition evaluates to `false`. CONDITION ? WHEN_TRUE : WHEN_FALSE ``` +Just like multiple if else statements, we have the concept to "chain" ternary operators. If the first condition is not true, it goes to the next condition and so on. + +```java +~void main() { +int age = 22; + +String message = age < 0 + ? "You are not born yet" + : age > 100 + ? "Sorry, you are too old." + : age < 25 + ? "You cannot rent a car!" + : "You might be able to rent a car"; + +IO.println(message); +~} +``` + +It is same as before, you write a condition followed by a `?`, a value to use when that condition evaluates to `true`, a `:`, +and then _chain_ another condition to use when that condition evaluates to `false`. + +Finally, if no condition was true, it matches the value after the final `:`. + +```java,no_run +CONDITION ? WHEN_TRUE : CONDITION ? WHEN_TRUE : CONDITION ? WHEN_TRUE ... : WHEN_ALL_FALSE +``` + [^ternary]: Some people will call this a ternary expression. Ternary meaning "three things." Same idea as tres leches. From 945f48ec44e7c48cb6366c7eaa53e18b01cc3b7c Mon Sep 17 00:00:00 2001 From: Abdul Karim Date: Sun, 15 Feb 2026 11:41:28 +0500 Subject: [PATCH 2/2] Update age < 0 condition --- src/branching_logic/conditional_operator.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/branching_logic/conditional_operator.md b/src/branching_logic/conditional_operator.md index 82dc74a..2d8f9a6 100644 --- a/src/branching_logic/conditional_operator.md +++ b/src/branching_logic/conditional_operator.md @@ -29,8 +29,8 @@ Just like multiple if else statements, we have the concept to "chain" ternary op ~void main() { int age = 22; -String message = age < 0 - ? "You are not born yet" +String message = age <= 0 + ? "You do not exist yet" : age > 100 ? "Sorry, you are too old." : age < 25