Download presentation
Presentation is loading. Please wait.
1
16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2013 Lecture 8: Loops: while, do-while
2
ECE Application Programming: Lecture 7
Lecture outline Announcements/reminders Program 3 posted; due 9/25 Review Conditional statements: switch Today’s lecture Loops: while, do-while 5/23/2018 ECE Application Programming: Lecture 7
3
Review: switch statements
When checking multiple exact values for expression, more sense to use switch statement switch (<expr>) { case <val1> : ... break; case <val2> : default: } break allows you to exit switch statement after completing code Otherwise, program will continue to run through cases until finding break default covers any values without specific case 5/23/2018 ECE Application Programming: Lecture 4
4
ECE Application Programming: Lecture 4
Repetition Say we have a program to print squares of numbers between 0 and 10: void main() { int i; // Number to square int iSquared; // Square of the number printf(" i i^2\n"); // Column headings // Compute and display the squares of numbers 0 through 10 i = 0; iSquared = i * i; printf("%2d%10d\n", i, iSquared); ... // Code for i = 1, 2, ... 8, 9 i = 10; } 5/23/2018 ECE Application Programming: Lecture 4
5
ECE Application Programming: Lecture 4
while loops Previous program does same thing 11 times Repetitive code can be captured in a loop Much less code to do same amount of work Simplest form: while loop while (<expression>) <statement> loop body Loop body will repeat as long as <expression> is true Loop body must therefore change expression <statement> may be one or more lines If multiple lines, need { } to denote block 5/23/2018 ECE Application Programming: Lecture 4
6
ECE Application Programming: Lecture 4
while loops - example x = 7; while ( x < 10 ) { printf("%d ",x); x = x + 1; } OUTPUT: 7 8 9 5/23/2018 ECE Application Programming: Lecture 4
7
ECE Application Programming: Lecture 4
while loops - example x = 7; while ( x < 3 ) { printf("%d ",x); x = x + 1; } OUTPUT: (no output) Possible to have while loop body that never executes! 5/23/2018 ECE Application Programming: Lecture 4
8
Repetition with while loop
Rewriting previous program with loop int main() { int i; // Number to square int iSquared; // Square of the number printf(" i i^2\n"); // Column headings // Compute and display the squares of numbers 0 to 10 i = 0; // Initialize i while (i <= 10) { // Loop until i > 10 iSquared = i * i; printf("%2d%10d\n", i, iSquared); i = i + 1; // Increment i } return 0; 5/23/2018 ECE Application Programming: Lecture 4
9
Application: loop with flexible limit
Could determine loop limit based on variable Result of calculation Input value See while2.c for an example Program to calculate average grade First reads # of grades to enter, then list of grades Keeps running sum of all grades entered Calculates average at end Loop: while (gradeCount < numGrades) { scanf("%lf", &grade); // Read grade gradeSum = gradeSum + grade; // Add to sum gradeCount = gradeCount + 1; // Inc. count } 5/23/2018 ECE Application Programming: Lecture 4
10
Application: sentinel value
Common to read input until a certain value(sentinel) is entered May be predetermined (i.e., run program until user enters ‘q’ for “quit”) Run until invalid value entered In file input, will often run until end of file See while3.c for an example Refined version of average grade program Core of program: // Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade); /* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade } 5/23/2018 ECE Application Programming: Lecture 4
11
ECE Application Programming: Lecture 4
do-while loops while loop is pre-tested Check condition at start; if false, don’t enter loop To guarantee at least one iteration, use post-tested loop: do-while Checks condition at end of loop do { <statements> } while ( <expression> ); Don’t forget semicolon! 5/23/2018 ECE Application Programming: Lecture 4
12
While vs. do-while: flowcharts
while: pre-tested loop Check condition, then execute loop body do-while: post-tested loop Execute loop body, then check condition A = 0? Loop body FALSE TRUE A = 0? TRUE Loop body FALSE All loops characterized by conditional test, backwards arrows indicating repetition of code 5/23/2018 ECE Application Programming: Lecture 4
13
comparison while vs do-while
x = 7; do { printf("%d ",x); x = x + 1; } while ( x < 10 ); OUTPUT: 7 8 9 x = 7; while ( x < 10 ) { printf("%d",x); x = x + 1; } OUTPUT: 7 8 9 5/23/2018 ECE Application Programming: Lecture 4
14
comparison while vs do-while
x = 7; do { printf("%d",x); x = x + 1; } while ( x < 3 ); OUTPUT: 7 x = 7; while ( x < 3 ) { printf("%d",x); x = x + 1; } OUTPUT: (no output) 5/23/2018 ECE Application Programming: Lecture 4
15
Application: sentinel value
Core of program demonstrating while loop // Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade); /* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade } 5/23/2018 ECE Application Programming: Lecture 4
16
Application: sentinel value
Rewrite grade average program to ensure at least one grade is read Change core of program (shown previously): /* Prompt for and read grades until invalid value entered */ do { printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read grade if ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Inc. grade count } } while ((grade >= 0.0) && (grade <= 100.0)); 5/23/2018 ECE Application Programming: Lecture 4
17
ECE Application Programming: Lecture 7
Final notes Next time Loops: for Reminders: Program 3 due 9/25 5/23/2018 ECE Application Programming: Lecture 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.