❖ COMP1511 Style |
Required use of a restricted subset of C:
if
while
for
❖ COMP2521 Style |
Extends the range of allowed constructs:
❖ Poor Style |
Examples of poor style:
int fff(int n) { int flab = 1; if (n < 1) return -1; for (int z = 1; z <= n; z++) flab = flab * z; return flab; } int ff(int n) { int f = 1; if (n < 1) return -1; for (int xy = 1; xy <= n; xy++) f *= xy; return f; }
❖ Comments |
COMP1511 used (exclusively?) /*...*/
Many books, code-bases use //...
Either is ok, but prefer
//
int nc; // count of characters
/*...*/
#...
#
#include
❖ Use of Brackets |
Put control-group start bracket after conditional expression
Can omit brackets if control structure owns a single statement
Examples:
if (x > 0) { | while (*c != '\0') {
y = y * x; | c++;
} | }
|
or
|
if (x > 0) | while (*c != '\0')
y = y * x; | c++;
|
or even (slightly naughty)
|
if (x > 0) y *= x; | while (*c != '\0') c++;
❖ ... Use of Brackets |
If condition followed by return
continue
break
// handle incorrect parameter if (x < 0) return -1; // early exit from loop for (c = str; *c != '\0'; c++) { if (*c == 'z') break; ... process next char in string ... } // ignore spaces in string for (c = str; *c != '\0'; c++) { if (isspace(*c)) continue; ... process non-space char ... }
❖ ... Use of Brackets |
Can put function start bracket on line after function header, e.g.
int myFun(parameters) {
... function body ...
}
or
int myFun(parameters)
{
... function body ...
}
or
int
myFun(parameters) { // name at start of line
... function body ...
}
❖ Assignment in Expressions |
Can use assignment statements in expressions, e.g.
// assign same value to multiple variables i = j = k = 0; or i = (j = (k = 0)); or k = 0; j = 0; i = 0; // scan stdin, char-by-char while ((ch = getchar()) != EOF) { ...process next char... }
but you should try to minimse their use in this way
❖ Conditional Expressions |
Conditional expressions return a value, based on a test
Handle a moderately common practical case:
if (x > 0) y = x + 1; else y = 0; can be expressed as y = (x > 0) ? x+1 : 0;
Requires: same variable in both if
❖ Control Structures |
Can use more C control structures
if
switch
while
do
for
break
continue
goto
setjmp()
longjmp()
ch = getchar(); while (ch != EOF) { if (isalpha(ch)) nalpha++; ch = getchar(); } or do { ch = getchar(); if (isalpha(ch)) nalpha++; } while (ch != EOF); or while ((ch = getchar()) != EOF) { if (isalpha(ch)) nalpha++; }
❖ Switch-statements |
switch
if (v == C1) { S1; } else if (v == C2) { S2; } ... else if (v == Cn) { Sn; } else { Sn+1; }
❖ ... Switch-statements |
Multi-way if
switch (v) { case C1: S1; break; case C2: S2; break; ... case Cn: Sn; break; default: Sn+1; }
Note: break
❖ ... Switch-statements |
Example of "fall-through" (when break
switch (ch) {
case 'a': printf("a\n");
case 'b': printf("b\n"); break;
case 'c': printf("c\n"); break;
case 'd': printf("d\n");
default: printf("?"); // break optional here
}
ch == 'a'
'a'
'b'
ch == 'b'
'b'
ch == 'c'
'c'
ch == 'd'
'd'
'?'
❖ For-loops |
for
initialise; while (Continuation) { do stuff; increment; }
as
for (initialise; Continuation; increment) { do stuff; }
❖ break continue |
These constructs affect how a loop operates, e.g.
while (Continuation) { ... do stuff1 ... if (Test1) continue; ... do stuff2 ... if (Test2) break; ... do stuff3 ... }
stuff1
Test1
Continuation
Test1
stuff2
Test2
Test2
stuff3
❖ Functions and return |
COMP1511 and "proper" style suggest that ...
return
return
❖ ... Functions and return |
Example: compute n!
-1
int factorial(int n) { int fac = 1; if (n < 1) return -1; // error return for (int i = 1; i <= n; i++) { fac = fac * i; } return fac; // return result } int factorial(int n) { if (n < 1) return -1; else if (n == 1) return 1; else return n * factorial(n-1); }
❖ ... Functions and return |
Example:
search for key
a[]
n
int search(int key, int a[], int n) { int where = -1; // not found value for (int i = 0; i < n; i++) { if (a[i] == key) where = i; } return where; // return result or not found } or int search(int key, int a[], int n) { for (int i = 0; i < n; i++) { if (a[i] == key) return i; // return result } return -1; // not found value }
❖ Relaxed Style |
Good: gives you more freedom and power
Produced: 1 Jun 2020