1 / 5

Mastering Loops in Programming: From Basics to Challenges

Learn about loop designing concepts like while and for loops, error debugging, nested loops, and practical exercises in 2D arrays. Understand count-controlled vs. sentinel-based looping strategies for efficient coding. Discover common errors like off-by-one and infinite loops, and practice with exercises on drawing shapes. Advance your programming skills with loop intricacies and challenges.

Download Presentation

Mastering Loops in Programming: From Basics to Challenges

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. Designing Loops 3.4

  2. Repeat “numTimes” many times • while loop intcount = 0; while(count < numTimes) { //execute statement(s) count++; } • for loop equivalent for(int i = 0; i < numTimes; i++) //execute statement(s)

  3. Ending a Loop • List headed by size (known number of iterations) – count-controlled loop • Ask before iterating (would you like to continue? (y/n)) • List ended with sentinel value – see example from yesterday • Running out of input – used when getting input from files

  4. Debugging Loops • Off by one errors • Infinite loops • Localize the problem • Trace variables

  5. Nested for loops • Used to process 2 dimensional data • Especially 2-D arrays which are discussed in APCS and CMB • Exercise 1: Draw an nxn square that looks like – * * * * * * * * * • Exercise 2: Draw a triangle with base n that looks like – * ** *** **** • Challenge: Draw a triangle with base n that looks like – * ** *** ****

More Related