%dw 2.0
output application/json
fun myfun() = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myfun() }
Flow Control and Scope Operations in DataWeave
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.
This example uses do to return the string "DataWeave" when the variable myVar is referenced from the main body of the script.
%dw 2.0
output application/json
var myVar = do {
    var name = "DataWeave"
    ---
    name
}
---
{ result: myVar }
Both scripts produce this output:
{
  "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.
%dw 2.0
output application/json
fun test(p: String) = do {
    var a = "Foo" ++ p
    ---
    a
}
---
{ result: test(" Bar") }
{
  "result": "Foo Bar"
}
See also, Examples: Local DataWeave Variables.
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:
%dw 2.0
var myVar = { country : "FRANCE" }
output application/json
---
if (myVar.country == "USA")
  { currency: "USD" }
else { currency: "EUR" }
{
  "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
Theif elsestatement returns the Boolean valuetrueif the operation1 + 1 == 55is true andfalseif not. - 
An equality operation in
ex2
Theif elsestatement returns1if the value of the specified index is1or a string if the value is not1. - 
An
isEmptyfunction inex3
Theif elsestatement returns the string"ID is empty"or"ID is not empty"depending on whetheraRecord.bookIdcontains a value. - 
A mapping in
ex4that iterates overfirstInput
Theif elsestatement returns the value of thebookIdas a Number if the value is equal to101. It returns the specified string if the value is not equal to101. 
%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"
	}
}
<?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.
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.
%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" }
{
  "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.
%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"
}
[ "Hola DataWeave", "Hello DataWeave", "Ola DataWeave" ]



