### Nest while mathematica

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Not really the answer to the original question, but I noticed that many people got interested in 0 stuff, so here I put a couple of non-trivial examples, hope they will be useful. Regarding the statement that for nested functions one should use functions with named arguments: while this is generally true, one should always keep in mind that lexical scoping for pure functions and generally is emulated in Mathematica, and can be broken. This behavior has to do with the intrusive nature of rule substitutions - that is, with the fact that Rule and RuleDelayed don't care about possible name collisions between names in scoping constructs which may be present in expressions subject to rule applications, and names of pattern variables in rules.

What makes things worse is that g and f work completely fine when taken separately. It is when they are mixed together, that this entanglement happens, and only because we were unlucky to use the same pattern variable x in the body of fas in a pure function. This makes such bugs very hard to catch, while such situations do happen sometimes in practice, so I'd recommend against passing pure functions with named arguments as parameters into higher-order functions defined through patterns. Expanding a bit on emulation of the lexical scoping.

What I mean is that, for example, when I create a pure function which is a lexical scoping construct that binds the variable names in its body to the values of passed parametersI expect that I should not be able to alter this binding after I have created a function. This means that, no matter where I use Function[x,body-that-depends-on-x]I should be able to treat it as a black box with input parameters and resulting outputs.

For example:. I was bitten by this last behavior a few times pretty painfully. This behavior was also noted by WReach at the bottom of his post on this page - obviously he had similar experiences. There are other ways of breaking the scope, based on exact knowledge of how Mathematica renames variables during the conflicts, but those are comparatively less harmful in practice. Generally, I don't think these sorts of things can be avoided if one insists on the level of transparency represented by Mathematica expressions.

It just seems to be "over-transparent" for pure functions and lexical scoping constructs generallybut on the other hand this has its uses as well, for example we can forge a pure function at run-time like this:. Where the integral is computed only once, at definition-time could use Evaluate as well. So, this looks like a tradeoff. In this way, the functional abstraction is better integrated into Mathematica, but is leaky, as WReach noted.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. This program should take inputs for the name and price of items and output them to a text file. When the sentinel value is entered for the item name, the while loop should cease and output all sets of inputs item name and price to the text file.

**Write Mathematica commands to compute 10!(Factorial 10) using Do loop, For loop and While loop.**

Only the most recent set of inputs name, price is displayed. How do I keep all inputs in memory? Entering for the item name does not cause the program to exit. Instead, the program ceases to display prompts.

How do I stop the program correctly? Then after you read in a name and price, stuff it in a structure and stuff the structure into the vector. This is a good start. Exactly right. Thing is, item-name hasn't been set the first time you get here, forcing some squirrelly logic inside the loop. General rule of thumb is read, then test. Test before read is not so useful. For one thing, there's nothing to test, but the real problem is now you're using untested data or have to include another test to catch it.

Reading in numerical values has a few dangers you have to watch out for. The big one is if whatever the user typed can't be turned into pricecin goes into error mode and won't get back out until the error is cleared.

And before you try to get price again, the garbage data needs to be removed. This lets you stack commands. If cin is still in a good state, all of the reads completed successfully, it can be tested and will return true.

This goofy-looking bit of code is about the safest way to do this. It will even catch and exit cin comes to a sudden end. Doesn't happen all that often with cinbut this is a great way to test for the end of a file. As an aside, don't do this trick with a file.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I ran into a weird situation today doing some one-time sql code. This nested loop doesn't seem to run the outer loop: it prints 0,00,10,2 and 0,3. Learn more. Asked 10 years, 3 months ago. Active 4 years, 8 months ago. Viewed 27k times. Thanks for this Q! I needed a big smile today.

Active Oldest Votes.

You are not resetting j. Chjquest Chjquest 2 2 silver badges 8 8 bronze badges.

## Subscribe to RSS

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Dark Mode Beta - help us root out low-contrast and un-converted bits.

Technical site integration observational experiment live on Stack Overflow. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.NestWhile [ fexprtest ]. NestWhile [ fexprtestm ]. NestWhile [ fexprtestAll ]. NestWhile [ fexprtestmmax ]. Start comparisons after 4 iterations, and compare using the 4 last values:.

