DOC PREVIEW
Penn CIS 240 - Control Structures

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Based on slides © McGraw-HillAdditional material © 2004/2005 Lewis/MartinChapter 13Control Structures58CSE 240Control Structures Conditional• Making decision about which code to execute, based onevaluated expression• if• if-else• switch Iteration• Executing code multiple times, ending based on evaluatedexpression• while• for• do-while59CSE 240If if (condition) 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).60CSE 240Example If Statements if (x <= 10) y = x * x + 5; if (x <= 10) { y = x * x + 5; z = (2 * y) / 3;}if (x <= 10) y = x * x + 5; z = (2 * y) / 3;compound statement;both executed if x <= 10only first statement is conditional;second statement isalways executedStyle: avoid singletonif statements (I really dislike them)261CSE 240More If Examples if (0 <= age && age <= 11) { kids = kids + 1; } if (month == 4 || month == 6 || month == 9 || month == 11) { printf(“The month has 30 days.\n”); } if (x = 2) { y = 5; } This is a common programming error (= instead of ==),not caught by compiler because it’s syntactically correct.Common C error, assignment (=)Versus equality (==)62CSE 240Generating Code for If Statement if (x == 2) { y = 5; } LDR R0, R6, #0 ; load x into R0 ADD R0, R0, #-2 ; subtract 2 BRnp NOT_TRUE ; if non-zero, x is not 2 AND R1, R1, #0 ; store 5 to y ADD R1, R1, #5 STR R1, R6, #1NOT_TRUE ... ; next statement63CSE 240If-else if (condition) action_if;else action_else;conditionaction_if action_elseT FElse allows choice between two mutually exclusive actions without re-testing condition. 64CSE 240Generating Code for If-Else if (x) { y++; z--; } else { y--; z++; } LDR R0, R6, #0 BRz ELSE ; x is not zero LDR R1, R6, #1 ; incr y ADD R1, R1, #1 STR R1, R6, #1 LDR R1, R6, #2 ; decr z ADD R1, R1, #-1 STR R1, R6, #2 JMP DONE ; skip else code ; x is zeroELSE LDR R1, R6, #1 ; decr y ADD R1, R1, #-1 STR R1, R6, #1 LDR R1, R6, #2 ; incr z ADD R1, R1, #1 STR R1, R6, #2DONE ... ; next statement365CSE 240Matching Else with If Else is always associated with closest unassociated ifif (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... Solution: always use braces (avoids the problem entirely)66CSE 240Chaining If’s and Else’s if (month == 4 || month == 6 || month == 9 || month == 11) { printf(“Month has 30 days.\n”);} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { printf(“Month has 31 days.\n”); } else if (month == 2) { printf(“Month has 28 or 29 days.\n”); } else { printf(“Don’t know that month.\n”); }67CSE 240While while (test) loop_body;testloop_bodyTFExecutes loop body as long as test evaluates to TRUE (non-zero)Note: Test is evaluated before executing loop body68CSE 240Generating Code for Whilex = 0;while (x < 10) { printf(“%d ”, x); x = x + 1;} AND R0, R0, #0 STR R0, R6, #0 ; x = 0 ; testLOOP LDR R0, R6, #0 ; load x ADD R0, R0, #-10 BRzp DONE ; loop body LDR R0, R6, #0 ; load x ... <printf> ... ADD R0, R0, #1 ; incr x STR R0, R6, #0 JMP LOOP ; test againDONE ; next statement469CSE 240Infinite Loops The following loop will never terminate: x = 0;while (x < 10) { printf(“%d ”, x); } Loop body does not change condition...• ...so test is never false Common programming error that can be difficult to find70CSE 240For for (init; end-test; re-init) statementinittestloop_bodyre-initFTExecutes loop body as long as test evaluates to TRUE (non-zero).Initialization and re-initialization code included in loop statement.Note: Test is evaluated before executing loop body71CSE 240Generating Code for Forfor (i = 0; i < 10; i++) { printf(“%d ”, i);} ; init AND R0, R0, #0 STR R0, R6, #0 ; i = 0 ; testLOOP LDR R0, R6, #0 ; load i ADD R0, R0, #-10 BRzp DONE ; loop body LDR R0, R6, #0 ; load i ... <printf> ... ; re-init ADD R0, R0, #1 ; incr i STR R0, R6, #0 JMP LOOP ; test againDONE ; next statementThis is the sameas the while example!72CSE 240Example For Loops /* -- what is the output of this loop? -- */ for (i = 0; i <= 10; i++) { printf("%d ", i); } /* -- what does this one output? -- */ letter = 'a'; for (c = 0; c < 26; c++) { printf("%c ", letter+c); } /* -- what does this loop do? -- */ numberOfOnes = 0; for (bitNum = 0; bitNum < 16; bitNum++) { if (inputValue & (1 << bitNum)) { numberOfOnes++; } }573CSE 240Nested Loops Loop body can (of course) be another loop /* print a multiplication table */ for (mp1 = 0; mp1 < 10; mp1++) { for (mp2 = 0; mp2 < 10; mp2++) { printf(“%d\t”, mp1*mp2); } printf(“\n”); }74CSE 240Another Nested Loop Here, test for the inner loop depends on counter variableof outer loop for (outer = 1; outer <= input; outer++) { for (inner = 0; inner < outer; inner++) { sum += inner; }}75CSE 240For vs. While In general: For loop is preferred for counter-based loops• Explicit counter variable• Easy to see how counter is modified each loop While loop is preferred for sentinel-based loops• Test checks for sentinel value. Either kind of loop can be expressed as other,so really a matter of style and readability76CSE 240Do-While do loop_body; while (test);loop_bodytestTFExecutes loop body as long astest evaluates to TRUE (non-zero).Note: Test is evaluated after executing loop body677CSE 240Break and Continue break;• used only in switch statement or iteration statement• passes control out of the “smallest” (loop or switch) statementcontaining it to the statement immediately following• usually used to exit a loop before terminating condition occurs(or to exit switch statement when case is done) continue;• used only in iteration statement• terminates the execution of the loop body for this iteration• loop expression is evaluated to see whether anotheriteration should be performed• if for loop, also executes the re-initializer78CSE 240Example What does the following loop do?for (i = 0; i <= 20; i++) { if (i%2 == 0) { continue; } printf("%d ", i);} What would be an easier way


View Full Document

Penn CIS 240 - Control Structures

Download Control Structures
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 Control Structures 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 Control Structures 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?