Lecture27 - ComputerProgrammingITAC162. More looping with...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Computer Programming I TA C162 . 10 Mar 2006 More looping with while Iteration in C for statement while statement do-while statement
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Computer Programming I TA C162 . 10 Mar 2006 While while (test) { loop_body; } test loop_body T F Loop body can contain block of statements. Executes loop body as long as test evaluates to TRUE (non-zero). Note: Test is evaluated before executing loop body.
Background image of page 2
3 Computer Programming I TA C162 . 10 Mar 2006 While in LC-3 x = 0; while (x < 10) { printf(“%d ”, x); x = x + 1; } AND R0, R0, #0 STR R0, R5, #0 ; x = 0 ; test LOOP 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 again DONE ; next statement
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Computer Programming I TA C162 . 10 Mar 2006 Infinite Loops The following loop will never terminate: x = 0; while (x < 10) printf(“%d ”, x); Loop body does not change condition, so test never fails. This is a common programming error that can be difficult to find.
Background image of page 4
5 Computer Programming I TA C162 . 10 Mar 2006 For vs. While In general: For loop is preferred for counter -based loops. Explicit counter variable
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

Lecture27 - ComputerProgrammingITAC162. More looping with...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online