Start comparisons after 4 iterations, and compare using the 6 last values:. Stop after at most 4 iterations, even if the test is still True :. Perform one more step after the condition is no longer True :. Return the last value for which the condition was still True :.

NestWhile returns if the condition returns anything other then True :. The outcome of a condition need not be True or False :. FixedPoint always compares the last two values; these two forms are equivalent:.

NestWhileList applies the same stopping criteria, but returns all values generated:. NestWhile can be expressed in terms of a While loop:. Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how.

NestWhile [ fexprtest ] starts with exprthen repeatedly applies f until applying test to the result no longer yields True. NestWhile [ fexprtestm ] supplies the most recent m results as arguments for test at each step. NestWhile [ fexprtestAll ] supplies all results so far as arguments for test at each step. NestWhile [ fexprtestmmax ] applies f at most max times.

NestWhile [ fexprtestmmaxn ] applies f an extra n times. NestWhile [ fexprtestmmax- n ] returns the result found when f had been applied n fewer times. NestWhile [ fexprtest ] returns the first expression f [ f [ … f [ expr ] … ] ] to which applying test does not yield True. If test [ expr ] does not yield TrueNestWhile [ fexprtest ] returns expr.

### While Loop in Mathematica

It does not put the results res i in a list.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.

Looking through the reference. I need to nest many for-loops in such a way I can do many things in the innermost for-loop. How does one do this in Mathematica? Here is an example which you can may be use to demonstrate of the kind of thing I want to do on Mathematica. If yes, then I am printing out the value of a,b and c. Since this is a quartic equation, I would think that Mathematica has a way of calculating the exact roots in terms of square-roots and then doing the comparison.

Also since this polynomial evaluates the eigenvalues of a Hermitian matrix, all roots should be positive. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered.

How do I write nested for-loops? Ask Question. Asked 5 years, 2 months ago. Active 5 years, 2 months ago. Viewed 3k times. Is such a kind of nested for-loop doable in Mathematica? Please take a look at the documentation for details.

All the examples on reference. Pay attention to the difference betweenand. But please do not use For if you are a beginner in Mathematica Line breaks don't matter at all, just like in C, Java, etc.Hot Threads. Featured Threads. Log in Register. Search titles only. Search Advanced search…. Log in. JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding. While Loop in Mathematica. Mathematica Thread starter joshmccraney Start date Feb 25, Hi PF!

I've never used the while loop in Mathematica, tried reading the documentation but was still uncertain.

Any help would be great! Scott Homework Helper. Homework Helper. Insights Author. I'm just a passerby on this question. I don't know Mathematica, but I know other software. What is the way to have multiple operations in your computation? Scott said:. DrClaude Mentor. Why have you defined NotDone as True? I just want to understand how to do this in Mathematica.

DrClaude said:.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up. Have I missed something or is there no built-in which mimics the behaviour of do I am looking for a construct that evaluates procedure once and then repeatedly evaluates it while test is not fulfilled.

But clearly the second argument is redundant, so I have a feeling that I'm missing something. Maybe not, but I just want to be sure :.

## Subscribe to RSS

While While[procedure; test] works, it looks very similar to While[test, procedure]. The only difference is ; vs. The only argument here is readability and "defensive programming" extra effort to avoid accidental problems.

Readability is subjective. Sign up to join this community. The best answers are voted up and rise to the top.

Home Questions Tags Users Unanswered. Asked 6 years, 1 month ago. Active 3 years, 6 months ago. Viewed 8k times. I mean, docs say first arg is a test which may be missleading and the example you are referring to is ok but formed in easy-to-overlook way. Especially for new users. I think it could be an useful topic for future visitors. My goal is to show to future beginners :who may have doubts similar to mine in the past, that this is the way.

Oct 5 '16 at Active Oldest Votes. Szabolcs Szabolcs k 18 18 gold badges silver badges bronze badges. Chip Hurst Chip Hurst I'm afraid I don't understand. RunnyKine RunnyKine

## thoughts on “Nest while mathematica”