260 likes | 373 Views
B Smith: See previous lecture for additional notes on splitting this up. Fall05: This will be L08. Math 130 Introduction to Computing. Lecture # 06 – “Repetition”. Overview. The while() Statement (§5.1) Using scanf() within a while() loop (§5.2) The for() Statement (§5.3)
E N D
B Smith: See previous lecture for additional notes on splitting this up. Fall05: This will be L08 Math 130Introduction to Computing Lecture # 06 – “Repetition”
Overview • The while() Statement (§5.1) • Using scanf() within a while() loop (§5.2) • The for() Statement (§5.3) • The do() Statement (§5.4) • break/continue Statements
The while statement • The while statement is similar to the if statement. Recall: if (expression) statement; • The while statement syntax is: while (expression) statement; • The while and if (expressions) are both tests which determine the next step in the program • The if statement is done once when the expression is true (or nonzero) • The while statement is performed repeatedly as long as (while) the expression is true (or nonzero)
The while statement • The algorithm for the while statement: • test the expression • if the expression has a true value • execute the statement following the parens • return to step 1 • else • exit the while statement
The while statement • Example: count = 1;while (count <= 10){ printf( “ %d “, count);} Start of loop Watch endless loops! End of loop count = 1;while (count <= 10){ printf( “ %d “, count); count = count + 1;} Start of loop End of loop
B Smith: Consider covering for loops before sentinels? Sentinels • Sentinel • to keep guard • to watch over as a guard • Used for continuous data entry • but for an undetermined number of loops • The sentinel is a flag of sorts. • It allows the user to flag the program to either start or stop data entry.
Sentinel Example Our sentinel is the value 100 #include <stdio.h> int main() { float grade, total; grade = 0; total = 0; printf("\nTo stop entering grades, type in any number"); printf("\n greater than 100.\n\n"); while (grade <= 100) { printf("Enter a grade: "); scanf("%f", &grade); total = total + grade; } printf("\nThe total of the grades is %f",total-grade); return 0; }
Sentinel Example (cont'd) To stop entering grades, type in any number greater than 100. Enter a grade: 95 Enter a grade: 87 Enter a grade: 99 Enter a grade: 62 Enter a grade: 100 Enter a grade: 101 The total of the grades is 443.000000 Press any key to continue
EOF Sentinel • EOF is a sentinel frequently seen in C programs • EOF = End Of File • EOF has an actual integer value that can be found in <stdio.h> . • To ensure portability, EOF is a generic sentinel whose actual value shouldn’t be tested for
B Smith: Discuss using scanf and EOF B Smith: Stopped here on Feb 8, 2005 EOF Sentinel • After changing the while test as follows • The output changes to: while (1) //loop until ^z { printf("Enter a grade: "); if (scanf("%d", &grade) == EOF) break; total = total + grade; } The compiler creates an executable that looks for the EOF sentinel (for the PC, this is ^z) Enter a grade: 80 Enter a grade: 90 Enter a grade: 100 Enter a grade: ^Z The total of the grades is 270.000000
B Smith: 2/7/2005 11:46 AM: A complete lecture should start here. (Start L08 here.) See previous lecture of additional notes. Format is good, but not enough examples. Lecture should be more interactive. From slide 12 to 29 is one complete lecture The for Statement • expression1 • initialize variables • this expression is done before the loop starts • expression2 • a test condition • when false (or 0), the loop stops • expression3 • performed at end of loop • usually an incrementer is found here (e.g., i++) for (expression1; expression2; expression3) statement; B Smith: 2/4/2005 9:57 AM:Start here Monday, 2/7.
The forStatement • The for and while statements are interchangeable • Here's the for statement: for (count = 1; count <= 10; ++count) printf(“%d”, count); • Here's a while statement that does the same thing: count = 1; while (count <= 10){printf(“%d”, count); ++count;}
The for Statement • No items are required in the for statement’s parentheses • The two semicolons must be present • Recall: for (init; test; alter) • Hence a legitimate statement could be: for (i=0 ;; i++)
The for Statement –eg01 #include <stdio.h> int main() { int count; count = 2; for ( ; count <= 20; count = count + 2) printf("%d ",count); return 0; } Initialization is done outside the loop
The for Statement –eg02 #include <stdio.h> int main() { int count; count = 2; for( ; count <= 20; ) { printf("%d ",count); count = count + 2; } return 0; } Initializing statement Test statement Alterizing statement
The for Statement –eg03 #include <stdio.h> int main() { int count; for ( count = 2 ; count <= 20 ; printf("%d ",count), count = count + 2 ); return 0; }
The for Statement –eg04 #include <stdio.h> int main() { int count; for (; ;); return 0; }
Infinite Loops while ( 1 ) { ...etc...etc...etc... } for ( ; 1 ; ) { ...etc...etc...etc... } for ( ; ; ) { ...etc...etc...etc... }
Infinite Loops while ( 1 ) { ...etc...etc...etc... } Use an: if ( condition ) { break; } statement to break the loop for ( ; 1 ; ) { ...etc...etc...etc... } for ( ; ; ) { ...etc...etc...etc... }
The do Statement • Similar to the while statement, but the “test” comes at end: do statement;while ( expression );
The doStatement • Recall the while statement: printf(“Enter a price:); scanf(“%f”, &price); while ( price != SENTINEL ) { salestax = RATE * price; printf(“The sales tax is $%5.2f”, salestax); printf(“\nEnter a price: “); scanf(“%f, &price); } • Here's a do statement that does the same thing: do { printf(“Enter a price:); scanf(“%f”, &price); if( price == SENTINEL) break; salestax = RATE * price; printf(“The sales tax is $%5.2f”, salestax); } while ( price != SENTINEL );
breakStatements • We previously used break to get out of a case/switch statement • The break statement forces an immediate break from • switch • while • for • do-while
breakexample while(count <= 10) { printf(“Enter a number: “); scanf(“%f”, &num); if (num > 76) { printf(“You lose!”); break; } else printf(“Keep on truckin!”); } break forces the program to jump here
continue statement • continue is very similar to break but less often used • used inside a loop • Instead of terminating the loop, continue starts re-executing the body of the loop from the top. • Only used within a for, while, or do-while loop • If continue is placed within an if statement, the if statement is not re-tested
continue example int x; printf(“Printing only the even numbers from 1 to 10 \n”); for ( x = 1; x <= 10; x++) { if( x % 2 != 0) /* Is x even ? */ continue; /* Yes, get next instance of x */ printf( “\n%d”, x); } This is one is tricky. It’s important to note that continue not only sends you back up to the test condition at loop start, but it also increments x.
Summary • The while() Statement (§5.1) • Using scanf() within a while() loop (§5.2) • The for() Statement (§5.3) • The do() Statement (§5.4) • break/continue Statements