Pages: [1]   Go Down
Author Topic: 2 while loops in a program loop, both can be true, which runs first?  (Read 492 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So further to my trying to work out what the hell this code I have been given does, there is a main program loop and inside the loop there are 2 while loops.

the opening line of each is as follows:

Code:
while (interrupt_val_1==LOW&&interrupt_val_2==LOW){some code...}

Code:
while(total_elapsed_time<=max_total_time){some code....}

both these things can be true. i.e. the two interrupts can be LOW and the total elapsed time can be less than the max allowable time (which is 12 minutes I think going off what is in the definitions up top).

So, given they are both true, which happens first?

The while loops follow on from each other, but if say, we get to the end of the first loop and the interrupt conditions and time conditions are still met, does the program flow on to the next while loop? or does it go back round to repeat the first loop? or does it do loop 1, then loop 2, then go back to loop 1?

incidentally the next line of the second while loop is:
Code:
if (interrupt_val_1==HIGH||interrupt_val_2==HIGH){some more code...}

so if it did go, loop 1, loop 2, loop 1.... loop 2 will be incredibly quick as the above condition will not be met and so it would skip all the code.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code.
We don't do keyhole debugging here.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Things are done in order, however after saying that, I'm confused by your question.
Logged

New England
Offline Offline
Sr. Member
****
Karma: 7
Posts: 295
Natural Semiinductor
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The first while loop goes first. If an interrupt happens, then that has priority over the while loop. After the first while loop is finished, the second while loop would go into action.
Logged

I am going to get going.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok guys cheers

I dont think i can post the code as it is, it is part of work that is confidential. I think posting it would just raise even more confusing questions as well. I will try tidy it up and remove commenting that relates to confidentiality and I may post it.

I have not written it, and I am 95% sure that it is not the final code I am supposed to have. I believe that has been lost into the ether or has gone walkies on the personal computer of the person who wrote it (who has since apparently destroyed a lot of information on confidentiality grounds). So this is what I am left with. It is poorly commented and seems to have been modified from even older code that did the same thing, but with different hardware and as such variables have been re-used and mixed in with new variables and its all gotten a bit confusing.

I just needed to know how loops behave if there are two loops, (one after the other within the main program loop, which use two different conditions for their execution), and if those two conditions are concurrently true. If I am understand right, the first loop will go, then the second, then we go back to the first. I was just unclear on how, once its entered the first loop, how it can escape to run the second loop, while the condition for the first loop is still met. i.e. should it not leave the first loop only when the condition is no longer met?

My idea of a while loop is:

you enter the loop based on a condition being true.

You then stay in the loop until this condition is no longer true.


so if I have:

Code:
void loop(){
n=0;
while(n<6){
n++;

while(value_1==LOW&&value_2=LOW){ some code }

while(total_time <= max_allowable_time){some more code}
}
some further code to do after the while loops
}

which is the basic structure of what I have actually. I enter the first loop which goes round as long as n is less than 6. I then enter the second nested loop because my value_1 and value_2 are LOW. My question then is, does the program loop around in there until such time that one of the values is no longer LOW?

Now, lets us say that an event happened and my interrupts fire. This now means value_1 is HIGH. Assuming I am then kicked out of that loop upon returning to the program, I should enter the second loop which will run if total time taken so far is less than the allowable time I have defined. I then go in that loop and it loops round.

The problem I see is that, if value_1 goes back LOW, nothing will happen. I will be stuck in the second loop until the elapsed time breaches the maximum.

But then maybe that is the point. In trying to explain it here, I can see things starting make a bit more sense. It is a great insight into other peoples logic looking at how they write code!
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem I see is that, if value_1 goes back LOW, nothing will happen. I will be stuck in the second loop until the elapsed time breaches the maximum.

Correct. And if you aren't checking the elapsed time within the second while loop it will be stuck there forever unless it's changed within the interrupt routine and an interrupt occurs. That's the reason we ask to see all the code. Otherwise we are guessing.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont think i can post the code as it is, it is part of work that is confidential.


This is a fundamental question about how code executes, and you should be able to post a code example that demonstrates the issue you are asking about without requiring any confidentiality.

My guess is that you are thinking in terms of a declarative language - in fact, you're programming in C++ which is a procedural language.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont think i can post the code as it is, it is part of work that is confidential.

This is a fundamental question about how code executes, and you should be able to post a code example that demonstrates the issue you are asking about without requiring any confidentiality.

For that matter write a small test program and see for yourself.
Logged

Pages: [1]   Go Up
Jump to: