Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014

2 Syllabus Data Types Array Function Expressions Caveat, and PL Politics Assignment Statement If Statement While Statement Do Statement Call

3 2 Data Type Purpose: Type defines a range of values, identified by so called “type name”. C is loosely typed, as opposed to strictly Type used to define data in variables, constants, functions Sample: int i;// type name int; e.g. values -1, 0 1, … char c1, c2;// type name char; e.g. c2 may be ‘x’, ‘y’ … float f = 3.14;// float -> single-precision floating point int min( int a, int a); // function returns an int value Meaning: Type classifies an object to hold those and only those values associated with the type Caveat: some type names are predefined; language allows user defined type names via typedef

4 3 Array Purpose: A C array is a collection of values all of identical type, all known by one name, each separate value identifiable via index, expressed as subscript[] Sample: int prime[ 10 ] = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 }; int x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; x[ 3 ] = prime[ 2 ]; Meaning: used to process different objects with similar or identical properties, whose only difference is their respective index Caveats: the first index (AKA low-bound) is always 0 in C; index range of N elements is [0..N-1]. C does not check index violations!

5 4 Array C is row major programming language, 2-D sample: #include #define MAX_ROW 3// 3 rows #define MAX_COL 4// 4 columns, each row #define BPW 4// different 4 from MAX_COL! int main() { // main int a[ MAX_ROW ][ MAX_COL ]; int row, col; for( row = 0; row < MAX_ROW; row++ ) { for ( col = 0; col < MAX_COL; col++ ) { a[ row ][ col ] = row * BPW + col; } //end for printf( "a[ 2 ][ 2 ] should be 2*4+2 = 10, and is %d\n", a[ 2 ][ 2 ] ); return 0; } /end main

6 5 Structs Purpose: is a collection of values of arbitrary types, all collectively known by one name, but each value is identifiable via a unique id, called the field name Not handled in ECE 103

7 6 Function Purpose: A function is a key part of a C program constituting a separate logical module, known by a name –the function name-- that can be called Function may have parameters: formal parameters at declaration, actual parameters at place of call Sample: int min( int a, int b )// 2 formal parameters { // min return a < b ? a : b; } //end min Meaning: allow logical modularization of complex SW into smaller, manageable parts Caveat: functions cannot be statically nested inside one another; functions are recursive

8 7 Expressions Purpose: An expression consists of operands, operators, and parentheses yielding a resulting value The value is of one of the allowable scalar types Sample: 3.14159265358979323// float value, constant ‘x’// char value, constant b >= c// boolean based on variables Meaning: expression yields one value of some type Caveat: operands may have different types; use explicit type cast to convert

9 8 Caveat, and PL Politics Given its early phase in the evolution of machine- independent programming languages, C is a shining maverick C is characterized as “an expression language” What other languages view as statements, C treats as an expressions; but that is language design politics, and we shall do as if C had statements, which are those syntactic constructs that perform computations, expressed in actions But remember, an “Assignment Statement” for example is just “an expression” for true C apostles

10 9 Assignment Statement Purpose: moves value of an expression to the right of the assignment operator = into a variable to the left Sample: min = ( a < b ) ? a : b;// conditional expr. Meaning: by moving values into addressable variables, assignments preserve values for arbitrary re-use during future execution Caveat: As an “assignment statement” (misnomer) is an expression, parts of the same expression can be further assignments; careful about evaluation order: a[ i++ ] = a[ a[ ++i ]++ ] = a[ i++ ]++;

11 10 If Statement Purpose: If statement is controlled by a boolean condition which allows selective execution of one or two alternatives Sample: if ( a < b ) {// { to allow multiple min = a;// statement 1, only 1 for then }else{ min = b;// statement 2, only 1 in else } //end if Meaning: selects statement 1 for execution, and only that one, if a is smaller than b Else If Statement picks statement 2 Caveat: the { } is not necessary; thus a single statements is allowed for the then- and else-clause, creating hard to read programs

12 11 While Statement Purpose: allows repeated execution of 0 or more times of the enclosed list of statements, based on a boolean condition; evaluated 0 or not-0; 0 == false Sample: i = 0; while ( i < 10 ) {// bad to use magic # process_element( i++ ); } //end while; Meaning: continue calling process_element() as long as i is less than 10 Caveat: the while body must provide a way for the boolean condition to yield false eventually

13 12 Do Statement Purpose: allows repeated execution of 1 or more times of the enclosed list of statements, based on a boolean condition Sample: i = 0; do { process_element( i++ ); } while( i < 10 ); //end do; // bad to use “10” Meaning: continue calling process_element() as long as i is less than 10; body is executed for sure once! Caveat: the while body must provide a way for the boolean condition to yield false eventually

14 13 Call Statement Purpose: change the sequential flow of control by activating the called function, execute its body, until the function causes a return to place after call Sample: i = 1; process_element( i++ );// meaningful ++ ? i = 0; Meaning: after setting i to 1, control transfers to the function body of process_element() and returns from there to i = 0; Caveat: calls can be dynamically nested, making the manual tracing of dynamically executing code hard Caveat: Calls can be statically nested: foo( bar( foo_bar() + 5 ), bar_foo() )


Download ppt "ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014."

Similar presentations


Ads by Google