# For loop to evaluate summation

**URL:** <https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272>\
**Category:** New to Julia\
**Created:** [February 24, 2017, 3:16pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272 "2017-02-24T15:16:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 3:16pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/1 "2017-02-24T15:16:48Z")

</div>

I have to evaluate:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac7b93d4d1e848cdd20f2ea334ddf42c353de706.png)

How can I evaluate it from this point on?

```julia
for i=1:1000
    if y=0.5^i ==
        display("even")
    else
        display("odd")
    end
end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 24, 2017, 3:38pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/2 "2017-02-24T15:38:14Z")

</div>

What is the condition that i has to satisfy to be even? (And please don’t post homework questions here.)

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [February 24, 2017, 3:48pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/3 "2017-02-24T15:48:02Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd13fccea00a78e0ff3bcb876c7d7da39a1aaac5.jpeg)

---

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 3:55pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/4 "2017-02-24T15:55:42Z")

</div>

It’s just an exercise to learn how to write in julia, and i’m asking for help since i just started yesterday.  
If i goes from 1 to 1000 and I have to take only even values of y then what should I put after the equal sign, in other words how can i express in code “take only even values” ?

---

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 4:05pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/5 "2017-02-24T16:05:29Z")

</div>

😏

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [February 24, 2017, 4:07pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/6 "2017-02-24T16:07:12Z")

</div>

[http://docs.julialang.org/en/release-0.5/manual/mathematical-operations/](http://docs.julialang.org/en/release-0.5/manual/mathematical-operations/)

---

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 4:15pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/7 "2017-02-24T16:15:58Z")

</div>

What’s the point of these answers? If my piece of code is wrong you could just write it’s wrong. I know where the documentation is

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 24, 2017, 4:33pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/8 "2017-02-24T16:33:38Z")

</div>

In Julia (or programming in general), there are always several different ways to solve a problem.

One way to check if a number is even in Julia is to use the function “iseven”, eg

if iseven(3)  
println(“3 is even”)  
end

Another is to realise that a number is even if the remainder when you divide by 2 is 0:

a = 3  
if a % 2 == 0  
…  
end

---

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 4:53pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/9 "2017-02-24T16:53:27Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 24, 2017, 5:16pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/10 "2017-02-24T17:16:09Z")

</div>

i think you started from the wrong angle. you have for, which is a “do this” sorta thing, while you need an expression, a “value” kinda thing.

the top level is a sum. in julia, it is `sum`.

inside the sum, you have an expression to be evaluated at points. in julia, it is a generator, that is, a `for` inside parens with a range and an `if` to select only evens. `iseven` checks for even-ness. example generator: `(2i for i in 5:100 if isprime(i))`

the whole thing must fit in one line.

---

<div class="post-metadata">

**Author:** ![Laura\_Macciocchi](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Laura\_Macciocchi](https://discourse.julialang.org/u/Laura_Macciocchi)\
**Post date:** [February 24, 2017, 5:23pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/11 "2017-02-24T17:23:48Z")

</div>

Thank you very much! Is this ok?

```julia
y = sum(0.5^i for i in 1:1000 if iseven(i))

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 24, 2017, 5:37pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/12 "2017-02-24T17:37:37Z")

</div>

> [@Laura\_Macciocchi](#):
>
> Thank you very much! Is this ok?

Learning how to develop a test to check your code is the most important thing in programming. So let me throw it back at you: what are some ways that you can check that it’s okay?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 25, 2017, 2:44pm UTC](https://discourse.julialang.org/t/for-loop-to-evaluate-summation/2272/13 "2017-02-25T14:44:13Z")

</div>

One way to make code like this more useful (and to answer @ChrisRackauckas’s question) is to not “hard-code” numerical values like 1000, but rather to make them variables. In this case, it would be natural to want to sum over `1:something` for different values of `something`, so we turn it into a function:

```julia
my_sum(N) = sum(0.5^i for i in 1:N if iseven(i))

```

and then call

```julia
my_sum(2)

```

```julia
my_sum(3)

```

etc.  
(Note that `my_sum(1)` gives an error, since there is nothing to sum over.)
