
////////////////////////////////////////////////////////////////////////
//                          Repetition (again)                        // 
////////////////////////////////////////////////////////////////////////

while (1) {}		// infinite loop - code just "hangs" here
while (1) ;			// same thing
while (0) ;			// This code does nothing

                                    // Assume button on bit 2 of PORTC is 0 when pressed
while ((PINC & 0x04) != 0x00) {}   // Wait for button to get pushed

// A much more readable way - use #defines at the top of your code
#define BUTTON (PINC & 0x04)   // parenthesis are needed
#define PUSHED 0x00
while (BUTTON != PUSHED) {}     // Waits until the button is pushed

// What if button is wired differently? I.e., this bit is 1 when pressed

while ((PINC & 0x04) != 0x04) {}   // Works, but we have to be careful we test against 0x04

                                // This is maybe cleaner
#define BUTTON (~PINC & 0x04)   // parenthesis are needed. Note addition of tilde
#define PUSHED 0x00
while (BUTTON != PUSHED) {}     // Waits until the button is pushed


x = 0;				// print values from 0 to 6
while (x < 7) {	
	printf("%d\n", x);
	x++;
}


for (x=0; x<7; x++) {		// "for" loop version
	printf("%d\n", x);
}


// do/while is not used as often - the body of the loop is always executed at least once
// use it when the body must be done in order to do the test

do { 
	c = getchar(); 	// call function to read character
	sendchar(c);	// call function to send character 
} while ( c != EOF );  	// Note ending ";"
						// EOF is an "end of file" character

Note:

while (x == 4) ...
for ( ; x==4 ; ) ...   // This is the same (but while is prefered)


*************** More on for loops ***************************

for (i=1; i<=10; i++) {		// Could start counting at 1, but this is not common
    dosomething();
}

for (i=0; i<10; i++) {	// Much more common. Note "i<10" vs "i<=10" as above
    dosomething();
}


for (i=9; i>=0; i--) {	// Count down. NOTE: "i>=0" "i" MUST NOT BE UNSIGNED!!!
    dosomething();
}


for (i=0; i<10; i++) {	
  
    if (i==3) continue;	// Skips the remainder of the loop
    
    if (i==7) break:	// Breaks out of loop (example only -- doesn't really make sense here)
    dosomething();
}



*************** Testing bits ***************************
unsigned char x, i;
char mask = 0x01;


if (x & 0x01) {
	dosomething();
}

// Testing the lower four bits of x, right to left
// "dosomething" if a particular bit is set

mask = 0x01;			// Version 1
for (i=0; i<4; i++) {
	if (x & mask) {
		dosomething();
	}
	mask <<= 1;
}

												// Version 2
for (i=0, mask = 0x01; i<4; i++, mask<<=1) {	// Using comma operator
	if (x & mask) {
		dosomething();
	}
}

						// Version 3
for (i=0; i<4; i++) {	// Version without "mask" variable
	if (x & (0x01<<i)) {	// mask created "on the fly" from i
		dosomething();
	}
}

								// Version 4
for (mask = 0x01; mask != 0b00010000; mask<<=1) {	// Version without "i" variable
	if (x & mask) {									// "mask" is the loop control variable
		dosomething();
	}
}





















