# Elm Syntax question: If and else inside the "let in" scope

**URL:** <https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549>\
**Category:** Learn\
**Created:** [January 2, 2024, 8:35pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549 "2024-01-02T20:35:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SANTMAN](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/santman/32/5101_2.png) [@SANTMAN](https://discourse.elm-lang.org/u/SANTMAN)\
**Post date:** [January 2, 2024, 8:35pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549/1 "2024-01-02T20:35:54Z")

</div>

Hi,

I’m not sure how to use the syntax for if and else inside the “let” scope in Elm.

What doesn’t work is the below code.

```auto
backgroundRect : Int -> Svg msg
backgroundRect v =
    let
        color = "red"
           
        m = modBy 2 v
        if m == 0 then
           color = "green"
       else
           color = "blue"
    in
      Svg.rect
          [ width "100%"
          , height "100%"
          , fill color
          ]
          []

```

As a work around I’m doing this, which is ugly. Any inputs on how to use “if and else inside the let and in block?”

```elm
backgroundRect : Int -> Svg msg
backgroundRect v =
    let
        color = "red"   
        m = modBy 2 v
    in

    if m == 0 then
      Svg.rect
          [ width "100%"
          , height "100%"
          , fill "Green"
          ]
          []
   else 
      Svg.rect
          [ width "100%"
          , height "100%"
          , fill "red"
          ]
          []

  

```

Best,  
SantMan

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [January 2, 2024, 9:26pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549/2 "2024-01-02T21:26:57Z")

</div>

I think you’re confusing if statements with if expressions. Unlike in Javascript, Python, and similar languages in Elm an `if .. else` returns a value. Additionally, `color` in your code is immutable. So when you write

```elm
if m == 0 then
    color = "green"
else
    color = "blue"

```

you’re attempting to mutate `color` which won’t be allowed. You likely want something like

```elm
backgroundRect v =
    let
        color = 
            if m == 0 then
                "green"
            else
                "blue"

        m = modBy 2 v
    in

```

---

<div class="post-metadata">

**Author:** ![progger](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@progger](https://discourse.elm-lang.org/u/progger)\
**Post date:** [January 2, 2024, 9:29pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549/3 "2024-01-02T21:29:01Z")

</div>

“If” expressions return a value, unlike in C where they only take an action. So you can do:

```auto
    let
        m = modBy 2 v
        color =
            if m == 0 then
                "green"

            else
                "blue"
    in

```

---

<div class="post-metadata">

**Author:** ![SANTMAN](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/santman/32/5101_2.png) [@SANTMAN](https://discourse.elm-lang.org/u/SANTMAN)\
**Post date:** [January 2, 2024, 9:36pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549/4 "2024-01-02T21:36:44Z")

</div>

Thank you @wolfadex and @progger for your response and explaination. I understood the syntax now.

Elm Error message was not so helpful in this case it was complaining about the let block and I couldn’t figure out what it is pointing at. Hope this could be improved in the near future.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [January 12, 2024, 9:37pm UTC](https://discourse.elm-lang.org/t/elm-syntax-question-if-and-else-inside-the-let-in-scope/9549/5 "2024-01-12T21:37:23Z")

</div>

Closed on January 12, 2024, 9:37pm UTC

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