<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp" -->

---
title: How to use if and switch as expressions in Swift | daily.dev
description: Swift 5.9&#x27;s SE-0380 lets if and switch statements produce values directly when assigning to a variable, declaring one, or returning from a function,...
canonical: https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to use if and switch as expressions in Swift | daily.dev
og:description: Swift 5.9&#x27;s SE-0380 lets if and switch statements produce values directly when assigning to a variable, declaring one, or returning from a function,...
og:url: https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp
og:image: https://api.daily.dev/og/posts/1hmVR2Rzp.png
og:image:alt: How to use if and switch as expressions in Swift
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to use if and switch as expressions in Swift

**[Sarunw](https://daily.dev/sources/sarunw)** · [@sarunw](https://daily.dev/sarunw) · 6 min read · 0 upvotes · 0 comments

## Summary

Swift 5.9's SE-0380 lets if and switch statements produce values directly when assigning to a variable, declaring one, or returning from a function, eliminating common var-then-assign boilerplate and awkward ternary chains. Key rules: each branch must be a single expression (with an exception for branches that throw or never return), an if expression requires an else, and branches are type checked independently rather than inferring a common type like the ternary operator does — so mixing an Int literal and a Double literal across branches fails without an explicit type annotation. The feature is scoped narrowly: it cannot be used as a function argument, sub-expression, or method receiver, and it doesn't change how result builders like SwiftUI's body handle if/switch.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://sarunw.com/posts/if-and-switch-expressions>

## Questions this post answers

### How do I use if and switch as expressions to assign a value in Swift 5.9?

Swift 5.9 introduced SE-0380, letting if and switch produce a value directly when assigning to a variable, declaring a let, or returning from a function or closure, for example let bullet = if isRoot && count == 0 { "" } else { "- " }. This removes the need for a var declared then assigned in a separate step, and works with pattern matching like if let too.

_Track Swift language proposals like SE-0380 on daily.dev while modernizing conditional assignment code._

### Why does Swift give a type error when if expression branches return different numeric types like Int and Double?

Each branch of an if or switch expression in Swift is type checked independently before the results are compared, so there is no bidirectional inference across branches like the ternary operator provides. A branch returning 0 infers Int and a branch returning 1.0 infers Double, causing a mismatch; the fix is an explicit type annotation such as let y: Double = if p { 0 } else { 1.0 }.

_Developers converting ternaries to if expressions can check typing quirks like this on daily.dev before shipping._

### Why do I get an error adding a statement before the value in a Swift if expression branch?

Each branch of a Swift if or switch expression must be a single expression, so adding a statement like a log line before the value fails to compile, and adding return does not help since return means exiting the enclosing function rather than supplying the branch's value. The one exception is a branch that ends by throwing or otherwise never returns, such as fatalError(), which skips type checking entirely.

_Swift developers debugging expression syntax errors can find rules like this on daily.dev._

---

Tags: [#general-programming](https://daily.dev/tags/general-programming), [#swift](https://daily.dev/tags/swift)

[View this post on daily.dev](https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to use if and switch as expressions in Swift","url":"https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp"},"datePublished":"2026-09-01T11:58:29.910Z","dateModified":"2026-09-02T08:00:27.599Z","description":"Swift 5.9's SE-0380 lets if and switch statements produce values directly when assigning to a variable, declaring one, or returning from a function,...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9f47c7e0d42459df325839ac46cf7de6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9f47c7e0d42459df325839ac46cf7de6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Sarunw","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Person","name":"Sarun Wongpatcharapakorn","url":"https://daily.dev/sarunw","image":"https://avatars.githubusercontent.com/u/795368?v=4","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":20}},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"general-programming,swift","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sarunw","item":"https://daily.dev/sources/sarunw"},{"@type":"ListItem","position":3,"name":"How to use if and switch as expressions in Swift"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-use-if-and-switch-as-expressions-in-swift-1hmvr2rzp#faq","mainEntity":[{"@type":"Question","name":"How do I use if and switch as expressions to assign a value in Swift 5.9?","acceptedAnswer":{"@type":"Answer","text":"Swift 5.9 introduced SE-0380, letting if and switch produce a value directly when assigning to a variable, declaring a let, or returning from a function or closure, for example let bullet = if isRoot && count == 0 { \"\" } else { \"- \" }. This removes the need for a var declared then assigned in a separate step, and works with pattern matching like if let too. Track Swift language proposals like SE-0380 on daily.dev while modernizing conditional assignment code."}},{"@type":"Question","name":"Why does Swift give a type error when if expression branches return different numeric types like Int and Double?","acceptedAnswer":{"@type":"Answer","text":"Each branch of an if or switch expression in Swift is type checked independently before the results are compared, so there is no bidirectional inference across branches like the ternary operator provides. A branch returning 0 infers Int and a branch returning 1.0 infers Double, causing a mismatch; the fix is an explicit type annotation such as let y: Double = if p { 0 } else { 1.0 }. Developers converting ternaries to if expressions can check typing quirks like this on daily.dev before shipping."}},{"@type":"Question","name":"Why do I get an error adding a statement before the value in a Swift if expression branch?","acceptedAnswer":{"@type":"Answer","text":"Each branch of a Swift if or switch expression must be a single expression, so adding a statement like a log line before the value fails to compile, and adding return does not help since return means exiting the enclosing function rather than supplying the branch's value. The one exception is a branch that ends by throwing or otherwise never returns, such as fatalError(), which skips type checking entirely. Swift developers debugging expression syntax errors can find rules like this on daily.dev."}}]}
```

