DOC PREVIEW
UCSC CMPE 012 - Introduction to “C” Control Loops and Functions

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to “C” Control Loops and FunctionsControl Structures“If”Example If StatementsMore If ExamplesIf’s Can Be NestedGenerating Code for If Statement“If-else”Generating Code for If-ElseMatching Else with IfChaining If’s and Else’s“While”Generating Code for WhileInfinite Loops“For”Generating Code for ForExample For LoopsNested LoopsAnother Nested LoopFor vs. While“Do-While”“Switch”Switch ExampleMore About SwitchFunctionExample of High-Level StructureFunctions in CFunction DefinitionWhy Declaration?ExampleImplementing Functions: OverviewRun-Time StackSlide 33Activation RecordActivation Record BookkeepingExample Function CallCalling the FunctionANSI C SummaryQuestions?Slide 401Introduction to “C”Introduction to “C”Control Loops and Control Loops and Functions Functions Patt and Patel Ch. 13 & 14Patt and Patel Ch. 13 & 142Control StructuresControl StructuresConditionalConditional–making a decision about which code to execute, based on evaluated expression–if–if-else–switchIterationIteration–executing code multiple times, ending based on evaluated expression–while–for–do-while3““If”If”if (condition)if (condition) action; action;conditionactionTFCondition is a C expression, which evaluates to TRUE (non-zero) or FALSE (zero).Action is a C statement, which may be simple or compound (a block).4Example If StatementsExample If Statementsif (x <= 10)if (x <= 10) y = x * x + 5; y = x * x + 5;if (x <= 10) {if (x <= 10) { y = x * x + 5; y = x * x + 5; z = (2 * y) / 3; z = (2 * y) / 3;}}if (x <= 10)if (x <= 10) y = x * x + 5; y = x * x + 5; z = (2 * y) / 3;z = (2 * y) / 3;compound statement;both executed if x <= 10only first statement is conditional; second statement is always executed5More If ExamplesMore If Examplesif (0 <= age && age <= 11)if (0 <= age && age <= 11) kids += 1; kids += 1;if (month == 4 || month == 6 ||if (month == 4 || month == 6 || month == 9 || month == 11) month == 9 || month == 11) printf(“The month has 30 days.\n”); printf(“The month has 30 days.\n”);if (x = 2)if (x = 2) y = 5; y = 5;This is a common programming error (= instead of ==), This is a common programming error (= instead of ==), not caught by compiler because it’s syntactically correct.not caught by compiler because it’s syntactically correct.always true, so action is always executed!6If’s Can Be NestedIf’s Can Be Nestedif (x == 3) if (y != 6) { z = z + 1; w = w + 2; }if ((x == 3) && (y != 6)) { z = z + 1; w = w + 2;}is the same as...7Generating Code for If StatementGenerating Code for If Statement; if (x == 2) y = 5;; if (x == 2) y = 5; LDR R0, R5, #0 LDR R0, R5, #0 ; load x into R0; load x into R0 ADD R0, R0, #-2 ADD R0, R0, #-2 ; subtract 2; subtract 2 BRnp NOT_TRUE BRnp NOT_TRUE ; if non-zero, x is not 2; if non-zero, x is not 2 AND R1, R1, #0 AND R1, R1, #0 ; store 5 to y; store 5 to y ADD R1, R1, #5 ADD R1, R1, #5 STR R1, R5, #-1 STR R1, R5, #-1NOT_TRUE ... NOT_TRUE ... ; next statement; next statement8““If-else”If-else”if (condition)if (condition) action_if; action_if;elseelse action_else; action_else;conditionaction_if action_elseT FElse allows choice between two mutually exclusive actions without re-testing condition.9Generating Code for If-ElseGenerating Code for If-Elseif (x) {if (x) { y++; y++; z--; z--;}}else {else { y--; y--; z++; z++;}} LDR R0, R5, #0 BRz ELSE ; x is not zero LDR R1, R5, #-1 ; incr y ADD R1, R1, #1 STR R1, R5, #-1 LDR R1, R5, #-2 ; decr z ADD R1, R1, #1 STR R1, R5, #-2 JMP DONE ; skip else code ; x is zeroELSE LDR R1, R5, #-1 ; decr y ADD R1, R1, #-1 STR R1, R5, #-1 LDR R1, R5, #-2 ; incr z ADD R1, R1, #1 STR R1, R5, #-2DONE ... ; next statement10Matching Else with IfMatching Else with IfElse is always Else is always associated with associated with closestclosest unassociated if.unassociated if.if (x != 10) if (y > 3) z = z / 2; else z = z * 2;if (x != 10) { if (y > 3) z = z / 2; else z = z * 2;}is the same as...if (x != 10) { if (y > 3) z = z / 2;}else z = z * 2;is NOT the same as...11Chaining If’s and Else’sChaining If’s and Else’sif (month == 4 || month == 6 || month == 9 || if (month == 4 || month == 6 || month == 9 || month == 11) month == 11) printf(“Month has 30 days.\n”); printf(“Month has 30 days.\n”);else if (month == 1 || month == 3 ||else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 5 || month == 7 || month == 8 || month == 10 || month == 8 || month == 10 || month == 12) month == 12) printf(“Month has 31 days.\n”); printf(“Month has 31 days.\n”);else if (month == 2)else if (month == 2) printf(“Month has 28 or 29 days.\n”);printf(“Month has 28 or 29 days.\n”);elseelse printf(“Don’t know that month.\n”);printf(“Don’t know that month.\n”);12““While”While”while (test)while (test) loop_body; loop_body;testloop_bodyTFExecutes loop body as long as test evaluates to TRUE (non-zero).Note: Test is evaluated before executing loop body.13Generating Code for WhileGenerating Code for Whilex = 0;while (x < 10) { printf(“%d ”, x); x = x + 1;} AND R0, R0, #0 STR R0, R5, #0 ; x = 0 ; testLOOP LDR R0, R5, #0 ; load x ADD R0, R0, #-10 BRzp DONE ; loop body LDR R0, R5, #0 ; load x ... <printf> ... ADD R0, R0, #1 ; incr x STR R0, R5, #0 JMP LOOP ; test againDONE ; next statement14Infinite LoopsInfinite LoopsThe following loop will never terminate:The following loop will never terminate:x = 0;x = 0;while (x < 10)while (x < 10) printf(“%d ”, x); printf(“%d ”, x);Loop body does not change condition, so test never Loop body does not change condition, so test never fails.fails.This is a common programming error that can be This is a common programming error that can be difficult to find.difficult to find.15““For”For”for (init; end-test; re-init)for (init; end-test; re-init) statement statementinittestloop_bodyre-initFTExecutes loop body as long as test evaluates to TRUE (non-zero).Initialization and re-initialization code includedin loop statement.Note: Test is evaluated before executing loop body.16Generating Code for ForGenerating Code for Forfor (i = 0; i < 10; i++)


View Full Document

UCSC CMPE 012 - Introduction to “C” Control Loops and Functions

Download Introduction to “C” Control Loops and Functions
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 Introduction to “C” Control Loops and Functions 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 Introduction to “C” Control Loops and Functions 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?