5

This is my code:

while( Func(x) != ERR_D)
{
   if(result == ERR_A) 
         throw...; 
   if(result == ERR_B)
         throw...;

  mydata.x = x;
 }

The problem is that I want to use result = Func(x) in the while condition as the result will be checked inside the while loop. The while loop should call Func(x) untill it returns ERR_D. I can't use

do{ 
    result = Func(x);
   if(result == ERR_A) 
         throw ...; 
   if(result == ERR_B)
         throw ...;
    mydata.x = x;
   }while(result != ERR_D); 

in my project as it first calls Func(x) which is what I don't want. But I have tried while(result = Func(x) != ERR_D), it doesn't work. Any ideas to solve this?

3
  • 2
    Well, x never changes. Maybe that has something to do with it? Hard to say, since "doesn't work" could mean almost anything and we have no idea what's supposed to be happening. Commented Sep 24, 2013 at 8:59
  • var result = Func(x); while (result != ERR_D) { doStuff(); result = Func(x); }? Commented Sep 24, 2013 at 8:59
  • Does it really make sense to throw exceptions in a while-loop? the loop will terminate as soon as one is thrown... Commented Sep 24, 2013 at 9:08

5 Answers 5

8

You just need to add some parentheses:

while((result = Func(x)) != ERR_D) { /* ... */ }

The != operator has a higher priority than the assignment, so you need to force the compiler to perform the assignment first (which evaluates to the assigned value in C#), before comparing the values on both sides of the != operator with each other. That's a pattern you see quite often, for example to read a file:

string line;

while ((line = streamReader.ReadLine()) != null) { /* ... */ }
Sign up to request clarification or add additional context in comments.

Comments

6

Try declaring result outside the loop and then assign it Funcs return value on each iteration.

For instance:

var result = Func(x);

while(result != ERR_D)
{
   if(result == ERR_A) 
         throw...; 
   if(result == ERR_B)
         throw...;

  mydata.x = x;
  result = Func(x);
 }

Comments

2

Try this:

while((result=Func(x)) != ERR_D){
 if(result == ERR_A) 
      throw...; 
 if(result == ERR_B)
      throw...;
 mydata.x = x;
}

NOTE: the assignment is done first in the parenthesis (result=Func(x)), this assignment is in fact done by the overload of the operator = and this operator returns the reference to the left side operand, that is result. After that, the result will be compared against the ERR_D via the operator !=.

Comments

1

Try

while((result = Func(x)) != ERR_D)

Comments

0

You could express this using a while (true)...:

while (true)
{
    var result = Func(x);

    if (result == ERR_D)
        break;

    if (result == ERR_A) 
        throw ...; 

    if (result == ERR_B)
        throw ...;

    mydata.x = x;            
}

This is a loop-with-one-exit (if you ignore the throws ;)) so it is a structured loop.

You could also use a for loop, although it looks a little funky (pun not intended!):

for (var result = Func(x); result != ERR_D; result = Func(x))
{
    if (result == ERR_A) 
        throw ...; 

    if (result == ERR_B)
        throw ...;

    mydata.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.