Lec25 - 1 Computer Programming I TA C162 . 10 Mar 2007 More...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 Computer Programming I TA C162 . 10 Mar 2007 More looping with while Iteration in C for statement while statement do-while statement 2 Computer Programming I TA C162 . 10 Mar 2007 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. 3 Computer Programming I TA C162 . 10 Mar 2007 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 4 Computer Programming I TA C162 . 10 Mar 2007 Infinite Loops The following loop will never terminate: Why??? 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. 5 Computer Programming I TA C162 . 10 Mar 2007 For vs. While In general: For loop is preferred for counter-based loops....
View Full Document

Page1 / 17

Lec25 - 1 Computer Programming I TA C162 . 10 Mar 2007 More...

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