Unformatted text preview:

1CMSC 212 – S05 (lect 4)Announcementsz Program #1A– due Thursdayz Reading– Chapter 3 & 7 (skip 7.6)– Chapter 10 (Thursday, skip 10.2)2CMSC 212 – S05 (lect 4)Storage Class and Linkage Exampleextern int b;static int c;int func1(int e){static int longLived;int funcLifetime;func2(42);}3CMSC 212 – S05 (lect 4)Statementsz Assignment is just an expression:– Legal statements:•X = 3;• Y = 5 + (x = 3);•Y + 3;•x == y;z If Statementif (expression) {statement} else {statement}– () are required – Good style is to Always use { } around statements in an if– Expression is of type int • any non-zero means the true statement is evaluated4CMSC 212 – S05 (lect 4)Examples of If Statementsif (x > 43) {printf(“X is bigger than 43\n”);}if (!x) {printf(“x is zero”);}if (x == y) {x = 42;} else {x = y;}if (x > 43) {printf(“X is bigger than 43\n”);} else if (x > 10) {printf(“X greater than 10\n”);} else {printf(“X is 10 or less\n”);}5CMSC 212 – S05 (lect 4)While Statementwhile (expression) {body}z Similar to while in Javaz Expression is (like if) an integer expression– Loop terminates when the expression is 0z Forced loop termination– break – exit one innermost loop level – break does not take a label like Javaz Forced next iteration– continue – return to loop head and evaluate expression• Also applies only to inner most loop6CMSC 212 – S05 (lect 4)Examples of While Loopswhile (x < y) {….}while (x < y && !error) {...}while (!done) {while (x < y) {…}}7CMSC 212 – S05 (lect 4)More Examples of While Loopwhile (!done) {...if (a == b) {done = 1;}…}while (1) {...if (a == b) {break;}…}8CMSC 212 – S05 (lect 4)For Statementz for (expression1; expression2; expression3) …– Expression1• Run once before first iteration• Used to initialize values– Expression2 • Evaluated before each loop iteration• Controls loop execution, when it’s 0 loop stops– Expression3• Run after each iteration– Can omit any expression– Loop body may contain break or continue like while loops9CMSC 212 – S05 (lect 4)Examples of For Loopfor (i=0; i < 10; i++) {a[i] = 0;}for (;;) {}for (i=0, b=21; i < 10; i++) {a[i] = b + i;}10CMSC 212 – S05 (lect 4)Do Statementdo {statement} while (expression);z Like a while, but the body always runsz Rarely used11CMSC 212 – S05 (lect 4)Switch Statementswitch (expression) {case constant-expression:statement;break;….default:statement;break;}zExecution starts at the constant-expression equal to the expression– Without the break, will continue to next statement!!!z Default constant expression matches anything else12CMSC 212 – S05 (lect 4)Example of Switch Statementswitch (command) {case 'A':add_entry();break;case 'D':delete_entry();break;case 'P':print_entry();break;case 'E':edit_entry();break;default:printf("Error");break;}13CMSC 212 – S05 (lect 4)Functionsz type name (parameters) block– type: return type for the function– name: name of the function– parameters: a comma separated list of types and names– block: the code to runz return statement: return expression;– terminates a function at that point– expression is the value returned by the function14CMSC 212 – S05 (lect 4)Function Exampleint find(int data[10], int value) {for (i=0; i < 10; i++) {if (data[i] == value) {return i;}}}15CMSC 212 – S05 (lect 4)Function Argumentsz Comma separated list of valuesz All parameters are passed by value– called function can modify values– arrays are passed by the address of their 0th element• modifying elements of array seen by calling functionz Can pass address of variable to change valuesint foo(int *a) {*a = 3;}int x;foo(&x);16CMSC 212 – S05 (lect 4)Recursive Functionsz Functions that call themselves – can be indirect: a calls b calls az Example:void binary_to_ascii(unsigned int value) {unsigned int quotient;quotient = value / 10;if (quotient != 0) {binary_to_ascii(quotient);}printf("%d", value % 10 + '0');}17CMSC 212 – S05 (lect 4)Abstract Data Typesz Key Idea: Hide implementation from users– lets implementation evolve without changing use– makes it easier to understand code• simply know what a function should do not howz Static keyword helps with this– hides global variables from other modules– hides functions that should not be called from other


View Full Document

UMD CMSC 212 - CMSC 212 Lecture 4

Download CMSC 212 Lecture 4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CMSC 212 Lecture 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CMSC 212 Lecture 4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?