CS150 Introduction to Computer Science I Section 02 MWF 9:00 ...

Loops section 5.2, 5.4, 5.7 9/28/07 CS150 Introduction to Computer Science 1 1 Loop! So far, we can o Get input Produce output o

Calculate Conditionally execute statements Loops o Perform the same bit of code many times statement1; statement2; . . . statement3; o Why might we want to do this? 9/28/07

CS150 Introduction to Computer Science 1 2 While Loop (5.2) while the expression is true, loop! while( expression ) { statement1; statement2; . . . statement3; } statement4; 1 Test the expression 2

Either Perform the statements in the loop or Move past the loop 3 9/28/07 Repeat CS150 Introduction to Computer Science 1 3 Example: What happens? int number = 0; while( number < 5 ) { cout << Number : ;

cout << number << endl; cout << Please enter a number : ; cin >> number; } cout << The final number is: ; cout << number << endl; 9/28/07 CS150 Introduction to Computer Science 1 4 Counters (5.4) Counter: A variable that is incremented or decremented each time a loop runs int theCounter = 0; // initialize the counter

while( theCounter < 2 ) { cout << theCounter : cout << theCounter << theCounter += 1; } // test the counter ; endl; // increment the counter What will happen? 9/28/07 CS150 Introduction to Computer Science 1 5

Key Ingredients of while loops Initialize MUST initialize the counter Test The value of the counter is tested before each iteration Update (Increment/Decrement) The counter is changed during each loop iteration If any one of these is missing or incorrect, your loop wont run properly--not at all, too many/few times or infinitely. 9/28/07 CS150 Introduction to Computer Science 1 6 Counters

int theCounter = 1; // initialize the counter while( theCounter < 2 ) { cout << theCounter : cout << theCounter << theCounter += 1; } // test the counter ; endl; // increment the counter What will happen? 9/28/07

CS150 Introduction to Computer Science 1 7 Counters int theCounter = 0; // initialize the counter while( theCounter < 2 ) // test the counter { theCounter += 1; // increment the counter cout << theCounter : ; cout << theCounter << endl; } What will happen? 9/28/07

CS150 Introduction to Computer Science 1 8 Counters int theCounter = 0; // initialize the counter while( theCounter > 2 ) // test the counter { cout << theCounter : ; cout << theCounter << endl; theCounter += 1; // increment the counter } What will happen? 9/28/07

CS150 Introduction to Computer Science 1 9 Practice Write a snippet of code that will print all the numbers from 0 to 10000 9/28/07 CS150 Introduction to Computer Science 1 10 Let the user control the Loop Let the user determine how many times to run the loop int theCounter = 0; int maxValue;

// initialize the counter cout << How many times should we run the loop? ; cin >> maxValue; while( ) // test the counter { cout << theCounter : ; cout << theCounter << endl; // increment the counter } 9/28/07 CS150 Introduction to Computer Science 1 11 Practice

Write a snippet of code that will ask the user for a number. Print the numbers from 0 to the square of the number the user supplied. 9/28/07 CS150 Introduction to Computer Science 1 12 Running totals (5.7) How many hours did you work on assignment 1? int theCounter = 0; int days; // initialize the counter // let the user tell us how many times to loop cout << How many days did you work on assignment 1? ; cin >> days;

while( { ) // test the counter // increment the counter } 9/28/07 CS150 Introduction to Computer Science 1 13 Practice Write a snippet of code that will ask the user for a number. Print the sum of all the numbers from 0 to the number the user

supplied. 9/28/07 CS150 Introduction to Computer Science 1 14 Practice Write a snippet of code that will ask the user for a number. Print the sum of all the even numbers from 0 to the square of the number the user supplied. 9/28/07 CS150 Introduction to Computer Science 1 15

Exercise Write a snippet of code that will ask for a students exam score and then print the appropriate letter grade (A,B,C,D,F). Continue asking for exam scores and printing letter grades until the user enters a negative exam score int examScore; 9/28/07 CS150 Introduction to Computer Science 1 16

Recently Viewed Presentations

• These books include many of your favorite chapter books like Junie B. Jones, The Magic Tree House, Diary of a Wimpy Kid and many more. These books are organized with the letters FIC and the first three letters of the...
• AP Capstone Seminar Information Night September 22, 2016. AP Capstone: ... economic, environmental, historical, ethical, cultural, social and futuristic lenses that reflect interdisciplinary ... AP Seminar Classroom Grading PolicyStudents' final course average will be determined by the ...
• Communication strategies for Effective Collaboration. ... Strategies for communication breakdown: I'M upset…. Do NOT respond. Take a minute and decide WHY you feel upset. ... Use 3-Way-Communication, rephrasing it as would make more sense in the conversation before the disconnect.
• MUSIC 318 MINI-COURSE ON SPEECH AND SINGING ACOUSTICS OF SINGING Science of Sound, Chapter 17 Resonance in Singing, Miller The Science of the Singing Voice, Sundberg FORMANTS AND PITCH IN BOTH SPEECH AND SINGING, THERE IS A DIVISION OF LABOR...
• Indus Valley civilization. flourished around 2,500 B.C. Located in western part of South Asia (modern day Pakistan and western India) It is often referred to as Harappan Civilization after its first discovered city, Harappa. The nearby city of MohenjoDaro is...
• The Counterclaim paragraph. Standards require that you "acknowledge" the opposing side, but you do not . need to refute it. This brief paragraph can then be shortened to a sentence and included . in one of the earlier paragraphs, or...
• The symbols opposite represent that the product inside has been tested to British and European safety standards. These symbols that are normally applied to non-food products such as electronic products or toys. However, they may still be applied to the...
• The word "story" is a synonym of "narrative". Each narration has a narrative code - a set of methods a narrator uses to communicate directly to the reader. Constructing narratives is a key way in which meanings & pleasures are...