Ad space (banner)
Lesson 18 / 20

The ternary operator

This lesson covers the ternary operator, which fits an if statement onto one line.

Writing condition ? value_if_true : value_if_false picks between two values on a single line.

The sample code uses the ternary operator to decide between "Pass" and "Fail" based on int score = 75;.

A common early stumble is overusing it until the code becomes hard to read. It is handy for a simple branch, but nesting them tends to make things less clear rather than more.

In professional work, the ternary operator turns up constantly inside Flutter UI code for switching a displayed value on a condition.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)