Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9062879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:50:51+00:00 2026-06-16T15:50:51+00:00

See the following pseudocode snippet that approximates my situation: function foo () { for

  • 0

See the following pseudocode snippet that approximates my situation:

function foo () {
  for ( velocity=0; velocity<100; velocity++ ) {
    root1 = computeRoot1();
    root2 = computeRoot2();
    // do a bunch of computation with root1
    // if result of computation is undesirable, do computations again with root2 
  }

So, basically I want to do the computations in the body of the for loop with root1, and then root2 if root1‘s computation result is invalid.

My first instinct was the obvious approach, to wrap the computation in a help function, but I’m not sure this is the most clear approach. I’m trying for good collocation of information in my code, and a function call for code that will be executed at most twice (per iteration) defeats that goal without providing a great deal of conciseness to my code.

I was thinking perhaps a for loop like:

for ( root=root1; root1IsInvalid==true || bothRootsInvalid==true; root=root2 )

or a while with similar functionality. But I’m certainly open to other suggestions

As someone reading this code, which approach would make it the most readable and concise to you?

As an aside, I’m writing this particular function in JavaScript, but language-agnostic solutions would be awesome.

EDIT: clarified code snippet

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:50:52+00:00Added an answer on June 16, 2026 at 3:50 pm

    You have several basic approaches:

    1. Put the values in an array and use a for loop to run the same code on each item in the array, perhaps stopping the iterations when some condition is met.
    2. Create a function that does the computation and then just write code that calls the function on the first one, then the second one and so on.
    3. Create a while loop and repeat your code until some condition is met.

    The first option is easier to extend to N items. The second option is perhaps simpler for just two items.

    You can make the computation function be a local function (declared and used inside the function you are currently executing) so it doesn’t add to the global namespace and your code remains more encapsulated.

    I’m also not sure what you intend to be doing with this line:

    root1, root2 = computeRoots();
    

    But, it is only assigning the value to root2 and it looks like you probably want var in front of these to define them as local variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the following code of the <?php class DefaultController extends Controller { public function
I have the following javascript object, somewhat pseudocode: { dateField: new Date(), addMinutes:function(numMinutes) {
See following class: function availItem(xs, s, m, l, xl) { this.xs = xs; this.s
Why might my compiler see the following GetLength function pointer as ambiguous pseudo-code: size_t
See following code with resides in fillowing directory mypack.pack1 package mypack.pack1; public class myclass
Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {
Please see following examples first, and which one is better? Could you compare some
How can I implement cross browser opacity gradient (not color gradient)? See following code:
See the following: struct A { std::string* get() const { //return const_cast<std::string*>(&m_pObj); return &const_cast<A*>(this)->m_pObj;
See the following output: 1.9.3p194 :001 > player = Player.randomize_for_market => #<Player id: nil,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.