---
schema_version: "1"
id: e599401874b8baed9948a393787f0ace1d37a1aab404df9a0e60bea95c8ed923
title: Flow Control and Scope Operations in DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-flow-control.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-flow-control
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0e8df4d9a4cd0a0ca125e0b93b15213c62a6e5257f81da7a3ab1c1905782bca2
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
---

# Flow Control and Scope Operations in DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

You can use the following operators within any DataWeave expression:

- [`do`](#control_flow_do)

- [`if else`](#control_flow_if_else)

- [`else if`](#control_flow_else_if)

## `do`

A `do` statement creates a scope in which new variables, functions, annotations, or namespaces can
be declared and used. The syntax is similar to a mapping in that it is composed of a header and body
separated by `---`. Its header is where all the declarations are defined, and its body is the result
of the expression.

This example uses `do` to return the string `"DataWeave"` when `myfun()` is called from the main
body of the script.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
fun myfun() = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myfun() }
```

This example uses `do` to return the string `"DataWeave"` when the variable `myVar` is referenced
from the main body of the script.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
var myVar = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myVar }
```

Both scripts produce this output:

Output:

```json
{
  "result": "DataWeave"
}
```

The next example uses `do` to prepend the string `"Foo"` to a string (`" Bar"`) that is passed to
the `test(p: String)` function.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
fun test(p: String) = do {
    var a = "Foo" ++ p
    ---
    a
}
---
{ result: test(" Bar") }
```

Output:

```json
{
  "result": "Foo Bar"
}
```

See also, [Examples: Local DataWeave Variables](https://docs.mulesoft.com/dataweave/2.6/dataweave-variables#example_local_variable).

## `if else`

An `if` statement evaluates a conditional expression and returns the value under the `if` only if
the conditional expression returns `true`. Otherwise, it returns the expression under `else`. Every
`if` expression must have a matching `else` expression.

The following example uses the input `{ country : "FRANCE" }`, which is defined by the `myVar`
variable in the header:

DataWeave Script:

```dataweave
%dw 2.0
var myVar = { country : "FRANCE" }
output application/json
---
if (myVar.country == "USA")
  { currency: "USD" }
else { currency: "EUR" }
```

Output:

{ "currency": "EUR" }

You can use the if-else construct on any condition that evaluates to `true` or `false`, including
mathematical, logical, equality, and relational statements. The condition can act on any valid
input.

The next DataWeave script applies `if else` statements to the result of the following conditional
statements:

- A mathematical operation in `ex1`  
  The `if else` statement returns the Boolean value `true` if the operation `1 + 1 == 55` is true
  and `false` if not.

- An equality operation in `ex2`  
  The `if else` statement returns `1` if the value of the specified index is `1` or a string if the
  value is not `1`.

- An `isEmpty` function in `ex3`  
  The `if else` statement returns the string `"ID is empty"` or `"ID is not empty"` depending on
  whether `aRecord.bookId` contains a value.

- A mapping in `ex4` that iterates over `firstInput`  
  The `if else` statement returns the value of the `bookId` as a Number if the value is equal to
  `101`. It returns the specified string if the value is not equal to `101`.

DataWeave Script:

```dataweave
%dw 2.0
var aRecord =
 [
    "bookId":"101",
    "title":"world history",
    "price":"19.99"
 ]
output application/xml
---
{ examples:
	{
	   ex1 : if (1 + 1 == 55) true
	         else false,
	   ex2 : if ([1,2,3,4][1] == 1) 1
	         else "value of index 1 is not 1",
	   ex3 : if (isEmpty(aRecord.bookId)) "ID is empty"
	  	 else "ID is not empty",
	   ex4 : aRecord.bookId map (idValue) ->
	   	 if (idValue as Number == 101) idValue as Number
		 else "not 101"
	}
}
```

Output:

<?xml version='1.0' encoding='UTF-8'?>
<examples>
  <ex1>false</ex1>
  <ex2>value of index 1 is not 1</ex2>
  <ex3>ID is not empty</ex3>
  <ex4>101</ex4>
</examples>

Additional examples are available in [DataWeave Operators](https://docs.mulesoft.com/dataweave/2.6/dw-operators).

## `else if`

You can chain several `else` expressions together within an if-else construct by incorporating
`else if`. The following example uses the input `var myVar = { country : "UK" }`, which is defined
by the `myVar` variable in the header.

DataWeave Script:

```dataweave
%dw 2.0
var myVar = { country : "UK" }
output application/json
---
if (myVar.country =="USA")
	{ currency: "USD" }
else if (myVar.country =="UK")
	{ currency: "GBP" }
else { currency: "EUR" }
```

Output

{ "currency": "GBP" }

The following example is similar but takes an array as input instead of an object. The body of the
script uses `if else` and `else if` statements within a `do` operation to populate the value of the
`hello` variable.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
---
["Argentina", "USA", "Brazil"] map (country) -> do {
  var hello = if(country == "Argentina") "Hola"
   else if(country == "USA") "Hello"
   else if(country == "Brazil") "Ola"
   else "Sorry! We don't know $(country)'s language."
   ---
   "$(hello) DataWeave"
}
```

Output:

\[ "Hola DataWeave", "Hello DataWeave", "Ola DataWeave" \]

## See Also

- [DataWeave Types](https://docs.mulesoft.com/dataweave/2.6/dataweave-types)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)
