homework

18
Homework • Any Questions?

Upload: ferris-copeland

Post on 30-Dec-2015

26 views

Category:

Documents


0 download

DESCRIPTION

Homework. Any Questions?. Statements / Blocks, Section 3.1. An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and statements into a compound statement { x = 0; y = 1; }/* Note: No semicolon after right brace */. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Homework

Homework

• Any Questions?

Page 2: Homework

Statements / Blocks, Section 3.1• An expression becomes a statement when it is followed

by a semicolon

x = 0;• Braces are used to group declarations and statements

into a compound statement

{

x = 0;

y = 1;

} /* Note: No semicolon after right brace */

Page 3: Homework

if statements, Section 3.2

• Shortcut for “equal and not equal to 0” tests

• Can use:

if (expression) if (!expression)

• In place of

if (expression != 0) if (expression == 0)

Page 4: Homework

Else-if, Section 3.3

• Consider cascading else-if sequence:

if (i == 1) /* NOTE: Only one will execute */ statement-1;else if (i == 2) statement-2;

. . . else if (i == 49) statement-49; else statement-50; /* Default or "catch-all" */

Page 5: Homework

Switch, Section 3.4• Also have switch statement **LIKE JAVA**

switch (i) { /* in special limited situations */ case 1: statement-1; break; case 2: statement-2; break; . . . case 49: statement-49; break; default: statement-50; }

Page 6: Homework

Switch• The way the if-else cascade works is to test

– Test if i == 1– If that fails, test if i == 2. If that fails, test if i == 3. . . . – When we test i == 27, we have done 26 prior tests.

• With the switch statement, we achieve the same effect, but probably faster: – Usually compiled into assembly language as a jump table– Array of "go to" instructions subscripted by the value of i – If i = 27 we "look up" the “go to” at address 27 in table– And execute only that one “go to”– Note the need for break statements. The default action is

to cascade down to the code for the next case!!

Page 7: Homework

Loops –while and for, Section 3.5

• This “for” statement”for (expr1; expr2; expr3)

statement;• Is equivalent to this “while” statement:

expr1;while (expr2) {

statement;expr3;

}

Page 8: Homework

For loop• Note any part of for loop can be left out.

for (init; loop-test; increment)• If init or increment expression is left out,

just not evaluated (program must initialize and increment by other means)

• If loop-test is left out, assumes permanently true condition and loops forever. (program must break or goto to end to exit the loop)

Page 9: Homework

Comma operator: ","

• Most often used in for loop statement

• Might have been useful for reverse () in hw1

for (i = 0, j = strlen(s) - 1; i < j; i++, j--)

• Pairs of expressions separated by "," are evaluated left-to-right and type/value of expression is type/value of result.

• See K & R, pg 53, Precedence Table

Page 10: Homework

Do … While, Section 3.6

• The do … while tests at the end of the loop

do {

statement(s);

} while (expression);

• Executes the statement(s) once even if the “while” loop expression is false upon entry

• Used much less often than “for” and “while”

Page 11: Homework

Break and Continue, Sections 3.7

• The break statement works for: – for loop / while loop / do loop and switch. – Brings you to end of loop or switch statement

ONE LEVEL ONLY.

• The continue statement works for:– for loop / while loop / do loop, but not switch!– It causes next iteration of enclosing loop to begin

Page 12: Homework

GOTO, and Labels, Section 3.8

• The goto statement breaks TWO OR MORE levels

• It “goes to” a statement with a label, e.g. ret:

• It may be OK to use goto if you always do it in a FORWARD direction. (See axtoi, if use a goto ret; then don't need flag or test in “for” loop.)

• K&R, pg 66: “goto should be used rarely, if at all.”

• Note: Project coding standards may prohibit “goto”!

Page 13: Homework

Binary Search

/* binsearch:find x in v[0] <= v[1] <= . . <= v[n-1]returns subscript of x if found, -1 if not

*/int binsearch ( int x, int v[ ], int n) {

int low, high, mid; low = 0;high = n - 1;

Page 14: Homework

Binary Search

while ( low <= high) {mid = (low + high)/2;if (x < v[mid])

high = mid - 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid;

}return -1; /* no match */

}

Page 15: Homework

Functions / Program Structure

• C makes functions efficient and easy to use

• Program != a few big functions

• Program = = many small functions

• Functions may reside in separate source files

• Source files compiled and loaded separately

• An Introduction to MAKEFILE is coming (A process that K&R does not go into)

Page 16: Homework

Function Prototypes

• Return type, function name, and ( )int foo ( );float foo ( );

• Argument ListList of Types and optionally Variable Names

int foo (int *, int, float);int foo (int array[], int i, float j);

Output arguments usually listed first

Page 17: Homework

Function Prototypes

• Special case for null argument lists• Always code function prototype as:

int foo (void);

• Keeps compiler parameter checking enabled• Demonstration of bad example:

int foo ();

x = foo (i, j, k); /* compiler won’t catch! */

Page 18: Homework

Function Declarations

• Same as function prototype, except:– Must have variable names in argument list– Followed by { function statements; } not ;

• Example:int foo (int array[], int i, float j){

function statements;}