# Conditional for function

**URL:** <https://discourse.julialang.org/t/conditional-for-function/71296>\
**Category:** Performance\
**Tags:** loops\
**Created:** [November 11, 2021, 12:25am UTC](https://discourse.julialang.org/t/conditional-for-function/71296 "2021-11-11T00:25:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 11, 2021, 12:25am UTC](https://discourse.julialang.org/t/conditional-for-function/71296/1 "2021-11-11T00:25:18Z")

</div>

Hi, I have a conditional inside the for loop, the conditional changes the function in each loop, but first i get the error “qxloc is not defined” so add qxloc (x) = nothing;

but this does not solve the problem and the conditional does not work.

```julia
for e = 1:3
     #x1 = xnod[LaG[e,NL1], X]; x2 = xnod[LaG[e,NL2], X]
     #y1 = xnod[LaG[e,NL1], Y]; y2 = xnod[LaG[e,NL2], Y]
     #L = hypot(x2-x1, y2-y1)

     ang1 = atan(2,4);

  
  
     if e == 1
      qxloc(x) =-2.8*sin(ang1)*cos(ang1)
      qyloc(x) =-2.8*cos(ang1)^2

     elseif e == 2

      qxloc(x) = -2*cos(ang1)^2
      qyloc(x) = 0

     elseif e == 3

      qxloc(x) = 0
      qyloc(x) = 0 

     end

     #load the function
     plot_deformada(qxloc,qyloc,)

end

```

How can I solve it?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 11, 2021, 12:39am UTC](https://discourse.julialang.org/t/conditional-for-function/71296/2 "2021-11-11T00:39:16Z")

</div>

> [@Sbeltranj](#):
>
> qxloc (x) = nothing

where did you add it? You should add it outside the loop (or simply `local gxloc`).

can you provide a minimal example of the code and the error?

You should probably use, for example, `qxloc = x -> cos(x) ` to assign the name qxloc to each of the possible function.

---

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 11, 2021, 1:01am UTC](https://discourse.julialang.org/t/conditional-for-function/71296/3 "2021-11-11T01:01:03Z")

</div>

Hi, It is a for loop with 3 different elements, in each element the qxloc and qyloc function changes because it is used in a BVProblem function for different conditions.

the function where change this parameter (qxloc, qyloc) is:

```julia

function diffeq1!(dydx,y,x,p)

    # y[v_,:] = v
    dydx[v_] = y[t_] # = theta
    dydx[t_] = y[M_]/(E*I) # = M/(EI)
    dydx[M_] = y[V_] # = V
    dydx[V_] = qyloc(x) # = qyloc   
    dydx[u_] = y[fax_]/(A*E) # = u
    dydx[fax_] = -qxloc(x) # = faxial

end
```

---

<div class="post-metadata">

**Author:** ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Post date:** [November 11, 2021, 1:11am UTC](https://discourse.julialang.org/t/conditional-for-function/71296/4 "2021-11-11T01:11:18Z")

</div>

qxloc = x → cos (x) has solved my problem, thank you very much

```julia
qxloc = [ x -> -2.8*sin(ang1)*cos(ang1)

            x -> 0

            x -> 0]

qyloc = [ x -> -2.8*cos(ang1)^2

            x -> 0

            x -> 0]

```
