1 / 11

Nested Loops

Nested Loops. Last Time. We Introduced the do/while repetition structure Today we will Examine the process of nesting repetition structures. Nested Loops. A loop within a loop Can repeat multiple things within a loop Example: Read in 10 grades for each of 20 students

tobina
Download Presentation

Nested Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nested Loops CS150 Introduction to Computer Science 1

  2. Last Time • We • Introduced the do/while repetition structure • Today we will • Examine the process of nesting repetition structures CS150 Introduction to Computer Science 1

  3. Nested Loops • A loop within a loop • Can repeat multiple things within a loop • Example: Read in 10 grades for each of 20 students • How would we write loops to do this? CS150 Introduction to Computer Science 1

  4. What is the Output? for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) cout << “*”; cout << endl; } CS150 Introduction to Computer Science 1

  5. What is the Output? for (int i = m; i > 0; i--) { for (int j = n; j > 0; j--) cout << “*”; cout << endl; } CS150 Introduction to Computer Science 1

  6. What is the Output? cout << setw(4) << "|"; for( int head = 0; head <= 10; head++ ) cout << setw(3) << head; cout << endl; for( int i=0; i< 3*12+1; i++ ) cout << "-"; cout << endl; for( int row = 0; row <= 10; row ++ ) { cout << setw(3) << row << "|"; for( int col = 0; col <= 10; col++ ) cout << setw(3) << row * col; cout << endl; } CS150 Introduction to Computer Science 1

  7. Problem • Write a program that outputs a rectangle of stars for any inputted width and height CS150 Introduction to Computer Science 1

  8. Problem • Write the nested ‘for’ loops that output the following: * ** *** **** ***** CS150 Introduction to Computer Science 1

  9. Problem • Write a program to play the stones game. There are 13 stones and 2 players alternate taking 1, 2 or 3 stones per turn. The winner is the player to remove the last stone. The program should print out (using *’s) a picture of how many stones are left each turn. CS150 Introduction to Computer Science 1

  10. Notes on Previous Assignment • Do not use magic constants • Make sure to test your program well • Use data that are likely to cause problems • You can’t test for everything but you can predict problematic input • Write efficient code • Don’t have superfluous selection or repetition structures • Print out code, follow coding standards, submit complete documentation CS150 Introduction to Computer Science 1

  11. Summary • In today’s lecture we covered • Nested Loops • Readings • We have completed chapters 1 and 2 CS150 Introduction to Computer Science 1

More Related