////////////////////////////////////////////////////////////////////////
//                             Selection                              // 
////////////////////////////////////////////////////////////////////////

if (x == 1) {			// Fully nested version
   printf ("one");
}
else {
   if (x == 2) {
      printf("two");
   }
   else {
      if (x == 3) {
         printf("three");
      }
      else {
      	printf("something else");
      }
   }
}   
 
if (x == 1) {		// Better layout - good if conditions are mutually exclusive
   printf ("one");
}
else if (x == 2) {
   printf("two");
}
else if (x == 3) {
   printf("three");
}   
else {
   printf("something else");
}

// I might accept this for compactness, but be aware of the pitfalls of omitted {}
if (x == 1)  printf ("one");
else if (x == 2) printf("two");
else if (x == 3) printf("three");
else printf("something else");


// Doing this with switch/case. When you are comparing a variable (or expression) to integer constants
// These constants must be known when you compile (not computed in the program)

switch (x) { 		// using the "switch" - the last control structure
case 1:
	printf ("one");	// can be multiple statements here -- no {} is required
	break;			// don't forget this - otherwise it "falls through" to the next case
case 2: 
	printf ("two");
	break;
case 3: 
case '3':			// multiple cases are allowed - matches either 3 or '3' (ASCII code for 3)
	printf ("three");
	break;
default:			// having a default is optional
   printf("something else");	// break is not needed on the last one
}

// Version using the ?: operator instead of a control structure
printf("%s", x==1 ? "one" : "not one");		// two selection version
printf(x==1 ? "one" : "not one");			// Also works with printf

x==1 ? printf("one") : printf("not one");  // BAD -- Do not use as a substitution for "if () {}"


// Full version - maybe a little  confusing to combine ?: operators, but it works. 
// Parenthesis on ?: parts are not required here, but do make it easier to read
printf(x==1 ? "one" : (x==2 ? "two" : (x==3 ? "three" : "something else")));


x = x<0 ? -x : x;	// Another ?: example

// Another example:
// Assume year, month and day are already calculated and month is either 3 or 4 (for "March" or "April"). 
// We assume in the following that if the year is not 3, you know it to be 4.

if (month==3) {
	printf("The date of Easter in $d is %s %d\n", year, "March", day);
}
else {
	printf("The date of Easter in $d is %s %d\n", year, "April", day);
}

// If two or more secions of your code are repeated, but with some small variation,
// think of combining them into one section that handles the differences.
//      This is usually clearer, easier to understand
//      This is easier to write and get right (don't have to make sure each version is correct)
//      This can be easier to debug (find and fix errors)
//      This is easier to maintain (to make changes as the program requirements change)

printf("The date of Easter in $d is %s %d\n", year, month==3?"March":"April", day);

// Do not use ?: as a substitute for if/else

month==3 ? printf("March") : printf("April")        // not good programming practice


////////////////////////////////////////////////////////////////////////
//                             Repetition                             // 
////////////////////////////////////////////////////////////////////////

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);
}

while (x == 4) ...
for ( ; x==4 ; ) ...   // Same

// 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









