tailieunhanh - Lecture Introduction to computing systems (2/e): Chapter 13 - Yale N. Patt, Sanjay J. Patel
Chapter 13 - Control structures. The main contents of this chapter include all of the following: Conditional constructs, iteration constructs, problem solving using control structures, additional c control structures. | Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending based on evaluated expression while for do-while 13- If if (condition) action; condition action T F Condition 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). 13- Example If Statements if (x 13- More If Examples if (0 13- If’s Can Be Nested if (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. 13- Generating Code for If Statement ; if (x == 2) y = 5; LDR R0, R6, #3 ; 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, #4 NOT_TRUE . ; next statement 13- If-else if (condition) action_if; else action_else; condition action_if action_else T F Else allows choice between two mutually exclusive actions without re-testing condition. 13- Generating Code for If-Else if (x) { y++; z--; } else { y--; z++; } LDR R0, R6, #3 BRz ELSE ; x is not zero LDR R1, R6, #4 ; incr y ADD R1, R1, #1 STR R1, R6, #4 LDR R1, R6, #5 ; decr z ADD R1, R1, #1 STR R1, R6, #5 JMP DONE ; skip else code ; x is zero ELSE LDR R1, R6, #4 ; decr y ADD R1, R1, #-1 STR R1, . | Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending based on evaluated expression while for do-while 13- If if (condition) action; condition action T F Condition 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). 13- Example If Statements if (x 13- More If Examples if (0 <= age && age <= 11) 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 .
đang nạp các trang xem trước