EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Advertisements

Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
ECE Application Programming
ECE Application Programming
Introduction to Computer Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
ECE Application Programming
ECE Application Programming
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 4 – Control Structures Part 1
Lecture 07 More Repetition Richard Gesick.
EECE.2160 ECE Application Programming
Repetition and Loop Statements
The while Looping Structure
The while Looping Structure
Computing Fundamentals
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
The while Looping Structure
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
The while Looping Structure
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Week 3 – Repetition (ctd.)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

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

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 (http://mjgeiger.github.io/eece2160/oldexams.htm) 5/25/2019 ECE Application Programming: Lecture 9

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

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

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

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

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

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

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

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

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

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

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

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

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

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 (http://mjgeiger.github.io/eece2160/oldexams.htm) 5/25/2019 ECE Application Programming: Lecture 11