issue with a while loop

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

The following code (see attached) using "if" to test the loop on Nb_Sync works well. If I try a "while", or a "do while", the loop does not work any more.
The if in the loop does not behave properly, it causes exit of do while!

Any Idea why ?

The following info, just in case this might have an influence:
m4 is a struct (see attached), the sequence is part of a switch, in a function part of an interrupt

Pierre

if ( m4.Nb_Sync < 11 )
{
m4.cur_bit++;
if (m4.cur_bit == m4.bits_in_state)
{
m4.cur_bit = 0;
m4.Nb_Sync++;
break;
}
else break;
}
else
{
m4.repeated++;
break;
}
=========================================
do
{
m4.cur_bit++;
if (m4.cur_bit == m4.bits_in_state)
{
m4.cur_bit = 0;
m4.Nb_Sync++;
break;
}
else break;
}
while ( m4.Nb_Sync < 11);
m4.repeated++;
break;

struct
{
enum m4_states state;
t_msg_type current_msg_type; // is_loco_m4_no_feedback, _1bit_feedback, _data_feedback
unsigned char bits_in_state; // Bits to output in this state
unsigned char cur_bit; // current bit in chain
char current_chaine[81]; // current message in output processing
unsigned char repeated;
unsigned char Nb_Sync; // number of Sync for feedback
unsigned int ocr; // M4 signal period 50uS, a dibit = 100uS
unsigned char phase;
unsigned char dibit;
} m4;

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The if in the loop does not behave properly, it causes exit of do while!
You have programmed it that way (to always exit the loop).

do {
  ...
  if (...) {
    ...
    break;
  }
  else
    break;
} while (...); 

Stefan Ernst

Last Edited: Tue. Apr 17, 2012 - 12:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if(), while(), do while() are all different constructions.

I note that you have some 'break' statements. These will 'escape' from a controlling loop.

Again the different constructs will involve different destinations for these 'escapes'.

Without knowing the full function, you could possibly have other issues with non-volatile members of a structure. (unlikely)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if ( m4.Nb_Sync < 11  )   	
{
	m4.cur_bit++;
	if (m4.cur_bit == m4.bits_in_state)
	       {
		m4.cur_bit = 0; 
		m4.Nb_Sync++;
		break;
		}
	else break;				
}	  
else
{
        m4.repeated++;   
	break;
}
=========================================
do	
{
	m4.cur_bit++;
	if (m4.cur_bit == m4.bits_in_state)
	{
		m4.cur_bit = 0; 
		m4.Nb_Sync++;
		break;
	}					  
	else break;				
}
while ( m4.Nb_Sync < 11);		  
	m4.repeated++;   
	break;	

struct
  {
    enum m4_states state;
	t_msg_type current_msg_type;     //  is_loco_m4_no_feedback, _1bit_feedback, _data_feedback
    unsigned char bits_in_state;            // Bits to output in this state
    unsigned char cur_bit;                  // current bit in chain
	char current_chaine[81];				// current message in output processing               
    unsigned char repeated;
	unsigned char Nb_Sync;          // number of Sync for feedback
	unsigned int ocr;            // M4 signal period 50uS, a dibit = 100uS
 	unsigned char phase;
 	unsigned char dibit;
  } m4;

Easier to analyze when you preserve formatting.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
You have programmed it that way (to always exit the loop).

Thanks, indeed I am fighthing with break and return, but it will come!