190 likes | 373 Views
‘C’ in a Nutshell. A “crash course” in C... ...with designs for embedded systems by J. S. Sumey. II. Program Flow Control. - specifies order in which program computations are performed. Control Flow Constructs. statements & blocks decision: if-else selection: switch
E N D
‘C’ in a Nutshell A “crash course” in C... ...with designs for embedded systems by J. S. Sumey
II. Program Flow Control - specifies order in which program computations are performed
Control Flow Constructs • statements & blocks • decision: if-else • selection: switch • looping: do-while, while, for • loop control: break, continue • labels and goto 'C' in a Nutshell by J. Sumey
Statements & blocks • statements are terminated with a semicolon • ex: x = 0; printf(...); • blocks are groups of statements bounded by braces • ex: { int i; for (i = 0; i < 10; i++) ... } • a block is valid anywhere a single statement is valid, useful for ‘very’ local variables 'C' in a Nutshell by J. Sumey
Decision: 1- or 2-way • if-else used for conditional execution • ex: if (expr)stmt1elsestmt2 • executes stmt1 if expr is true (non-zero), stmt2 if expression is false (equal to 0) • ‘else stmt2’ is optional • nested ifs: else is always paired with most recent elseless-if; use braces to override this! 'C' in a Nutshell by J. Sumey
if-elses may be cascaded for multi-way decisions first true expr causes its stmt to execute and terminate whole chain optional final else part handles “none of the above” (default) case ex:if (expr1)stmt1else if (expr2)stmt2else if (expr3)stmt3...elsestmt0 Decision: multi-way 'C' in a Nutshell by J. Sumey
multi-way decision based on a single expression against a number of constant values matching const value causes execution to start at that stmt use break within each stmt group to prevent “fall-thru” optional default: part executes if no match is found ex:switch (expr){ caseconst1: stmts1caseconst2: stmts2...default: stmts0} Selection 'C' in a Nutshell by J. Sumey
repeats a statement or block until expression is no longer true loop body always executes at least once(posttest loop) equivalent to a “repeat-until” construct of other languages ex:dostmtwhile (expr); or:do {stmt1 stmt2 ...} while (expr); Looping: do-while loop 'C' in a Nutshell by J. Sumey
repeats a statement or block as long as the expression is true loop body may execute zero times!(pretest loop) more commonly used than do-while ex:while (expr)stmt or:while (expr){stmt1 stmt2 ...} Looping: while loop 'C' in a Nutshell by J. Sumey
is a fancy while-do typically used when number of iterations is known in advance expr1 typically used as initializer, expr2 for test condition, expr3 for loop maintenance expressions may be “complex”; are also optional ex:for (expr1; expr2; expr3)stmt same as:expr1;while (expr2){stmt expr3;} Looping: for loop update part terminal test initializer 'C' in a Nutshell by J. Sumey
Example: for loop • display the numbers from 1 to 49 with their “mirrors” about 50 in two columns; also compute & display their sum int i, sum;for (sum = 0, i = 1; i < 50; sum += i++) printf( “%i %i\n”, i, 100-i );printf( “sum = %i\n”, sum ); 'C' in a Nutshell by J. Sumey
causes early exit from a loop or switch useful for detecting abnormal conditions in loops or to prevent “fall-thru” in switch blocks ex:switch (result){ case -1: do_neg(); break; case 0: do_zero(); break; case +1: do_pos();} Loop control: break 'C' in a Nutshell by J. Sumey
forces next iteration of a for, while or do-while loop useful for bypassing a ‘complicated’ loop ex: find the sum of the positive elements of a 10-element array ex:sum=0;for (i=0; i<10; i++){ if (a[i] < 0) continue; sum += a[i];} Loop control: continue 'C' in a Nutshell by J. Sumey
typically considered “bad practice” and is avoided however, useful in the main processing loop of embedded systems syntax: gotolabel; label names follow same rules as variables, followed by colon ex:main(){ MainLoop: read_sensors(); do_calculations(); control_outputs(); goto MainLoop;} Labels & goto 'C' in a Nutshell by J. Sumey
III. Functions - allow large, complicated problems to be broken down into smaller, simpler pieces
Function basics • C programs are typically composed of many small, well-defined functions • a typical program is simply a set of related variable and function definitions • one ‘special’ function named main() is treated as the ‘top’ of the program • functions may accept zero or more arguments and return zero or one result value 'C' in a Nutshell by J. Sumey
Function syntax • function definition form: return-val-type function-name (argument_declarations ){ declarations and statements} • only function_name is required • ex: useless() { } 'C' in a Nutshell by J. Sumey
Return values • by default (i.e. return type not specified), a C function returns an integer • ex: int compute_average() ... • use return statement to exit function • ex: return expression; • can return other types instead • char, float, double, or pointer to a complex type • a function with no return value should be declared as returning a void type • ex: void do_calculations() ... • a function having no arguments should be declared as such (some compilers enforce this!) • ex: void do_calculations(void)… 'C' in a Nutshell by J. Sumey
Example function a function to return 1 if a specified year is a leap year, 0 if not int leapyear( int y ){ return ( y%4 == 0 && y%100 != 0 || y%400 == 0 );}main(){ int yr = 2016; printf( “%i is %sa leapyear”, yr, leapyear(yr) ? “” : “not ”);} 'C' in a Nutshell by J. Sumey