Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Make a loop in such way that i will be increasing by 5 from 4 up to 30 and by 3 from 30 till up to 50.

1 visualización (últimos 30 días)
im still confused on using while loops and if loops can someone help me with this question
  2 comentarios
Rik
Rik el 7 de Oct. de 2019
It is extremely rude to edit away your question, especially if you received an answer from someone who took the time to read and understand your question and then spent time finding a solution and posting an answer. If you want private help, hire a consultant.

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de Oct. de 2019
for VariableName = [4:5:30,30:3:50]
Note that in a case like this, you should always check in case you accidentally repeat a value. For example, [4:13:30, 30:3:50] would start [4, 17, 30] and then continue from [30 33 36 ...] . It so happens that 4:5:30 stops at 29 and so there is no duplication of 30.
  1 comentario
Walter Roberson
Walter Roberson el 7 de Oct. de 2019
for VariableName = [4:5:30,30:3:50]
something
end
is the same as
for VariableName = [4:5:30]
something
end
for VariableName = [30:3:50]
something
end
In turn,
for VARIABLENAME = FIRST_VALUE : INCREMENT : FINALVALUE
something
end
for positive increment is the same as (for your needs, but some of the details differ)
VARIABLENAME = FIRST_VALUE;
while VARIABLE_NAME <= FINALVALUE
something
VARIABLENAME = VARIABLENAME + INCREMENT;
end
So you can simply write two while loops in a row.
However, you can also code something like,
VARIABLENAME = FIRST_VALUE;
first_phase = true;
while VARIABLE_NAME <= FINALVALUE
something
if first_phase
VARIABLENAME = VARIABLENAME + FIRSTINCREMENT;
if VARIABLENAME > BREAKPOINT
VARIABLENAME = BREAKPOINT;
first_phase = false;
end
else
VARIABLENAME = VARIABLENAME + SECONDINCREMENT;
end
end

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by