Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 10: While/do-while examples
2
Announcements/reminders
Late submissions and regrade requests: Dr. Geiger and Pakin Program 1 regrade requests due today Program 2 regrade requests due 10/3 Program 3 due Monday, 10/1 Exam 1: Friday, 10/5 Allowed one double-sided 8.5” x 11” note sheet No calculators or other electronic devices allowed 7/23/2019 ECE Application Programming: Lecture 11
3
ECE Application Programming: Lecture 11
Lecture outline Review Switch statements While loops Today’s lecture While loop applications Do-while loops Example problems 7/23/2019 ECE Application Programming: Lecture 11
4
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 7/23/2019 ECE Application Programming: Exam 1 Preview
5
ECE Application Programming: Lecture 11
Review: while loops Used for repetition of code while (<expression>) <statement> loop body 7/23/2019 ECE Application Programming: Lecture 11
6
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 } 7/23/2019 ECE Application Programming: Lecture 11
7
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 } 7/23/2019 ECE Application Programming: Lecture 11
8
ECE Application Programming: Lecture 11
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! 7/23/2019 ECE Application Programming: Lecture 11
9
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 7/23/2019 ECE Application Programming: Lecture 11
10
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) 7/23/2019 ECE Application Programming: Lecture 11
11
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 } 7/23/2019 ECE Application Programming: Lecture 11
12
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)); 7/23/2019 ECE Application Programming: Lecture 11
13
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'. 7/23/2019 ECE Application Programming: Lecture 11
14
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; } 7/23/2019 ECE Application Programming: Lecture 11
15
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); 7/23/2019 ECE Application Programming: Lecture 11
16
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'); 7/23/2019 ECE Application Programming: Lecture 11
17
ECE Application Programming: Lecture 11
Final notes Next time PE3: Conditionals and loops (Tuesday, 2/20) Reminders: Late submissions and regrade requests: Dr. Geiger and Pakin Program 1 regrade requests due today Program 2 regrade requests due 10/3 Program 3 due Monday, 10/1 Exam 1: Friday, 10/5 Allowed one double-sided 8.5” x 11” note sheet No calculators or other electronic devices allowed 7/23/2019 ECE Application Programming: Lecture 11
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.