1 / 20

CMPT 102 Introduction to Scientific Computer Programming

CMPT 102 Introduction to Scientific Computer Programming. Control Structures while Loops continue; and break; statements. 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

Télécharger la présentation

CMPT 102 Introduction to Scientific Computer Programming

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 102Introduction to Scientific Computer Programming Control Structures while Loops continue; and break; statements

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

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

  6. Counting while statement Initial statements start = n end = m loopIndex = start; Repeats Statements m-n times Loop condition loopIndex < end F loopIndex += step; T Update statement First Statement: ….. Last Statement;

  7. Example while Loop in C /* Sum the Integers from 1 to 25 inclusive */ X = 1; Sum = 0; while ( X <= 25 ) { Sum += X; X++; } printf(“%d\n”, Sum);

  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;

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

  10. Counting do…while statement Initial statement start = n end = m loopIndex = start; Repeats Statements m-n times First Statement: ….. Last Statement; loopIndex += step; Update statement loopIndex < end F T Loop condition

  11. Example do…while Loop /* Sum the Integers from 1 to 25 inclusive */ X = 1; sum = 0; do { sum += X; X++; } while ( X <= 25 ); printf(“%d”, sum);

  12. Differences do vs do…while /* Sum Integers from n to m */ n = 4: m = 3: sum = 0; while ( n <= m ) { sum += n; n++; } printf(“%d”, sum); /* Sum Integers from n to m */ n = 4: m = 3: sum = 0; do { sum += n; n++; } while ( n <= m ); printf(“%d”, sum); After code sum = 4 Body of loop executes once After code sum = 0 Body of loop does not execute

  13. 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 into your linux or cygwin command window

  14. Uses of an infinite loop • To make an infinite loop useful, you must have a way to exit from it • It is possible to exit from a loop by using a break; statement (discussed below) • If you use an infinite loop intentionally it is usually a loop you intend to execute many times until • Some condition is met OR • The loop has executed the maximum number of times • If you use an infinite loop intentionally you should always exit after a maximum number of times through the loop, just in case the condition you expect to take you out of the loop never occurs.

  15. The Break Statement: Loops • Sometimes you wish to exit from a loop even if the loop condition is still true • For example if your loop is reading and processing data and an error occurs that can be recognized but cannot be corrected, you may wish to print an error message and exit the loop immediately • break; • A C statement that causes you to exit the loop you are in • Program execution will continue at the statement following the loop

  16. break statement Condition Loop condition F T Statement 1; ….. T condition2 break; F ….. Statement n;

  17. The Continue Statement: • Sometimes you wish to execute only some of the statements in the body of the loop for a particular value of the loop index • For example you may wish to skip the last half of the body of the loop if (loopindex/14)= = 1 • continue; • A C statement that causes you to go from anywhere in the loop to the update statement and loop condition • Program execution will continue at the update statement of the loop

  18. continue statement Condition F T Statement 1; ….. T condition2 continue; F ….. Statement n;

  19. continue in Counting while Start = n End = m loopIndex = start; Repeats Statements m-n times Initial statement Loop condition loopIndex < end F loopIndex += step; T First Statement: ….. Update statement T condition2 continue; F ….. Last Statement;

  20. Example infinite Loop in C /* The user inputs a positive integer */ /* the routine prints the square of the number, */ /* to terminate the user enters -999 */ while ( 1 ) { printf("enter a positive integer " ); scanf("%d", &positiveIntNum); if(positiveIntNum < 0) { if( positiveIntNum == -999) {break}; printf("error*** negative input ***\n"); continue; } printf("%d\n", positiveIntNum*positiveIntNum); }

More Related