CMPT 102 Introduction to Scientific Computer Programming Control

  • Slides: 20
Download presentation
CMPT 102 Introduction to Scientific Computer Programming Control Structures while Loops continue; and break;

CMPT 102 Introduction to Scientific Computer Programming Control Structures while Loops continue; and break; statements 10/28/2020 © Janice Regan, CMPT 102, Sept. 2006 0

Control Structures ] Three methods of processing a program S In sequence S Branching

Control Structures ] Three methods of processing a program S In sequence S Branching S 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 102, Sept. 2006 1

Basic Loops ] When one action is to be repeated a number of times

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 S while loop or do. . . while loop l Used to continue repetition while a condition holds l Can also be used (along with a counter variable) to repeat a particular number of times S for loop l Specifically designed to repeat a particular number of times © Janice Regan, CMPT 102, Sept. 2006 2

A while Loop in C while ( condition ) { /* Series of actions

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 102, Sept. 2006 3

Structure of while loop condition F T Statement 1; Statement n; © Janice Regan,

Structure of while loop condition F T Statement 1; Statement n; © Janice Regan, CMPT 102, Sept. 2006 4

Counting while statement Initial statements start = n end = m loop. Index =

Counting while statement Initial statements start = n end = m loop. Index = start; Repeats Statements m-n times Loop condition loop. Index < end loop. Index += step; F T Update statement First Statement: …. . Last Statement; © Janice Regan, CMPT 102, Sept. 2006 5

Example while Loop in C /* Sum the Integers from 1 to 25 inclusive

Example while Loop in C /* Sum the Integers from 1 to 25 inclusive X = 1; Sum = 0; while ( X <= 25 ) { Sum += X; X++; } printf(“%dn”, Sum); © Janice Regan, CMPT 102, Sept. 2006 */ 6

do…while Loop in C do { /*Series of actions to be taken /*each time

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 102, Sept. 2006 7

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

Structure of do…while Loop Statement 1; T Statement n; T condition F Loop condition © Janice Regan, CMPT 102, Sept. 2006 8

Counting do…while statement Initial statement start = n end = m loop. Index =

Counting do…while statement Initial statement start = n end = m loop. Index = start; Repeats Statements m-n times First Statement: …. . Last Statement; Update statement loop. Index += step; T © Janice Regan, CMPT 102, Sept. 2006 loop. Index < end F Loop condition 9

Example do…while Loop /* Sum the Integers from 1 to 25 inclusive X =

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); © Janice Regan, CMPT 102, Sept. 2006 */ 10

Differences do vs do…while /* Sum Integers from n to m */ n =

Differences do vs do…while /* 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 © Janice Regan, CMPT 102, Sept. 2006 /* Sum Integers from n to m */ n = 4: m = 3: sum = 0; while ( n <= m ) { sum += n; n++; } printf(“%d”, sum); After code sum = 0 Body of loop does not execute 11

Infinite Loops ] When using while or do. . while loops it is possible

Infinite Loops ] When using while or do. . while loops it is possible that the loop condition is always true. S 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 S 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 © Janice Regan, CMPT 102, Sept. 2006 12

Uses of an infinite loop ] To make an infinite loop useful, you must

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 S Some condition is met OR S 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. © Janice Regan, CMPT 102, Sept. 2006 13

The Break Statement: Loops ] Sometimes you wish to exit from a loop even

The Break Statement: Loops ] Sometimes you wish to exit from a loop even if the loop condition is still true S 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; S A C statement that causes you to exit the loop you are in S Program execution will continue at the statement following the loop © Janice Regan, CMPT 102, Sept. 2006 14

break statement Loop condition Condition F T Statement 1; …. . condition 2 F

break statement Loop condition Condition F T Statement 1; …. . condition 2 F …. . Statement n; © Janice Regan, CMPT 102, Sept. 2006 T break; 15

The Continue Statement: ] Sometimes you wish to execute only some of the statements

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 S For example you may wish to skip the last half of the body of the loop if (loopindex/14)= = 1 ] continue; S A C statement that causes you to go from anywhere in the loop to the update statement and loop condition S Program execution will continue at the update statement of the loop © Janice Regan, CMPT 102, Sept. 2006 16

continue statement Condition F T Statement 1; …. . condition 2 F …. .

continue statement Condition F T Statement 1; …. . condition 2 F …. . Statement n; © Janice Regan, CMPT 102, Sept. 2006 T continue; 17

continue in Counting while Start = n Initial statement End = m loop. Index

continue in Counting while Start = n Initial statement End = m loop. Index = start; Loop condition Repeats Statements m-n times loop. Index < end loop. Index += step; Update statement T First Statement: …. . condition 2 F …. . Last Statement; © Janice Regan, CMPT 102, Sept. 2006 F T continue; 18

Example infinite Loop in C /* The user inputs a positive integer /* the

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", &positive. Int. Num); if(positive. Int. Num < 0) { if( positive. Int. Num == -999) {break}; printf("error*** negative input ***n"); continue; } printf("%dn", positive. Int. Num*positive. Int. Num); } © Janice Regan, CMPT 102, Sept. 2006 19