Contoh matlap method IF switch and while

clc;clear;
%untuk soal: 1-20 genap ^2
                    1-20 genap ^2 di balik dari besar kekecil
                    1-10 ganjil^3
                    1-10 ganjil^3 di balik dari besar kekecil
saya bagi menjadi 4 contoh untuk memudahkan pemahaman
%if elseif else method
for a=1:4;
    if a==1;
        for s=1:10;
            m_if(a,s)=(2*s)^2;
        end
    elseif a==2;
        for s=1:10;
            m_if(a,s)=(22-(2*s))^2;
        end
    elseif a==3;
        for s=1:5;
            m_if(a,s)=(s*2-1)^3;
        end
    else
        for s=1:5;
            m_if(a,s)=(10-(s*2-1))^3;
        end
    end
end
m_if

%switch case otherwise method
for z=1:4;
    switch z;
        case 1;
            for x=1:10;
                m_switch(z,x)=(2*x)^2;
            end
        case 2;
            for x=1:10;
                m_switch(z,x)=(22-(2*x))^2;
            end
        case 3;
            for x=1:5;
                m_switch(z,x)=(x*2-1)^3;
            end
        otherwise
            for x=1:5;
                m_switch(z,x)=(10-(2*x-1))^3;
            end
    end
end
m_switch

%if + while method
v=0;
while v<4;
    v=v+1;
    if v==1;
        b=0;
        while b<10;
            b=b+1;
            m_if_plus_while(v,b)=(b*2)^2;
        end
    elseif v==2;
        b=0;
        while b<10;
            b=b+1;
            m_if_plus_while(v,b)=(22.-(b.*2)).^2;
        end
    elseif v==3;
        b=0;
        while b<5;
            b=b+1;
            m_if_plus_while(v,b)=(2*b-1)^3;
        end
    else
        b=0;
        while b<5;
            b=b+1;
            m_if_plus_while(v,b)=(10-(b*2-1))^3;
        end
    end
end
m_if_plus_while

%switch + while method
q=0;
while q<4;
    q=q+1;
    switch q;
        case q==1;
            w=0;
            while w<10;
                w=w+1;
                m_switch_plus_while(q,w)=(w*2)^2;
            end
        case 2;
            w=0;
            while w<10;
                w=w+1;
                m_switch_plus_while(q,w)=(22-(2*w))^2;
            end
        case 3;
            w=0;
            while w<5;
                w=w+1;
                m_switch_plus_while(q,w)=(w*2-1)^3;
            end
        otherwise
            w=0;
            while w<5;
                w=w+1;
                m_switch_plus_while(q,w)=(10-(w*2-1))^3;
            end
    end
end        
m_switch_plus_while

%RESULT

m_if =

       4    16    36       64   100   144   196   256   324   400
   400   324   256   196   144   100     64     36     16       4
       1    27    125   343   729       0       0       0       0       0
   729   343   125     27      1        0       0       0       0       0


m_switch =


       4    16    36       64   100   144   196   256   324   400
   400   324   256   196   144   100     64     36     16       4
       1    27    125   343   729       0       0       0       0       0
   729   343   125     27      1        0       0       0       0       0


m_if_plus_while =


       4    16    36       64   100   144   196   256   324   400
   400   324   256   196   144   100     64     36     16       4
       1    27    125   343   729       0       0       0       0       0
   729   343   125     27      1        0       0       0       0       0


m_switch_plus_while =


       4    16    36       64   100   144   196   256   324   400
   400   324   256   196   144   100     64     36     16       4
       1    27    125   343   729       0       0       0       0       0
   729   343   125     27      1        0       0       0       0       0