Effacer les filtres
Effacer les filtres

How can I display all the answer from while looping?

1 vue (au cours des 30 derniers jours)
Indri Djon Hansemit
Indri Djon Hansemit le 7 Mai 2013
I'm a beginner. I tried practise with looping "while". The following codes were :
%whilelooping
a=4;
b=2;
c=a/b;
while c>1
a=a-1;
c=a/b;
end
disp(a);
disp(b);
disp(c);
And the answer was 2 2 1. I don't understand how to make the answer become : a=4 3 2, b=2 2 2, c=2 1.5 1. Need help, thank you for advice.

Réponses (1)

David Sanchez
David Sanchez le 7 Mai 2013
Since you are using disp() outside the while loop, only after exiting the while-loop the disp() comes to play, displaying the last value of the variables. Note that if you do not add semicolon at the end of a line, the value of the variable acting on that line will be displayed on the command widow ( as below )
a=4;
b=2;
c=a/b;
while c>1
a=a-1
b
c=a/b
end
But this code will not present your data in array format either, since your variables are not arrays. To do so, you should redefine your variables as such and think again what you want to do.

Community Treasure Hunt

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

Start Hunting!

Translated by