1 / 15

CMPT 128: Introduction to Computing Science for Engineering Students

CMPT 128: Introduction to Computing Science for Engineering Students. Control Structures Loops. 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

nile
Download Presentation

CMPT 128: Introduction to Computing Science for Engineering Students

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Loops

  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)

  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

  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 continue executing while some condition is true and terminate when it becomes false • Use a condition based while loop

  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;

  6. condition F T Statement 1; Statement n; Structure of while loop

  7. Example while Loop in C++ // Sum the Integers from 1 to 5 inclusive x = 1; largest = 5; sum = 0; while ( x <= largest ) { sum += x; x++; } cout << sum;

  8. Trace code by hand • Begin while loop • (x=1) <= (largest =5) • Enter loop • sum=0+1=1 • x=x+1=1+1=2 • Renter loop (x=2)<=5 • sum=1+2=3 • x=x+1=2+1=3 • Renter loop (x=3)<=5 • sum=3+3=6 • x=x+1=3+1=4 x = 1; largest = 5; sum = 0; while ( x <= largest ) { sum += x; x++; } cout << sum;

  9. Trace code by hand • Renter loop (x=3)<=5 • sum=3+3=6 • x=x+1=3+1=4 • Renter loop (x=4)<=5 • sum=6+4=10 • x=x+1=4+1=5 • Renter loop (x=5)<=5 • sum=10+5=15 • x=x+1=5+1=6 • Leave loop (x=6) > 5 • Print value of sum, 15 x = 1; largest = 5; sum = 0; while ( x <= largest ) { sum += x; x++; } cout << sum;

  10. 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;

  11. Statement 1; Statement n; T T condition F Loop condition Structure of do…while Loop

  12. Example do…while Loop // Sum the Integers from 1 to 5 inclusive x = 1; sum = 0; do { sum += x; x++; } while ( x <= 5 ); cout << sum;

  13. Differences do vs do…while // Sum Integers from n to m n = 4; m = 3; sum = 0; while ( n <= m ) { sum += n; n++; } cout << sum; // Sum Integers from n to m n = 4; m = 3; sum = 0; do { sum += n; n++; } while ( n <= m ); cout << sum; After code sum = 4 Body of loop executes once After code sum = 0 Body of loop does not execute

  14. Infinite Loops • When using while or do..while loops it is possible that the loop condition is always true. • If the variable or expression tested in the loop condition is not modified in the action statements within the loop, then if the condition is true for the first loop test it remains true for ever • Even if the variable or expression changes it may satisfy the test condition for ever • An infinite loop will cause your program to execute forever • If your program is caught in an infinite loop you can terminate it by typing <CNTRL>C

  15. Example: infinite while Loop //Programmer intended to sum integers 2 to 25 // Actually Summed the Integers from 27 to …? x = 27; sum = 0; while ( x != 25 ) { sum += x; x++; } cout << sum;

More Related