# Algorithm with if...else

**URL:** https://discourse.julialang.org/t/algorithm-with-if-else/62962
**Category:** New to Julia
**Tags:** question
**Created:** [June 15, 2021, 4:46pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962 "2021-06-15T16:46:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![12345\_12345](https://avatars.discourse-cdn.com/v4/letter/1/977dab/32.png) [@12345\_12345](https://discourse.julialang.org/u/12345_12345)
#### Post date: [June 15, 2021, 4:46pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/1 "2021-06-15T16:46:14Z")

</div>

I’m newbie in Julia, and after seeing YT free course in my free time I followed 2nd lesson of 3rd week [julia for nervous beginner] (with if…else) and I’m trying to write a bigger algorithm with Julia, like this one but I have errors.

 ![Laboratory-examination-algorithm-in-thyroid-nodule-diagnosis](https://global.discourse-cdn.com/julialang/original/3X/f/4/f40c6517dc17bbf8faab105c72b7b960f26999a5.png)

someone can help me if I find it later in some books? (or directly here)

Thanks

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 15, 2021, 5:07pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/2 "2021-06-15T17:07:13Z")

</div>

Welcome to our community! 🎊

Can you post what you tried and what you are having a problem with?

If I understand correctly, you just want to print some message based on the value of some variables? (i.e., if TSH serum level is “Decline” then you check variable FT4 Level and if it is also “Decline” you print “Secondary hypothyroid”).

---

<div class="post-metadata">

### Author: ![12345\_12345](https://avatars.discourse-cdn.com/v4/letter/1/977dab/32.png) [@12345\_12345](https://discourse.julialang.org/u/12345_12345)
#### Post date: [June 15, 2021, 5:35pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/4 "2021-06-15T17:35:49Z")

</div>

yes, on YT course there is an example of a simply (in science computer you write like flow chart) in medicine we call diagnostic algorithm, and I’ve done like on YT example an easy of that, the problem is if I get more functions with more methods. Example my one:  
function thyroid(TSH)  
if TSH\>3.0  
println(“patient need suregery intervention”)  
elseif TSH\<3.0  
println(“patient need sonography”)  
else TSH=1.5  
println(“patient need scintigraphy”)  
end  
end

and it works. the problem if when I add more critical points over TSH like FT4 level like in that flowchart, and if I add more elseif… so where I’m wrong to create that complex flowchart? 🙂

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 15, 2021, 5:50pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/5 "2021-06-15T17:50:13Z")

</div>

Tip: use a triple backtick fence around your code to make it more legible.

I’m still not sure what your perceived problem is, but I see one problematic line:

```julia
else TSH = 1.5

```

The `else` here does not introduce a new `if`, and the `TSH = 1.5` _assigns_ 1.5 to `TSH`, it doesn’t compare them. You might want `==` (or `≈`).

Also, if that’s supposed to be a new `if`, don’t forget to add a base case: what happens if none of the cases match.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 15, 2021, 5:53pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/6 "2021-06-15T17:53:09Z")

</div>

Please use triple backticks to format your code.

```  
your indented code here  
```

The first problem with your code is that `else TSH=1.5` makes little sense, for various reasons.

1. It is an `else`, not an `elseif`, so there should be nothing after the `else` in that line.
2. The `TSH=1.5` changes the value of variable `TSH` to `1.5` it is not an equality test, if you wanted to do an equality test you should use `==` instead. EDIT: but as @gustaphe pointed out, prefer using something like `≈` for numbers with decimal cases.
3. A numerical value can only be above `3.0`, below `3.0`, or equal to `3.0` so you `else` is only reached if the value is exactly `3.0` because if it is above `3.0` it stops at the first clause, and if it is below `3.0` it stops at the second one.
4. Therefore, what happens if you add a `println(TSH)` below the `println("patient need scintigraphy")` is that you will see the messages `patient need scintigraphy` and `1.5` if you call `thyroid` with `3.0`.

In your case, I would do something like:

```julia
function thyroid(TSH, TH4)
    if TSH > 3.0
        println("patient need suregery intervention")
    elseif TSH <= 3.0 && TSH > 1.5
        println("patient need sonography")
    else # TSH < 1.5
        println("patient need scintigraphy")
        if TH4 > 3.0 # dummy value, I do not understand medicine, XD
            println("hyperthyroid")
        elseif TH4 <= 3.0 && TH4 > 1.5
            println("T3 thyrotoxicosis")
        else # TH4 < 1.5
            println("secondary hypothyroid")
        end
    end
end

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 15, 2021, 6:10pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/7 "2021-06-15T18:10:17Z")

</div>

For advanced mode, use early return:

```julia
function thyroid(TSH, TH4)
    TSH > 3.0 && return println("patient need suregery intervention")
    TSH > 1.5 && return println("patient need sonography")
    # TSH < 1.5
    println("patient need scintigraphy")
    TH4 > 3.0 && return println("hyperthyroid")
    TH4 > 1.5 && return println("T3 thyrotoxicosis")
    # TH4 < 1.5
    return println("secondary hypothyroid")
end

```

Note, specifically, that you don’t need to test if `TSH <= 3.0`, because that’s the complement to the first comparison.

---

<div class="post-metadata">

### Author: ![12345\_12345](https://avatars.discourse-cdn.com/v4/letter/1/977dab/32.png) [@12345\_12345](https://discourse.julialang.org/u/12345_12345)
#### Post date: [June 15, 2021, 9:00pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/8 "2021-06-15T21:00:23Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/a/3a251befb1d2fd095c96f4c02857ff562b644f32.png)  
… after else give me error

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 15, 2021, 9:48pm UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/9 "2021-06-15T21:48:14Z")

</div>

Please triple-quote your code with back-ticks as suggested by others earlier in the thread.

You are just missing an `end`. In the last line, it should be `end` not `else` to close out the function.

---

<div class="post-metadata">

### Author: ![12345\_12345](https://avatars.discourse-cdn.com/v4/letter/1/977dab/32.png) [@12345\_12345](https://discourse.julialang.org/u/12345_12345)
#### Post date: [June 16, 2021, 10:12am UTC](https://discourse.julialang.org/t/algorithm-with-if-else/62962/10 "2021-06-16T10:12:30Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/0/0/00b5292573ef113553cf5ca0d2cee57a3c69a8e3.png)  
thank you! it works.

I’ll do more exercises, and try to create more complex. flowchart.
