This page shows you how to use jumps or for loops to control the order
in which your workflow's steps run. Basic jumps allow you to define which step
the workflow runs next. Conditional jumps build on basic jumps, allowing you to
use conditional
[expressions](https://docs.cloud.google.com/workflows/docs/reference/syntax/expressions.md.txt)
to control the order of execution through a workflow. For example, you can run
certain steps only when a variable or response from another workflow step meets
specific criteria.

The examples on this page use a sample API that returns the day of the week.

## Before you begin

1. You should already have an existing workflow where you want to change the order of execution. To learn how to create and deploy a workflow, see [Create and update a
   workflow](https://docs.cloud.google.com/workflows/docs/creating-updating-workflow).

## Use jumps to change execution order

By default, all workflows are ordered lists where every step runs
in the order you define in the workflow's source code. You can choose
to override this default ordering by using jumps.

### Basic jumps

You can specify which step to run next in a workflow by using basic jumps.

### Console

1. Open the Workflows page in the
   Google Cloud console:   
   [Go to Workflows](https://console.cloud.google.com/workflows)

2. Select the name of the workflow where you want to change the order of
   execution of the existing steps.

3. On the **Edit workflow** page, select **Next** to go to the workflow
   editor.

4. Add the `next` field at the end of a step to tell the workflow to jump
   to a particular step:

   <br />

   ### YAML

   ```yaml
        - step_a:
            ...
            next: STEP_NAME_TO_JUMP_TO
        - step_b:
            ...
        - next_step:
            ...
   ```

   ### JSON

   ```json
       [
         {
           "step_a": {
             ...
             "next": "STEP_NAME_TO_JUMP_TO"
           }
         }
         {
           "step_b": {
             ...
           }
         }
         {
           "next_step": {
             ...
           }
         }
       ]
   ```

   <br />

   Replace `STEP_NAME_TO_JUMP_TO` with the name
   of the step you want the workflow to execute next. For example,
   `next_step`.
5. Select **Deploy**.

### gcloud

1. Open your workflow's definition file in the text editor of your choice.

2. Add the `next` field at the end of a step to tell the workflow to jump
   to a particular step:

   ### YAML

   ```yaml
        - step_a:
            ...
            next: STEP_NAME_TO_JUMP_TO
        - step_b:
            ...
        - next_step:
            ...
   ```

   ### JSON

   ```json
     [
       {
         "step_a": {
           ...
           "next": "STEP_NAME_TO_JUMP_TO"
         }
       }
       {
         "step_b": {
           ...
         }
       }
       {
         "next_step": {
           ...
         }
       }
     ]
   ```

   Replace `STEP_NAME_TO_JUMP_TO` with the name of the
   step you want the workflow to execute next. For example, `next_step`.
3. Save the workflow file.

4. To deploy the workflow, enter the following command:

   ```bash
   gcloud workflows deploy WORKFLOW_NAME \
   --source=WORKFLOW_FILE.YAML
   ```

   Replace the following:
   - `WORKFLOW_NAME`: required. The name of your
     workflow.

   - `WORKFLOW_FILE.YAML`: required. The source file
     for the workflow.

### Example

For example, the following workflow has its steps out of order:


### YAML

```yaml
  - get_time:
     call: http.get
     args:
         url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
     result: currentTime
  - return_daylight_savings_bool:
     return: ${daylightSavings}
  - get_daylight_savings_bool:
     assign:
         - daylightSavings: ${currentTime.body.isDayLightSavingsTime}
```

### JSON

```json
    [
      {
        "get_time": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
          },
          "result": "currentTime"
        }
      },
      {
        "return_daylight_savings_bool": {
          "return": "${daylightSavings}"
        }
      },
      {
        "get_daylight_savings_bool": {
          "assign": [
            {
              "daylightSavings": "${currentTime.body.isDayLightSavingsTime}"
            }
          ]
        }
      }
    ]
```

<br />

In this example, `next` fields have been added to the `get_daylight_savings_bool`
and `return_daylight_savings_bool` steps so that the steps execute in the
correct order:

<br />

### YAML

```yaml
  - get_time:
     call: http.get
     args:
         url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
     result: currentTime
     next: get_daylight_savings_bool
  - return_daylight_savings_bool:
     return: ${daylightSavings}
  - get_daylight_savings_bool:
     assign:
         - daylightSavings: ${currentTime.body.isDayLightSavingsTime}
     next: return_daylight_savings_bool
```

### JSON

```json
    [
      {
        "get_time": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
          },
          "result": "currentTime",
          "next": "get_daylight_savings_bool"
        }
      },
      {
        "return_daylight_savings_bool": {
          "return": "${daylightSavings}"
        }
      },
      {
        "get_daylight_savings_bool": {
          "assign": [
            {
              "daylightSavings": "${currentTime.body.isDayLightSavingsTime}"
            }
          ],
          "next": "return_daylight_savings_bool"
        }
      }
    ]
```

<br />

### Conditional jumps

You can use conditional jumps to determine which step to run next in a workflow.
Conditional jumps use `switch` blocks, which jump to a specified step if a
particular [condition](https://docs.cloud.google.com/workflows/docs/reference/syntax/conditions.md.txt) is met. Each
`switch` block can include a maximum of 50 conditions.

### Console

1. Open the Workflows page in the
   Google Cloud console:   
   [Go to Workflows](https://console.cloud.google.com/workflows)

2. Select the name of the workflow to which you want to add a `switch` block.

3. On the **Edit workflow** page, select **Next** to go to the workflow
   editor.

4. On the **Define workflow** page, your workflow's current definition is
   displayed. To edit the workflow so that it executes a certain step based
   on a conditional statement, add a step that uses a `switch` block:

   <br />

   ### YAML

   ```yaml
        - SWITCH_STEP_NAME:
            switch:
                - condition: ${EXPRESSION_ONE}
                  next: STEP_A
                - condition: ${EXPRESSION_TWO}
                  next: STEP_B
                ...
            next: STEP_C
        - STEP_A
            ...
        - STEP_B
            ...
        - STEP_C
            ...
   ```

   ### JSON

   ```json
       [
         {
           "SWITCH_STEP_NAME": {
             "switch": [
               {
                 "condition": "${EXPRESSION_ONE}",
                 "next": "STEP_A"
               },
               {
                 "condition": "${EXPRESSION_TWO}",
                 "next": "STEP_B"
               }
             ],
             "next": "STEP_C"
           }
           "STEP_A": {
             ...
           }
           "STEP_B": {
             ...
           }
           "STEP_C": {
             ...
           }
         }
       ]
   ```

   <br />

   Replace the following:
   - `SWITCH_STEP_NAME`: the name of the step
     containing the `switch` block.

   - `EXPRESSION_ONE`: the first expression to
     evaluate. If this expression evaluates to true, the workflow executes
     `STEP_A` next. If this expression evaluates to false, the workflow
     evaluates the next expression.

   - `EXPRESSION_TWO`: the second expression to
     evaluate. If this expression evaluates to true, the workflow executes
     `STEP_B` next. If this expression evaluates to false, the workflow
     executes `STEP_C`.

   - `STEP_A`, `STEP_B`,
     `STEP_C`: placeholder step names.

5. Select **Deploy**.

### gcloud

1. Open your workflow's definition in the text editor of your choice.

2. To edit the workflow so that it executes a certain step based on a
   conditional statement, add a step that uses a `switch` block:

   ### YAML

   ```yaml
    - SWITCH_STEP_NAME:
        switch:
            - condition: ${EXPRESSION_ONE}
              next: STEP_A
            - condition: ${EXPRESSION_TWO}
              next: STEP_B
            ...
        next: STEP_C
    - STEP_A
        ...
    - STEP_B
        ...
    - STEP_C
        ...
   ```

   ### JSON

   ```json
   [
     {
       "SWITCH_STEP_NAME": {
         "switch": [
           {
             "condition": "${EXPRESSION_ONE}",
             "next": "STEP_A"
           },
           {
             "condition": "${EXPRESSION_TWO}",
             "next": "STEP_B"
           }
         ],
         "next": "STEP_C"
       }
       "STEP_A": {
         ...
       }
       "STEP_B": {
         ...
       }
       "STEP_C": {
         ...
       }
     }
   ]
     
   ```

   Replace the following:
   - `SWITCH_STEP_NAME`: the name of the step
     containing the `switch` block.

   - `EXPRESSION_ONE`: the first expression to
     evaluate. If this expression evaluates to true, the workflow executes
     `STEP_A` next. If this expression evaluates to false, the workflow
     evaluates the next expression.

   - `EXPRESSION_TWO`: the second expression to
     evaluate. If this expression evaluates to true, the workflow executes
     `STEP_B` next. If this expression evaluates to false, the workflow
     executes `STEP_C`.

   - `STEP_A`, `STEP_B`,
     `STEP_C`: placeholder step names.

3. Save the workflow file.

4. To deploy the workflow, enter the following command:

   ```bash
   gcloud workflows deploy WORKFLOW_NAME \
   --source=WORKFLOW_FILE.YAML
   ```

   Replace the following:
   - `WORKFLOW_NAME`: required. The name of your
     workflow.

   - `WORKFLOW_FILE.YAML`: required. The source file
     for the workflow.

### Example

For example, this workflow uses a switch block to control the workflow's
order of execution:

<br />

### YAML

```yaml
  - getCurrentTime:
      call: http.get
      args:
          url:  https://us-central1-workflowsample.cloudfunctions.net/datetime
      result: currentTime
  - conditionalSwitch:
      switch:
          - condition: ${currentTime.body.dayOfTheWeek == "Friday"}
            next: friday
          - condition: ${currentTime.body.dayOfTheWeek == "Saturday" OR currentTime.body.dayOfTheWeek == "Sunday"}
            next: weekend
      next: workWeek
  - friday:
      return: "It's Friday! Almost the weekend!"
  - weekend:
      return: "It's the weekend!"
  - workWeek:
      return: "It's the work week."
    
```

### JSON

```json
    [
      {
        "getCurrentTime": {
          "call": "http.get",
          "args": {
            "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
          },
          "result": "currentTime"
        }
      },
      {
        "conditionalSwitch": {
          "switch": [
            {
              "condition": "${currentTime.body.dayOfTheWeek == "Friday"}",
              "next": "friday"
            },
            {
              "condition": "${currentTime.body.dayOfTheWeek == "Saturday" OR currentTime.body.dayOfTheWeek == "Sunday"}",
              "next": "weekend"
            }
          ],
          "next": "workWeek"
        }
      },
      {
        "friday": {
          "return": "It's Friday! Almost the weekend!"
        }
      },
      {
        "weekend": {
          "return": "It's the weekend!"
        }
      },
      {
        "workWeek": {
          "return": "It's the work week."
        }
      }
    ]
      
```

<br />

In this example, the `switch` block has two conditions. The parser evaluates
each condition in order, and if the result of the condition's expression is `true`,
that condition's `next` step is called. If none of the conditions are met,
the workflow calls the step specified by the `next` field outside of the
`switch` block, which, in this example, is the step `workWeek`.

For example, if the day of the week is `"Saturday"`, then the workflow jumps to
the step `weekend` and returns the message `"It's the weekend!"`. The workflow
doesn't execute the step `friday` because the conditional jump skips over it,
and the workflow doesn't execute the step `workWeek` because `return` stops the
execution of the workflow at the end of the `weekend` step.

## Use `for` loops to iterate

You can use `for` loops to iterate over a sequence of numbers or through a
collection of data, such as a list or map.

You can walk through every item in a list or map using item-based
iteration. If you have a specific range of numeric values to iterate through,
you can use range-based iteration.

For more information and examples, see the syntax reference on [iteration](https://docs.cloud.google.com/workflows/docs/reference/syntax/iteration.md.txt).

## What's next

- [Execute a workflow](https://docs.cloud.google.com/workflows/docs/executing-workflow.md.txt)