CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with WHILE

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Problem with WHILE
PostPosted: Fri Dec 16, 2011 8:52 am     Reply with quote

Hi, someone can tell me what is my mistake!!!, why not leave the conditional (do..while), when any of the two conditions is true ...
but now, just let this conditional only when two conditions are true ...

Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar

#define RESETX       0   
 
char Estado = RESETX;   

int8 k;
 
#define   Program      PIN_B4

void main()   
{   

while (TRUE) {   
 
   k = 1;
   Estado = 1;
   do{
      if (Estado == 1) {                       
      k++;
     } 

   } while ((input(Program)!=0)||(k < 5));
  }   
}
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: Problem with WHILE
PostPosted: Fri Dec 16, 2011 1:37 pm     Reply with quote

pilar wrote:
Hi, someone can tell me what is my mistake!!!, why not leave the conditional (do..while), when any of the two conditions is true ...
but now, just let this conditional only when two conditions are true ...

Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar

#define RESETX       0   
 
char Estado = RESETX;   

int8 k;
 
#define   Program      PIN_B4

void main()   
{   

while (TRUE) {   
 
   k = 1;
   Estado = 1;
   do{
      if (Estado == 1) {                       
      k++;
     } 

   } while ((input(Program)!=0)||(k < 5));  <<-- *** WRONG LINE, move to the brace above
  }   
}

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Fri Dec 16, 2011 3:48 pm     Reply with quote

No, it is just awfully laid out, so it _looks_ like it is on the wrong line....
Code:

void main()  {   
   while (TRUE) {   
      k = 1;
      Estado = 1;
      do {
         if (Estado == 1) {                       
            k++;
         }
      } while ((input(Program)!=0)||(k < 5));
   }   
}

However how would you ever know it had left the do...while?. As soon as it leaves, it'll execute the outer while loop, and start again.

Big question though is do you really want to use 'OR'?. The loop will be done, while pin B4 is high _OR_ k is less than 5. So the 'exit condition', is B4 being low _AND_ k>=5.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group