Piecewise functions in Desmos use curly braces and conditional statements to graph different formulas over different ranges
A piecewise function is a function that uses different equations for different parts of its domain — for example, one formula when x is less than 0 and a different formula when x is greater than or equal to 0. Desmos handles this with a syntax that lets you specify conditions directly in the equation box.
The basic structure is: {condition: formula, condition: formula}. You type the conditions using inequality symbols, and Desmos evaluates them left to right, graphing whichever formula matches first. This approach works for any number of pieces, though readability drops after three or four.
The most common mistake is forgetting that Desmos reads conditions in order and stops at the first match. If your first condition catches everything, the rest never run. Ordering matters.
Key Takeaways
- Piecewise functions in Desmos use curly braces with conditions separated by colons: {x < 0: -x, x >= 0: x} creates an absolute value function.
- Conditions are evaluated left to right, so place the most restrictive conditions first to avoid later pieces being ignored.
- You can nest piecewise functions inside each other or use them in more complex expressions like y = {x < 2: x^2, x >= 2: 2x}.
- Desmos displays piecewise functions as continuous or broken lines depending on whether the pieces connect at their boundaries.
The basic syntax for two-piece functions
Start by opening Desmos in your browser and clicking in the expression list on the left. Type an equation using the piecewise syntax. For a simple example, enter y = {x < 0: -x, x >= 0: x}. This creates the absolute value function: when x is negative, the output is -x; when x is zero or positive, the output is x.
The curly braces are required. Inside them, each piece takes the form condition: formula. Separate multiple pieces with commas. Desmos accepts standard inequality symbols: <, >, <=, and >=. You can also use = for equality, though this is rarely useful in piecewise functions since a single point has measure zero.
After you type the equation, press Enter. Desmos graphs the result immediately. If you see a blank graph, check that your conditions actually cover the visible range. For instance, y = {x > 100: x} will appear empty if you are zoomed in near the origin.
Ordering conditions to avoid overlap
Desmos evaluates conditions from left to right and uses the first one that is true. This means the order of your pieces matters. If you write y = {x >= 0: x, x < 5: x^2}, the second piece never runs for any x less than 5, because all negative numbers fail the first condition and all non-negative numbers pass it.
The correct order depends on your intent. For a function that is linear when x is less than 2 and quadratic otherwise, write y = {x < 2: x, x >= 2: x^2}. The first condition catches everything below 2, and the second catches everything from 2 onward. No overlap, no gaps in logic.
If you have three or more pieces, sort them so the most specific conditions come first. For example: y = {x < -1: 1, x < 1: 0, x >= 1: -1} works because the first condition is the narrowest, the second catches the middle range, and the third catches the rest.
Creating functions with more than two pieces
Add more pieces by separating them with commas inside the curly braces. A three-piece function might look like y = {x < -2: -1, -2 <= x < 2: x, x >= 2: 1}. This creates a function that is constant at -1 for x less than -2, linear from -2 to 2, and constant at 1 for x greater than or equal to 2.
As you add pieces, keep the conditions mutually exclusive and exhaustive. Mutually exclusive means no input satisfies two conditions at once. Exhaustive means every input in your domain satisfies at least one condition. If you leave a gap — for instance, y = {x < 0: x, x > 0: x} with no rule for x = 0 — Desmos leaves that point undefined and shows a hole in the graph.
For readability, limit yourself to four or five pieces. Beyond that, the expression becomes hard to debug if something goes wrong. If you need many pieces, consider whether a single formula with absolute values or other functions might work instead.
Handling boundaries and continuity
At the boundary between two pieces, the function may be continuous (the pieces meet) or discontinuous (there is a jump). Desmos shows this visually: a continuous function has a solid line, while a discontinuous one has a gap or jump.
For example, y = {x < 1: x, x >= 1: x + 1} is discontinuous at x = 1. When x approaches 1 from the left, y approaches 1. When x equals 1, y equals 2. Desmos draws a line up to (1, 1) with an open circle, then a separate line starting at (1, 2) with a closed circle.
To make a continuous function, ensure the two pieces have the same output at the boundary. For y = {x < 1: x, x >= 1: x}, both pieces output 1 when x = 1, so the graph is a single unbroken line. This is obvious in this case, but with more complex formulas you may need to check the boundary values by hand.
Nesting piecewise functions and combining them with other operations
You can use a piecewise function inside another expression. For instance, y = 2 * {x < 0: -x, x >= 0: x} scales the absolute value function by 2. Or y = {x < 0: x^2, x >= 0: sqrt(x)} combines a parabola and a square root.
You can also nest one piecewise function inside another, though this gets confusing quickly. y = {x < 0: {x < -5: 1, x >= -5: 0}, x >= 0: x} is valid but hard to read. If you find yourself nesting, step back and ask whether you can flatten the logic into a single set of conditions.
Piecewise functions work in any context where Desmos accepts an expression: in the y = field, in parametric equations, in inequalities, and even inside other functions. The syntax stays the same.
Common errors and how to fix them
The most frequent mistake is forgetting the curly braces. y = x < 0: -x, x >= 0: x will not work; you need y = {x < 0: -x, x >= 0: x}. Desmos will show a red error indicator if you omit them.
Another common error is using and or or inside conditions. Desmos does not support these operators in piecewise syntax. If you need to combine conditions, use the ordering trick instead. For a function that is x when -1 < x < 1 and 0 otherwise, write y = {-1 < x < 1: x, 1 <= x: 0, x <= -1: 0} rather than trying to write a single condition with and.
If a piece does not appear on the graph, check that your condition actually matches some part of the visible range. Zoom out or pan to see whether the piece exists outside your current view. You can also hover over the expression in the list to see Desmos highlight the corresponding part of the graph.
Frequently Asked Questions
Can I use inequalities like <= and >= in piecewise functions?
Yes. Desmos supports <= and >= in conditions. Use <= when you want to include the boundary point in that piece and < when you want to exclude it. For example, {x < 1: x, x >= 1: x + 1} assigns the point x = 1 to the second piece.
What happens if I write a condition that is never true?
That piece simply never runs. For instance, in y = {x > 0: x, x < 0: x^2, x > 100: 1}, the third piece is unreachable because all positive x are caught by the first condition. Desmos will still graph the function, but the third piece contributes nothing.
Can I use variables other than x in conditions?
Yes, if those variables are defined elsewhere. You can write y = {x < a: x, x >= a: x^2} if you have a slider or another expression that defines a. When you change the slider, the boundary moves and the graph updates.
How do I make a piecewise function with an open or closed circle at a boundary?
Desmos draws open circles automatically where a piece is undefined and closed circles where it is defined. If your first piece uses x < 1 and your second uses x >= 1, Desmos shows an open circle at (1, y₁) and a closed circle at (1, y₂). To control this, adjust your inequality symbols.
Can I graph a piecewise function with more than one variable?
Yes, but the syntax is the same. For a function of two variables like z = {x + y < 0: 1, x + y >= 0: 0}, Desmos treats it as a 3D surface or a 2D region depending on context. In 2D graphing mode, you will see the regions where each piece applies.