<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShadersGraph",
  "identifier" : "/documentation/MetalPerformanceShadersGraph/MPSGraph/if(_:then:else:name:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal Performance Shaders Graph",
      "MetalPerformanceShadersGraph"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSGraph(im)ifWithPredicateTensor:thenBlock:elseBlock:name:"
  },
  "title" : "if(_:then:else:name:)"
}
-->

# if(_:then:else:name:)

Adds an if-then-else operation to the graph.

```
func `if`(_ predicateTensor: MPSGraphTensor, then thenBlock: @escaping MPSGraphIfThenElseBlock, else elseBlock: MPSGraphIfThenElseBlock?, name: String?) -> [MPSGraphTensor]
```

## Parameters

`predicateTensor`

Tensor must have a single scalar value, used to decide between then/else branches

`thenBlock`

If predicate is true operations in this block are executed

`elseBlock`

If predicate is false operations in this block are executed

`name`

name of operation

## Return Value

results If no error, the tensors returned by user. If not empty, user must define both then/else block,
both should have same number of arguments and each corresponding argument should have same elementTypes.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)