0
$\begingroup$

when i try to append values using do loop and if condition i can store all the values like below.

datax1 = {};

nl = 0.0; h = 0.007;

Do[If[t == nl*h, AppendTo[datax1, {t, 2}]; nl = nl + 10 ], {t, 0.0, 1.0,h}];

Length[datax1]

143

but when i use the same conditions in while loop i can store only less values using append to why ?

t = 0.0; nl = 0.0; h = 0.0007;datax2 = {};

While[t <= 1.0, If[t == nl*h, AppendTo[datax2, {t, 2}]; nl = nl + 10]; t = t + h]

 Length[datax2]

127

Can anyone help me in fixing this

$\endgroup$
  • $\begingroup$ @Bill Thanks for your reply, i have re edited the code and i have checked it but still i got the same result. I dont know why appendTo stores less values in while loop when we are using if condition. $\endgroup$ – revanth roy Nov 1 '18 at 4:40
  • $\begingroup$ Why not just use something like Table[{i, 2}, {i, 0., 1, .07}]? Similar to list comprehensions, if one must recall some counterpart. $\endgroup$ – Αλέξανδρος Ζεγγ Nov 1 '18 at 4:40
  • $\begingroup$ Thanks for the fixes. The reason for the difference appears to be how Mathematica deals with numbers containing decimal points. If you change all your numbers to exact integers or exact rationals with no decimal points then both blocks of code return 15. Once you have that out of the way then you might explore exactly what those extra steps are doing. You can use FullForm on a decimal value to see more than the usual half dozen digits that are displayed. $\endgroup$ – Bill Nov 1 '18 at 4:42
  • $\begingroup$ @ΑλέξανδροςΖεγγ thanks for the reply. Actually i want to store some points in the code using if condition .The table function stores all points. Can i know how your idea works? Thanks in advance $\endgroup$ – revanth roy Nov 1 '18 at 4:48
  • 1
    $\begingroup$ Or maybe you can select "valid" iterators by your conditions first, and then use Table. $\endgroup$ – Αλέξανδρος Ζεγγ Nov 1 '18 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.