Logic condition evluation timing & While loop
Posted: 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);
}
}
-----------------------------------------------------------
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);
}
}
-----------------------------------------------------------