notes2

The grades for this quiz are available to you

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: next item and cross it off my list. – while loop repeated until condition becomes false The while Repetition Statement • Example int product = 1; while ( product < 1000 ) { printf( “%d\n”, product ); product = 2 * product; } product < 1000 true print “product”; product = 2*product; false Formulating program with a while loop • Program with a while loop (and many other programs as well) have 3 phases: – Initialization: define and initialize program variables – Processing: input data values and perform all processing of data – Termination: Calculate and print final results Counter-Controlled Repetition • Counter-controlled repetition – Loop repeated until counter reaches a certain value – Example: A class of n students took a quiz. The grades for this quiz are available to you. Determine the class average on the quiz. – Pseudocode Set total to zero. Set counter to one. User inputs number of students in class. While counter is less than or equal to number of students, Input the next grade. Add the grade to the total. Add one to the grade counter. Set average to the total divided by the number of students. Print the class average. Ex. Prog. – Counter-controlled repetition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* class average program with counter-controlled repetition */ #include <stdio.h> int main() { /* define variables */ int students; /* number of students in the class */ int counter; /* grade number to be entered */ int grade; /* grade value */ float total; /* sum of...
View Full Document

Ask a homework question - tutors are online