From 98f52a448e174a3e43ac04fe06f514757837a222 Mon Sep 17 00:00:00 2001 From: SimonFuet <42030442+SimonFuet@users.noreply.github.com> Date: Wed, 17 Apr 2019 14:01:20 +0200 Subject: [PATCH] Add ternary operator to control flow statements (#29269) * Add ternary operator to control flow statements * Remove external links, formatted code and removed useless codeblock --- guide/english/java/control-flow/index.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/guide/english/java/control-flow/index.md b/guide/english/java/control-flow/index.md index a3acd6a99f..f13dc1eecc 100644 --- a/guide/english/java/control-flow/index.md +++ b/guide/english/java/control-flow/index.md @@ -98,4 +98,15 @@ if( cash < 25 ){ ``` In this example, `meetFriendsAtSportsBar()` will be executed. -![:rocket:](https://forum.freecodecamp.org/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") Run Code +* `ternary operator` + +The ternary operator is a nice alternative to simple `if...else` structures. It is an expression taking 3 operands that allows you to write in one line what you would otherwise have written in 4-6 lines. This expression returns a value, so it can be used in assignments or as a continionnal parameter when calling a function. +```java +(condition) ? valueIfTrue : valueIfFalse +``` +```java +int number = 15; +String s = number > 10 ? "high" : "low"; + +System.out.println(number > 10 ? "high" : "low"); +```