I have 3 methods

MethodA()
MethodB()
MethodC()

All 3 methods must be run at least once per frame/update.

MethodA generates a integer count, let's call it 'intCount'. If the count is 0 then MethodB and MethodC don't need to be called.

All 3 methods will continue to be called, up to 5 times, if MethodA generates an 'intCount' other than 0.

This is my current logic for the above conditions.

for (int i = 0; i < 5; ++i)
            {
                MethodA();

                if (intCount == 0 && i > 0)
                {
                    break;
                }

                MethodB();
                MethodC();
            }

I'm not happy with its current state and I was hoping that someone could suggest an improvement.

Recommended Answers

All 9 Replies

If MethodA doesnt have to run before B and C then you could use a do..while loop:

int i = 0;
    do
    {
        MethodB();
        MethodC();
        i++;
    } while (MethodA() != 0 && i < 5);

Can you clarify, if MethodA returns zero the first time, do you still need to run MethodB and MethodC once?

Hmm How about this:

public void condop()
        {
            int i,intCount=0;
            bool Condition;

            for (i = 0; i < 5; ++i)
            {
                MethodA();

                Condition = intCount == 0 ? true : false;

                if (Condition == true)
                {
                    MethodB();
                    MethodC();
                
                }
            
               
            
            }
        }

@softwareguy, You might want to reconsider your code :p
This:

Condition = intCount == 0 ? true : false;
if (Condition == true)

is the same as

if(intCount==0)

Your loop will run 5 times, regardless of the result of MethodA() and will only run MethodB and MethodC if intCount is zero.

DaveTran, am i correct in thinking that if MethodA returns zero the first time through the loop, you still want to run MethodB and MethodC. Also, if MethodA returned zero and you ran MethodB and MethodC anyway, would you still want to perform the second iteration of your loop, getting a new value for MethodA which might not be zero? ie, is the following a correct result of your logic:

intCount = 0;
run MethodB
run MethodC

intCount = 3;
run MethodB
run MethodC

intCount = 0;
exit loop

Or should it be

intCount = 0;
run MethodB
run MethodC
exit loop

@Ryshad..
Yes that is true :], but atleast it allows him to take away that "break" statement.

Thanks for pointing out though.

Is it wrong to have a break?

Ryshad:

You are correct :)

- If MethodA returns 0, B and C must still be run once.
- The loop doesn't need to run again if MethodA returned 0 the first time.

intCount = 0;
run MethodB
run MethodC
exit loop

If intCount is anything other than 0 then the loop will keep going until intCount is 0 or the loop reaches the desired number of iterations.

How about this then:

int i = 0;
    int intCount = MethodA();
    do
    {
        MethodB();
        MethodC();
        i++;
    } while (i < 5 && intCount != 0 && (intCount = MethodA()) != 0);

Set your initial intCount and run through the loop. At the end of each loop, if you need another iteration, and if intCount isnt already zero, and its not zero after reassignment then it will run the loop again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.