lecture8 Continue & Break

lecture8 Continue & Break - 1 © Janice Regan CMPT...

Info iconThis preview shows pages 1–10. 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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Loops continue; and break; statements © Janice Regan, CMPT 128, Jan 2007 2 Control Structures Three methods of processing a program In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repetition of a particular block of statement(s) © Janice Regan, CMPT 128, Jan 2007 3 Basic Loops When one action is to be repeated a number of times a loop is used. Loops are repetition structures There are two common types of loops while loop or do...while loop Used to continue repetition while a condition holds Can also be used (along with a counter variable) to repeat a particular number of times for loop Specifically designed to repeat a particular number of times © Janice Regan, CMPT 128, Jan 2007 4 What kind of loop When you know how many time you want to execute the block of code use a counting loop A counting while loop, you set up increment and test your counter A for loop, give the critical values the for loop. for loop sets up, increments and tests for you When you have no idea how many times the loop will be executed Loop will stop executing when some condition becomes true Use a condition based while loop © Janice Regan, CMPT 128, Jan 2007 5 A while Loop in C++ while ( condition ) { // Series of actions to be taken // each time the loop is executed // loop is executed when condition is True action 1; action 2; } // When condition is false execute following actions actions; © Janice Regan, CMPT 128, Jan 2007 6 Structure of while loop T F condition Statement 1; Statement n; © Janice Regan, CMPT 128, Jan 2007 7 Example while Loop in C++ // Sum the Integers from 1 to 25 inclusive x = 1; largest = 25; sum = 0; while ( x <= largest ) { sum += x; x++; } cout << sum; © Janice Regan, CMPT 128, Jan 2007 8 do…while Loop in C++ do { // Series of actions to be taken // each time the loop is executed // Always executed on first pass // Subsequently executed if condition is true action 1; action 2; } while ( condition ); // When condition is false execute following actions actions; © Janice Regan, CMPT 128, Jan 2007 9 Structure of do…while Loop T F condition Statement 1; Statement n; T Loop condition © Janice Regan, CMPT 128, Jan 2007...
View Full Document

{[ snackBarMessage ]}

Page1 / 29

lecture8 Continue &amp; Break - 1 © Janice Regan CMPT...

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

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