First Day Mathematica Demo

We will often do repetitive things in Mathematica. One useful construct is the Do loop.

Do[
Print[{i,i^2}],
{i,5,20,3}
]

Often we will need loops that modify some value at each iteration. A common construct is to initializes a variable outside of a loop, update the variable at each iteration of the loop, and then do something with the result.

sum=0;
Do[
Print[{i,i^2,sum}];
sum+=i^2,
{i,1,10}
]
sum

How to make plus/minus alternate?

Do[
Print[{i,(-1)^i}],
{i,1,10}
]

One solution to Exercise 1

sum=0;
Do[
(*Print[{i,2i-1,sum}];*)
sum+=(-1)^i/(2i+1)
,{i,0,1000}]
N[4*sum]