Borrar filtros
Borrar filtros

How can I change this loop to a switch loop?

1 visualización (últimos 30 días)
Valeria Chacon
Valeria Chacon el 21 de Nov. de 2016
Comentada: Valeria Chacon el 21 de Nov. de 2016
width=input('Enter a positive number: ');
disp(' ');
while width<0
width=input('Enter a positive number: ');
disp(' ');
end
%dimension input
dimension=input('Input an integer greater than 1: ');
disp(' ');
while (dimension<=1)
dimension=input('Input an integer greater than 1: ');
end
reorint=0;
while reorint==0
if isreal(dimension)&&mod(dimension,1)==0
reorint=1;
else
dimension=input('Input an integer greater than 1: ');
reorint=0;
end
end
%colors input
color1=input('Enter 1st color: red(r), yellow(y), green(g), black(k), blue(b), magenta(m), white(w), cyan(c): ','s');
disp(' ');
color2=input('Enter different 2nd color: red(r), yellow(y), green(g), black(k), blue(b), magenta(m), white(w), cyan(c): ','s');
disp(' ');
N=4;
R=width;
Phase=pi/4;
theta=zeros(1,N);
for b=1:N
theta(b)=2*pi*(b-1)*(1/4);
end
x1=R*cos(theta+Phase)/sqrt(2); %cosine function
y1=R*sin(theta+Phase)/sqrt(2); %sine function
for m= 0 : dimension-1
for n= 0 : dimension-1
if mod(m+n,2) == 0 %adding your columns and rows to fill them with 2 different colors
fill(x1+m*width, y1+n*width,color1);
else
fill(x1+m*width, y1+n*width,color2);
end
hold on;
end
hold on;
end
How can I change the for loops starting from where it say "for b=1:N;" and the rest of the code and make it into a switch loop? Even if I can't change all of it can a change a few parts of it?
  2 comentarios
Alexandra Harkai
Alexandra Harkai el 21 de Nov. de 2016
' switch ' and loop statements are different and not directly interchangeable the way you sound to want them.
Why do you want to change those loops to a switch statement?
Valeria Chacon
Valeria Chacon el 21 de Nov. de 2016
I just wanted to see it from a different approach and I thought that a switch statement could be done, but yes I understand that they're not the same thing, thank you!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by