CS/CE 218 Lecture -*- Outline -*- connection: still on details of C, now we'll look at yet another ADT the program counter! The operations on the program counter are better known as statements... * control flow in C ** statement syntax (section 3.1) Q: How are semi-colons used differently in C than in Pascal? semi-colon is a terminator, not a separator as in Pascal *** blocks (section 3.1) Q: What's the equivalent of Pascal's begin-end in C? { } takes role of begin-end but no semi-colon after } so begin s1; s2 end; s3 becomes { s1; s2; } s3 Another difference from Pascal: blocks can be used to localize declarations ------------- { double z; z = f(x,23.0 + 9.3*y); x = z - 42.56; } { int z; for (z = 0; z < MAX; z++) { /* ... */ } } ------------- ** conditionals *** if-else, else-if (sections 3.2 and 3.3) Q: Is the else part of an if statement necessary? Q: What does the condition of an if statement test? if the expression is not 0 Q: When should you use brackets for the body of an if? I use them all the time; might need to add more statements makes syntax I have to remember simpler avoids ambiguity But I violate this rule if the whole if goes on one line. *** switch (section 3.4) like Pascal's case statement in many respects, but different in others. Q: What is a constant expression? --------------- enum {even=0, odd=1}; switch (n % 2) { case even : n = n / 2; break; case odd : n = (3*n + 1) / 2; break; } --------------- Q: What is break used for in a switch statement? example: translation of phone numbers in letters ---------------- switch (c) { case 'a': case 'b': case 'c' : putchar('1'); break; /*...*/ case 'w': case 'x': case 'y' : putchar('9'); break; case 'q': case 'z': fprintf(stderr, "%c is illegal\n", c); exit(1); break; default: putchar(c); break; } ---------------- Q: Is the break needed after the last case? no, but it's a good idea It's terribly tricky to fall through after executing some code... should be loudly commented ** loops *** while and for loops (section 3.5) Q: The semantics of a for loop are explained on p. 60 using a translation to a while loop. Is there any difference between the translation and the original? Yes, behavior of continue continue in a for goes to the increment step, not to the loop head Can leave out parts of the for for (; i<20; i++) stmt for (;;) stmt is an infinite loop Q: What initialization should go in the for loop? only those related directly to the loop itself. bad example: for (x=20, y=30, i=0; i int atoi(const char s[]) /* ensures result is the number represented by s */ { int i, n, sign; for (i = 0; isspace(s[i]); i++) { ; } sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') { i++; } for (n = 0; isdigit(s[i]); i++) { n = 10 * n + (s[i] - '0'); } return sign * n; } ------------------ note: leading white space allowed, also s itself is left alone *** do-while (section 3.6) Q: How is do-while like repeat-until in Pascal? How is it different? similar to repeat-until in Pascal, but sense is different ---------------- int pathlen(int n) /* requires: n is odd and n>1 */ /* ensures: iterate(T, result, n)==1 */ { int count = 0; do { n = T(n); count++; } while (n > 1); /* n == 1 */ return count; } ---------------- Q: Can you do one or more of exercises 3-4, 3-5, and 3-6 on page 64? *** break and continue (section 3-7) Q: Is there any difference between break and continue in C and in the Bourne shell? not much: break also works on switch and do break exits the smallest enclosing loop ---------------- for (i = 0; i < MAX_X; i++) { for (j = 0; j < MAX_Y; j++) { /* ... */ break; /* ... */ } some_stuff(i); } ---------------- Q: What does a continue inside switch mean? next iteration of smallest enclosing while or for. ** comma operator (pages 62-63) The comma operator allows an expression to be broken up into parts and still have a value ------------------- for (i=0, j=0; i