# Conditional steps if-else without code duplication

**URL:** <https://discuss.circleci.com/t/conditional-steps-if-else-without-code-duplication/45030>\
**Category:** Build Environment\
**Tags:** circle-yml\
**Created:** [August 11, 2022, 8:19am UTC](https://discuss.circleci.com/t/conditional-steps-if-else-without-code-duplication/45030 "2022-08-11T08:19:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![throwaway-fishery](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/throwaway-fishery/32/30807_2.png) [@throwaway-fishery](https://discuss.circleci.com/u/throwaway-fishery)\
**Post date:** [August 11, 2022, 8:19am UTC](https://discuss.circleci.com/t/conditional-steps-if-else-without-code-duplication/45030/1 "2022-08-11T08:19:12Z")

</div>

Hi, is there a way to emulate an if-else statement for conditional steps?  
Currently, is it possible to do that by mirroring the conditions like such:

```auto
    steps: 
      - when:
          condition: << parameters.win >>
          steps:
            - run: echo "You won!"
      - when:
          condition:
              not: << parameters.win >> 
          steps:
            - run: echo "You lost!"

```

But as you see, it’s code duplication, and will make the config hard to read and large.  
Is there a something similar to this?

```auto
    steps: 
      - when:
          condition: << parameters.win >>
          steps:
             - run: echo "You won!"
          else: 
               steps:
                 - run: echo "You lost!"

```

---

<div class="post-metadata">

**Author:** ![KyleTryon](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/kyletryon/32/9977_2.png) [@KyleTryon](https://discuss.circleci.com/u/KyleTryon)\
**Post date:** [August 17, 2022, 1:47pm UTC](https://discuss.circleci.com/t/conditional-steps-if-else-without-code-duplication/45030/2 "2022-08-17T13:47:19Z")

</div>

Depending on the situation, you may be able to do your conditionals with bash rather than YAML. In this specific example for instance:

```auto
- run:
  environment:
    WIN_RESULT: <<parameters.win>>
  command: | 
    if [$WIN_RESULT = 1]; then
      echo "You Won!"
    else
      echo "You Lose!"
    fi

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [August 27, 2022, 1:47pm UTC](https://discuss.circleci.com/t/conditional-steps-if-else-without-code-duplication/45030/3 "2022-08-27T13:47:53Z")

</div>

Closed on August 27, 2022, 1:47pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
