Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 10: While/do-while examples

2 Announcements/reminders
Text exercises due 3 days after each lecture Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page ( 5/25/2019 ECE Application Programming: Lecture 9

3 ECE Application Programming: Lecture 11
Lecture outline Review While, do-while loops Today’s lecture While loop applications Do-while loops Example problems 5/25/2019 ECE Application Programming: Lecture 11

4 ECE Application Programming: Lecture 11
Review: while loops Used for repetition of code while (<expression>) <statement>  loop body 5/25/2019 ECE Application Programming: Lecture 11

5 Application: loop with flexible limit
Could determine loop limit based on variable Result of calculation Input value See while2.c for an example (on website) 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/25/2019 ECE Application Programming: Lecture 11

6 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 (on website) 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/25/2019 ECE Application Programming: Lecture 11

7 Review: 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/25/2019 ECE Application Programming: Lecture 11

8 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/25/2019 ECE Application Programming: Lecture 11

9 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/25/2019 ECE Application Programming: Lecture 11

10 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/25/2019 ECE Application Programming: Lecture 11

11 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/25/2019 ECE Application Programming: Lecture 11

12 ECE Application Programming: Lecture 11
Examples Write a while or do-while loop for each of the following tasks: Print all multiples of 3 between 0 and 100 (including 0) Given two variables, x and y, repeatedly increment x by 1 and decrement y by 1 until x is greater than y. Count the number of iterations this loop takes and print it when the loop is done Print the initial values of x and y before the loop starts Repeatedly prompt for and read a single non-space character into a variable, cmd, until the user enters either 'X' or 'x'. 5/25/2019 ECE Application Programming: Lecture 11

13 ECE Application Programming: Lecture 11
Example solutions Print all multiples of 3 between 0 and 100 (including 0) int i = 0; while (i < 100) { printf("%d\n", i); i = i + 3; } 5/25/2019 ECE Application Programming: Lecture 11

14 Example solutions (continued)
Given two integer variables, x and y, repeatedly increment x by 1 and decrement y by 1 until x is greater than y. Print the initial values of x and y before the loop starts Count the number of iterations this loop takes and print it when the loop is done int x, y; int i = 0; // i = # iterations ... // Code to assign values to x & y printf("x = %d, y = %d initially\n", x, y); while (x <= y) { x = x + 1; y = y - 1; i = i + 1; } printf("Number of iterations: %d\n", i); 5/25/2019 ECE Application Programming: Lecture 11

15 Example solutions (continued)
Repeatedly prompt for and read a single non-space character into a variable, cmd, until the user enters either 'X' or 'x'. char cmd; do { printf("Enter character: "); scanf(" %c", &cmd); } while (cmd != 'X' && cmd != 'x'); 5/25/2019 ECE Application Programming: Lecture 11

16 ECE Application Programming: Lecture 11
Final notes Next time Exam 1 Preview Reminders: Text exercises due 3 days after each lecture Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page ( 5/25/2019 ECE Application Programming: Lecture 11


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google