Logic condition evluation timing & While loop

Moderators: TomKerekes, dynomotion

Post Reply
smartmc
Posts: 7
Joined: Sat Jan 16, 2021 12:59 am

Logic condition evluation timing & While loop

Post by smartmc » Tue Jul 12, 2022 7:21 pm

Hello,
I hope to understand when does "While" evaluate it's logic condition in this simple program to better understand how execution is sequenced in C programs in general.
Referring to the below program that runs but have some strange behavior.
Q.1: If I don't evaluate the LogicVal after the "while" (**(2)) for the second time, the program starts when bit 1026 is 1, but never stops even it 1026 become 0, why?
I understand the evaluation (**(1)) should be sufficient since after completion of the while loop it will be re-evaluated in the outer (;;) forever loop,
am I missing something?

Q.2: If bit 1026=1 then immediately 1026=0 during execution of the loop, the loop does not end after completion of current 4 iterations but makes another round of 4 iterations! why?
I understand that 1026 status is re-evaluated after the completion of the 4 iterations of the while loop not before.
The logic goes that if the motor is still oscillating then the loop is still running then 1026 is not evaluated, or the program execution is not like this?
Appreciate clarifications.
Thanks.
--------------------------------------------------------
#include "KMotionDef.h"
void main()
{
int LogicVal;
for (;;)
{

LogicVal=ReadBit(1026);//***(1) test output

while (LogicVal==1)
{
LogicVal=ReadBit(1026);//***(2)
SetBit(48);//Servo Enable output of the Pulse/Dir

int i;
for (i=0; i<4; i++)
{
MoveAtVel(0,3500,4000);
while(!CheckDone(0));
Delay_sec(0.3);
MoveAtVel(0,1000,4000);
while(!CheckDone(0));
Delay_sec(0.3);
}
}
ClearBit(48);
}
}
-----------------------------------------------------------

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Logic condition evluation timing & While loop

Post by TomKerekes » Tue Jul 12, 2022 8:17 pm

when does "While" evaluate it's logic condition
At the beginning of the loop
Q.1: If I don't evaluate the LogicVal after the "while" (**(2)) for the second time, the program starts when bit 1026 is 1, but never stops even it 1026 become 0, why?
nothing in the loop would ever change LogicVal
I understand the evaluation (**(1)) should be sufficient since after completion of the while loop it will be re-evaluated in the outer (;;) forever loop,
am I missing something?
It will never leave the while loop so the outer loop will never re-execute

Q.2: If bit 1026=1 then immediately 1026=0 during execution of the loop, the loop does not end after completion of current 4 iterations but makes another round of 4 iterations! why?
I understand that 1026 status is re-evaluated after the completion of the 4 iterations of the while loop not before.
The logic goes that if the motor is still oscillating then the loop is still running then 1026 is not evaluated, or the program execution is not like this?
Appreciate clarifications.
After the moves complete even though bit 1026 might be clear LogicVal is still set. So an entire loop will again be required before LogicVal will be cleared.

You might pretend to be the computer and yourself execute the program line-by-line and observe when LogicVal is set and when LogicVal is tested.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

smartmc
Posts: 7
Joined: Sat Jan 16, 2021 12:59 am

Re: Logic condition evluation timing & While loop

Post by smartmc » Tue Jul 12, 2022 10:05 pm

Thanks Tom,
I understand that nothing in the loop changes the condition.
What I understood from your answer is that when the for ( i=...) iteration is done, execution goes to the while condition check and NOT to the begining of the program at the for (;;).
This is what you mean?
Not like a PLC where at each scan the whole function is executed from start to end.

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Logic condition evluation timing & While loop

Post by TomKerekes » Tue Jul 12, 2022 10:16 pm

Yes that is correct. Not like ladder logic.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply