1

I am wondering if there is a way to check multiple while loop conditions using the same variable. That's a bit vague, but this example should clear it up:

while (myFunction(x) == 0 || myFunction(x) == 13639 || myFunction(x) == -4261.9583)
{ x++; }

Is it possible to only evaluate myFunction(x) once per loop while checking the three conditions, so that the function doesn't have to run three separate times for a result that is the same each time?

I am doing this for optimization/efficiency purposes. myFunction() could be a pretty time-consuming function, so I want it to run the minimum amount of times necessary.

Typically I would define the value of myFunction(x) before I start the while loop, but in this case, the value of myFunction(x) will be changing as the loop goes through each iteration, since the value of x will be changing.

2
  • 2
    You can extract it to a function. It would fulfill your requirements and also would be better in terms of code transparency - the name of the function will self-explain the meaning of these currently "magic" numbers like 0, 13639, -4261.9583. Commented May 7, 2019 at 17:52
  • I concur with @YeldarKurmangaliyev he is right, extract it to a function; for example a bool return would suffice... Also can you show the code for myFunction()? You are already doing something I would assume... Commented May 7, 2019 at 17:54

3 Answers 3

4

Yes, this can be done:

double result;
while ((result = myFunction(x)) == 0 || result == 13639 || result == -4261.9583)
{ x++; }
Sign up to request clarification or add additional context in comments.

1 Comment

You can also declare the variable inline in recent version of C# (7?): while (myFunction(x) is var result && (result == 0 || result == 13639 || result == -4261.9583))
2

Three options:

Do it backwards

Check a list for the result rather than checking the result against the list.

var list = new float[] { 0F, 13639F, -4261.9583F );
while (list.Contains(myFunction(x))
{
    x++;
}

Write a function

Extracting the logic to another function is always a nice way to break down the problem.

bool IsValid(float input)
{
    var result = myFunction(input);
    return (result == 0 || result == 13639 || result == -4261.9583);
}

while (IsValid(x))
{ 
    x++;
}

Use while(true)

Whenver the condition of a while loop is complicated, a common option is to remove the check from the () and put it in the {} instead. When you do this, use while (true).

while (true)
{
    var result = myFunction(x);
    if (result != 0 && result != 13639 && result != -4261.9583) break;
    x++;
}

Comments

1

You can do this simply by moving the check into a separate method, which takes the return value of the function as parameter, and then performs the 3 checks on the value directly and returns a boolean accordingly.

Assuming the function returns a int typed value this may for example look something like this:

bool CheckResultValue(int value) {
    return value == 0 || value == 13639 || value == -4261.9583;
}

Then your while loop could look something like this:

while (CheckResultValue(myFunction(x)))
{ x++; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.