F[a_,b_]:=Which[
(((D[f[ξ],ξ])/.ξ->a)>0)&&(((D[f[ξ],ξ])/.ξ->b)>0),f[a],
(((D[f[ξ],ξ])/.ξ->a)<0)&&(((D[f[ξ],ξ])/.ξ->b)<0),f[b],
((D[f[ξ],ξ]))==0,0
]
u=1;
L=2;
f[q_]:=u*q;

q0[x_]:=Piecewise[
{{0,x<-1},
{1,x≥-1&&x≤1},
{0,x>1}}
]

Plot[q0[x],{x,-L-2,L+2}]
II=100;
NN=50;
dx=2L/(II-1);
dt=dx;
x=Range[-L,L,dx];
Q={};
AppendTo[Q,Table[q0[x[[i]]],{i,1,II}]];
For[
n=1,n≤NN+1,n++,
{
AppendTo[Q,Table[0,{j,1,II}]];
For[
i=2,i≤II-1,i++,
{
Q[[n+1,i]]=Q[[n,i]]-(dt/dx)*(F[Q[[n,i]],Q[[n,i+1]]]-F[Q[[n,i-1]],Q[[n,i]]]);
}];
}];
Manipulate[ListPlot[{Table[{x[[i]],Q[[n,i]]},{i,1,II}]},Joined->True,PlotRange->{{-L-1,L+1},{0,1.2}}],{n,1,NN,1}]

s[a_,b_]:=If[a≠b,(f[b]-f[a])/(b-a),a];

F[a_,b_]:=Which[
(((D[f[ξ],ξ])/.ξ->a)>=0)&&(((D[f[ξ],ξ])/.ξ->b)>=0)&&(s[a,b]>=0),f[a],
((D[f[ξ],ξ]/.ξ->b)>=(D[f[ξ],ξ]/.ξ->a))&&((D[f[ξ],ξ]/.ξ->a)>=0),f[b],
((D[f[ξ],ξ]/.ξ->a)<=0)&&0<(D[f[ξ],ξ]/.ξ->a),f[0.5]  (*    ???!?!?!?!*),
(((D[f[ξ],ξ]/.ξ->a))<=(D[f[ξ],ξ]/.ξ->b))&&(D[f[ξ],ξ]/.ξ->b)<=0,f[a],
(((D[f[ξ],ξ]/.ξ->a))>=(D[f[ξ],ξ]/.ξ->b))&&s[a,b]<=0,f[b]
]
u=1;
L=2;
f[q_]:=0.5*u*q^2;


q0[x_]:=Piecewise[
{
{0,x<-1},
{1,x>-1&&x<1},
{0,x>1}}
]
Plot[q0[x],{x,-L-2,L+2},PlotRange->{{-L-1,L+1},{0,2.2}}]
II=100;
NN=50;
dx=2L/(II-1);
dt=dx;
x=Range[-L,L,dx];
Q={};
AppendTo[Q,Table[q0[x[[i]]],{i,1,II}]];
For[
n=1,n≤NN+1,n++,
{
AppendTo[Q,Table[0,{j,1,II}]];
For[
i=2,i≤II-1,i++,
{
Q[[n+1,i]]=Q[[n,i]]-(dt/dx)*(F[Q[[n,i]],Q[[n,i+1]]]-F[Q[[n,i-1]],Q[[n,i]]]);
}];
}];
Manipulate[ListPlot[{Table[{x[[i]],Q[[n,i]]},{i,1,II}]},Joined->True,PlotRange->{{-L-1,L+1},{0,2.2}}],{n,1,NN,1}]

*

Q[[3]]

((D[f[ξ],ξ]/.ξ->a)<0)&&(0<(D[f[ξ],ξ]/.ξ->a))

F[a_,b_]:=Which[
(((D[f[ξ],ξ])/.ξ->a)>0)&&(((D[f[ξ],ξ])/.ξ->b)>0)&&(s[a,b]>0),f[a],
((D[f[ξ],ξ]/.ξ->b)>(D[f[ξ],ξ]/.ξ->a))&&((D[f[ξ],ξ]/.ξ->a)>0),f[b],
(D[f[ξ],ξ]/.ξ->a)<=0&&0<=(D[f[ξ],ξ]/.ξ->a),1(*    ???!?!?!?!     *),
(D[f[ξ],ξ]/.ξ->a)<=(D[f[ξ],ξ]/.ξ->b)&&(D[f[ξ],ξ]/.ξ->b)<0,f[a],
(D[f[ξ],ξ]/.ξ->a)>(D[f[ξ],ξ]/.ξ->b)&&s[a,b]<0,f[b]
]

Manipulate[ListPlot[{Table[{x[[i]],Q[[n,i]]},{i,1,II}]},Joined->True,PlotRange->{{-L-1,+1},{0,2.2}}],{n,1,NN,1}]

q0[x_]:=Piecewise[
{{2,x<0},
{1,x>0}}
]

q0[x_]:=Piecewise[
{

{0,x<-2},
{-1,x>-2&&x<-1},
{0,x>-1&&x<1},
{1,x>1&&x<2},
{0,x>2}}
]

q0[x_]:=Piecewise[
{
{0,x<-1},
{1,x>-1&&x<1},
{0,x>1}}
]