Download presentation
Presentation is loading. Please wait.
1
© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops
2
© Janice Regan, CMPT 102, Sept. 2006 1 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
© Janice Regan, CMPT 102, Sept. 2006 2 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
© Janice Regan, CMPT 102, Sept. 2006 3 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 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
5
© Janice Regan, CMPT 102, Sept. 2006 4 for statement T loopIndex < end loopIndex += step; loopIndex = start; Initial statement Loop condition First Statement: ….. Last Statement; F Repeats Statements m-n times Update statement start = n end = m
6
© Janice Regan, CMPT 102, Sept. 2006 5 for Loop in C for (initial statement; loop condition; update statement) { /*Series of actions to be taken */ /*each time the loop is executed*/ action 1; action 2; }
7
© Janice Regan, CMPT 102, Sept. 2006 6 Example for Loop in C SumOfInt = 0; for (index=0 ; index <= 10 ; index++) { /*Loop reads 11 integers and */ /*determines the sum of those integers*/ printf("enter an integer "); scanf( "%d", &newnum); printf( "%d\n", newnum); SumOfInt += newnum; } /* print the value of the sum to the screen */ printf( "%d", SumOfInt);
8
© Janice Regan, CMPT 102, Sept. 2006 7 Example while Loop in C sumOfInt = 0; index = 0; while( index <= 10 ) { /*Loop reads 11 integers and */ /*determines the sum of those integers*/ printf("enter an integer "); scanf( "%d", &newnum); printf( "%d\n", newnum); sumOfInt += newnum; index++; } /* print the value of the sum to the screen */ printf( "%d", sumOfInt);
9
© Janice Regan, CMPT 102, Sept. 2006 8 Condition controlled loops Types of conditions for controlling while loops General condition controlled Sentinel controlled Flag controlled Endfile controlled (special sentinel/flag value) Input validation loop
10
© Janice Regan, CMPT 102, Sept. 2006 9 Sentinel Controlled loops A sentinel value is a special value of a particular variable known as a sentinel variable. The sentinel value of a sentinel variable must be a value of the variable that is not encountered in normal operation of the loop When the sentinel variable has the sentinel value at the end of the statements in the loop, execution will pass to the next statement following the loop Often used for reading unknown amounts of input data
11
© Janice Regan, CMPT 102, Sept. 2006 10 Sentinel Controlled loop while ( sentinel variable != sentinel value ) { /*Series of actions to be taken each time*/ /*the loop is executed */ /*one of these actions will change the */ /*value of the sentinel variable */ action 1; action m changes the value of sentinel variable; action n; }
12
© Janice Regan, CMPT 102, Sept. 2006 11 Sentinel controlled Loop T Sentinelvariable == Sentinelvalue Sentinelvariable =Start; Statement 1; ….. Statement n changes value of Sentinel value; …… Statement m; Initial statement Loop condition F
13
© Janice Regan, CMPT 102, Sept. 2006 12 Sentinel Controlled loop /* The user inputs a positive integer the routine prints the square of the number, */ /* to terminate the user enters -999 */ printf("enter a positive integer" ); scanf("%d", &positiveIntNum); while ( positiveIntNum != -999 ) { if(positiveIntNum < 0) { printf("error*** negative input ***\n"); } else { printf("%d\n", positiveIntNum*positiveIntNum); } printf("enter a positive integer" ); scanf("%d", &positiveIntNum); }
14
© Janice Regan, CMPT 102, Sept. 2006 13 Endfile controlled Loop T nscan == EOF int nscan: … nscan = scanf(“&lf”, v1); Initial statement Loop condition F Statement 1; … Statement m; Update statement
15
© Janice Regan, CMPT 102, Sept. 2006 14 Endfile controlled Loop /* Data is read from a file, one line (three values at a time */ /* Each time a line is read the cost is updated */ /* After all data is read the cost is printed to the screen */ FILE * infile; double v1,v2, v3, cost; int nscan; fopen(“filename”,”r”); /* should check if file has been opened */ /*after reading nscan holds the number of items read, or an error condition */ nscan = fscanf(infile, “&lf”, v1); while ( nscan != EOF ) { cost += v1; nscan = fscanf(“&lf”, v1); } fclose(infile); printf( “Total cost is %f”, cost);
16
© Janice Regan, CMPT 102, Sept. 2006 15 Flag controlled Loop T nscan <= 3 int nscan: … nscan = scanf(“&lf, &lf, &lf”, v1, v2, v3); Initial statement Loop condition F Statement 1; … Statement m; Update statement
17
© Janice Regan, CMPT 102, Sept. 2006 16 Flag controlled Loop /* Data is read from a file, one line (three values at a time */ /* Each time a line is read the cost is updated */ /* After all data is read the cost is printed to the screen */ FILE * infile; double v1,v2, v3, cost; int nscan; fopen(“filename”,”r”); /* should check if file has been opened */ /*after reading nscan holds the number of items read, or an error condition */ nscan = fscanf(infile, “&lf, &lf, &lf”, v1, v2, v3); while ( nscan == 3 ) { cost += (v2 + v1 / v3); nscan = fscanf(“&lf, &lf, &lf”, v1, v2, v3); } fclose(infile); printf( “Total cost is %f”, cost);
18
© Janice Regan, CMPT 102, Sept. 2006 17 Flag Controlled loop /* The user inputs a positive integer the routine prints the square of the number, */ /* to terminate the user enters -999 */ printf("enter a positive integer" ); scanf("%d", &positiveIntNum); while ( positiveIntNum != -999 ) { if(positiveIntNum < 0) { printf("error*** negative input ***\n"); } else { printf("%d\n", positiveIntNum*positiveIntNum); } printf("enter a positive integer" ); scanf("%d", &positiveIntNum); }
19
© Janice Regan, CMPT 102, Sept. 2006 18 Input validation Loop T If v1 is valid int nscan: … nscan = scanf(“&lf”, v1); Initial statement Loop condition F printf (“invalid input, please reenter v1”); Update statement
20
© Janice Regan, CMPT 102, Sept. 2006 19 Input validation loop Use when requesting data from user Do not use when reading from a file When an error occurs reading a file a subsequent read will read the same incorrect data You will create an infinite loop by using an input validation loop When an error is detected reading from a file, reading must be terminated, if more information from the file is required the program should be terminated with an error message
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.