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 need loops that modify some variable at each iteration. A common construct is to initialize a variable outside of the loop, update it with 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

Solution to Exercise 1

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