06LoopsSmall

06LoopsSmall - CMSC 106 Lecture Set #6 Loops Set Started:...

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

View Full Document Right Arrow Icon
1 CMSC 106 Lecture Set #6 Loops Set Started: Friday, September 30, 2011 loops also called "repetition statements " s The While Statement while (condition) statement; s action: s 1) condition is tested s 2) if the condition is true the statement is performed; if the condition is false, continue after the loop s 3) after completing the loop’s body, go back to number 1 s iteration = one execution of the subsidiary statement Trace int var= 1; while (var < 5) { var = var + 1; printf("%d\n", var); }
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 Infinite Loops s The loop will never terminate on its own. s In UNIX, to stop a program with an infinite loop s Control-c s there can be a delay int var= 1; while (var < 5) { printf("%d\n", var); } The do/while loop s Format: do { statements } while (condition); s the curly braces are not required, but good style otherwise the line with "while" can be easily confused with the beginning of a while loop. s Action: s 1) execute the body s 2) test the condition s 3) if the condition is true, go back to #1; if the condition is false, continue with the line after the loop
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

06LoopsSmall - CMSC 106 Lecture Set #6 Loops Set Started:...

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

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