06LoopsLarge

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

Info iconThis preview shows pages 1–4. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 loops body, go back to number 1 s iteration = one execution of the subsidiary statement 2 Trace int var= 1; while (var < 5) { var = var + 1; printf("%d\n", var); } 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); } 3 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...
View Full Document

Page1 / 8

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

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

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