Borrar filtros
Borrar filtros

Replacing a for loop with a while loop?

4 visualizaciones (últimos 30 días)
Kali
Kali el 26 de Jul. de 2013
Respondida: Bob Khosh el 25 de Abr. de 2018
I'm given the following code:
x = 1;
for i = 1:5
x = x*i;
end
And then I'm asked to rewrite the script using a while loop such that it would yeild the same result.
Being relatively new to MATLAB and having a poor grasp on loops, I have no idea how to attempt this.

Respuestas (2)

Youssef  Khmou
Youssef Khmou el 26 de Jul. de 2013
hi,
Using while, you need to increment the variable manually, :
y=1;
time=1;
while time<6
y=y*time;
time=time+1;
end

Bob Khosh
Bob Khosh el 25 de Abr. de 2018
x=1; i=0;
while i<5
i = i+1;
x = x*i;
end

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by