if(Key_temp==0x48); //Up - short— ^ --- Bug code below always executes. if(Month_current==12){ Month_current=1; Update_month(); }
Never write if(); or while();
Always write if()
;
while()
;
I always write:
if()
{
if()
{
}
}
if(Key_temp==0x48) //Up - short— // if(Month_current==12){// Month_current=1;// Update_month();// }If you comment out that code, then it won't compile!!!!
With out the second if block, then you do need a ';'
BUT
if(Key_temp==0x48) //Up - short—
;
will generate NO CODE because there is nothing to do!
This will compile: if(Key_temp==0x48) //Up - short– if(Month_current==12){ Month_current=1; Update_month(); }
BETTER:
if(Key_temp==0x48) { //Up - short–
if(Month_current==12){
Month_current=1;
Update_month();
}
}
Because:
if(Key_temp==0x48) { //Up - short–
// if(Month_current==12){
// Month_current=1;
// Update_month();
// }
}
WILL compile (but still generate no code).
if(Key_temp==0x48) { // Up - short (increment month) if(Month_current==12) // if month = 12 (max) Month_current=1; // then make it a 1 else Month_current++; // otherwise, increment month Update_month(); // and go update & back to Do_month }for(;;){ char kb = readkey(); if( 0 == kb ) continue; // go back and read the key. (jmp back to top) if( 48 == kb) { // do stuff continue; // got back to the top } if(xx == kb) { // do stuff for xx continue; }} switch (kb) { case 48: // do 48 stuff break; case xx: // do xx stuff break; default: // 0 will end up here as there is nothing to do. ;}void Do_month(void){START: while (Key_temp == 0) // Wait for key press ; switch (Key_temp) { case XX:..... // All your previous cases here case 48: // Case for incrementing the month by 1 if (Month_current == 12) // If at the end of the year { Month_current = 1; // Set to the start of the year } else { Month_current++; // Otherwise increment the month by 1 } goto UPDATE_MONTH; // Now update month case XX:... // the rest of your case statements here } goto START; // If no matches go back to the start UPDATE_MONTH: // Insert your Update_month code here goto START;} // Any case that you want to exit Do_month must end with a "return;"
for(;;)
{
char kb = readkey();
if( 48 == kb)
{
/* do stuff */
}
else if(xx == kb)
{
/* do stuff for xx */
}
else
{
/* 0, do nothing */
}
}