Programming

Timings

Timing[N[Pi,10^2]]

Timing[N[Pi,10^4];]
Timing[N[Pi,10^5];]

If[], Which[], Switch[], With[], Module[], Block[] ...
Look up the constructs in the Mathematica guide!

Blocks

With[{
x=0, 
y=Apply[Plus,Array[v,{4}]]
},

With[{x=1},
Print["x=",x,"   y=",y];
];
Print["x=",x,"   y=",y];
]

Clear[x,y]

With[{
x=4,
y=x+5},

{x,y}
]

Probably that was not what you're looking for. 
Module[]

Module[{
x=4,
y=x+5},

{x,y}
]

Block[]

Block[{
x=4,
y=x+5},

{x,y}
]

Example: Fibonacci

What's the most efficient way to compute the n-th number of the Fibonacci sequence?

1) Recursive

fibR[0]=0;fibR[1]=1;
fibR[n_Integer?Positive]:=fibR[n-1]+fibR[n-2]

fibR[30]//Timing

n=15;
calls=Table[0,{n+1}]

fibR[0]:=(calls[[1]]++;0);fibR[1]:=(calls[[2]]++;1)
fibR[n_Integer?Positive]:=(calls[[n+1]]++;fibR[n-1]+fibR[n-2])

fibR[15]

calls

2) Dynamic Programming

$RecursionLimit=100000;

Clear[fibD]

fibD[0]=0;fibD[1]=1;
fibD[n_]:=fibD[n]=fibD[n-1]+fibD[n-2]

fibD[10000]

MemoryInUse[]

3+4

?fibR

?fibD

fibD[30]

?fibD

Clear[fibD]

fibD[1000]

Timing[fibD[30]]

3) Imperative: While, For, Do

?For

fibI[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,i++,
{t0,t1}={t1,t0+t1}
];
t1
)

fibI[1000]

fibI2[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,
i++,
aux=t0;t0=t1;t1=aux+t1
];
t1)

fibI2[1000]

4) Symbolic

RSolve[{g[n]==g[n-1]+g[n-2],g[1]==1,g[0]==0},g[n],n]

Clear[fibS]

fibS[n_]:=Expand[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))]

((((1/2)-((Sqrt[5])/2)))^16)//Expand

fibS[7]

fibS[1000]//Timing

5) Numeric

Clear[fibN]

fibN[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5])),Round[n/2]]

fibN[1000]//Timing

6) Functional

next[{x_,y_}]:={y,x+y}

?Nest

fibF[n_]:=
Nest[next,{0,1},n-1][[2]]

fibF[1000]//Timing

7) Matrix Form

M1={{0,1},{1,1}};
M2={{a,b},{b,c}};
{M1//MatrixForm,
M2//MatrixForm,
M1.M2/.{a+b->c,b+c->d}//MatrixForm}

Table[MatrixForm[MatrixPower[{{0,1},{1,1}},i]],{i,7}]

fibM[n_]:=
MatrixPower[{{0,1},{1,1}},n-1][[2,2]]

fibM[100]

{vals,vects}=Eigensystem[{{0,1},{1,1}}]

Expand[vals^1000];

Tests

$RecursionLimit= 10000

Clear[fibD];
fibD[0]=0;fibD[1]=1;
fibD[n_]:=fibD[n]=fibD[n-1]+fibD[n-2];

Map[
Flatten@{#,Timing[#[1000];]}&,{fibD,fibI,fibI2,fibS,fibN,fibF,fibM}
]//MatrixForm

Clear[fibD];
fibD[0]=0;fibD[1]=1;
fibD[n_]:=fibD[n]=fibD[n-1]+fibD[n-2];
$RecursionLimit=100000;

Timing[fibD[10000]]

fibD breaks down. Out of memory?

definitions

fibI[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,i++,
{t0,t1}={t1,t0+t1}
];
t1
)

fibI2[n_]:=
(
For[
i=1;t0=0;t1=1,
i<n,
i++,
aux=t0;t0=t1;t1=aux+t1
];
t1)

fibS[n_]:=Expand[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5]))]

fibN[n_]:=N[-((((((1/2)-((Sqrt[5])/2)))^n)-((((1/2)+((Sqrt[5])/2)))^n))/(Sqrt[5])),Round[n/2]]

next[{x_,y_}]:={y,x+y}

fibF[n_]:=
Nest[next,{0,1},n-1][[2]]

fibM[n_]:=
MatrixPower[{{0,1},{1,1}},n-1][[2,2]]

Map[
Flatten@{#,Timing[#[1000000];]}&,{fibN,fibM}
]//MatrixForm

FibM[n]

n=10;
{
Timing[fibM[n];],
Timing[Fibonacci[n];]
}