Algorithm with if...else

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.

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

Thanks

Welcome to our community! :confetti_ball:

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”).

2 Likes

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? :slight_smile:

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:

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.

1 Like

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:

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
2 Likes

For advanced mode, use early return:

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.

2 Likes


… after else give me error

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.

4 Likes


thank you! it works.

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