Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Re: help for while loop

$
0
0
On 12/30/2012 3:17 PM, Xing Li wrote:
> I have a very simple while loop, which was supposed to break right away until n = 10.
>But this loop never break because n = 10.1 in the end. How could this happened?
>
> n = 0;
> flag = 0;
> while (flag == 0)
> if( n < 10)
> n = n + 0.1;
> if(n == 10)
> flag = 1;
> end
> end
> end
>

and just for fun, I tried the above in Mathematica, which uses
arbitrary-precision numbers (but can be made to work in
machine precision as well)

------------
n = 0;
flag = 0;
While [flag == 0,
    If[n < 10,
       n = n + 0.1;
       If[n == 10,
           flag = 1
       ]
    ]
]
--------------------

In[14]:= n
Out[14]= 10.


--Nasser

Viewing all articles
Browse latest Browse all 19628

Trending Articles