Title: Do while loop in calculated field
Last modified: November 27, 2021

---

# Do while loop in calculated field

 *  Resolved [mewaqqas](https://wordpress.org/support/users/mewaqqas/)
 * (@mewaqqas)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/do-while-loop-in-calculated-field/)
 * Hi
    i am trying to create a form in which user will provide few inputs. based
   on which, form has to calculate required variable **iteratively**. so what i 
   am trying:
    1.  creating a loop (do-while loop) to solve for required variable iteratively.
 *  1.  it solve the equation after user click at the “calculate” button.
 * i have written the equation and its working as intended on local simple html 
   page. but when i include it plugin calculate field and click at the calculate
   button. my browser become unresponsive.
    please guide me how can i achieve the
   goals. here is the code of loop including equation. before this loop i simply
   declaring the variable.
 *     ```
        do {
                   Pcr=(Sigma*A)/(1+ec_over_r/COS(theeta*SQRT(old_force)));
                 allowed_error= ABS(Pcr-old_force);
                 old_force=Pcr;
                 }
                 while(allowed_error > 0.0001);
       return old_force;
       ```
   
 * Pcr is the variable i need to find. while old_force=1 in first iteration.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/do-while-loop-in-calculated-field/#post-15108524)
 * Hello [@mewaqqas](https://wordpress.org/support/users/mewaqqas/)
 * Please, send me the link to the form and the values you are entering for testing.
 * First, the use of `return` instruction requires you be into the context of a 
   function, like:
 *     ```
       (function(){
       /** YOUR EQUATION'S CODE HERE **/
       })()
       ```
   
 * Furthermore, based on the values of the variables, the condition `allowed_error
   > 0.0001` may never be true, and your code would be running in an endless loop
   blocking the browser.
 * Best regards.
 *  Thread Starter [mewaqqas](https://wordpress.org/support/users/mewaqqas/)
 * (@mewaqqas)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/do-while-loop-in-calculated-field/#post-15110273)
 * Thank You for your quick response.
    my problem got resolved. all i did is paste
   the equations again into the equation designated area while keeping the structure
   of code as you have suggested. `allowed_error > 0.0001` was not the problem in
   my case as this condition become false in less than 10 iterations and terminate
   the loop. calculator is giving result as expected based on input. now [My calculator](https://www.mechanical360.net/tools/critical-force-in-column-calculator/)
   is live on my website.
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/do-while-loop-in-calculated-field/#post-15110782)
 * Excellent !!!!!
 * Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Do while loop in calculated field’ is closed to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

## Tags

 * [loops](https://wordpress.org/support/topic-tag/loops/)

 * 3 replies
 * 2 participants
 * Last reply from: [codepeople](https://wordpress.org/support/users/codepeople/)
 * Last activity: [4 years, 6 months ago](https://wordpress.org/support/topic/do-while-loop-in-calculated-field/#post-15110782)
 * Status: resolved