SlideServe
Menu
Préférences
© SlideServe · v2026

Loops

zalika avatar
14 diapositives · 299 Views · Jul 31, 2014

Explore the fundamentals of programming loops in this comprehensive guide. Learn how to effectively use while loops to draw multiple lines, and avoid infinite loops by properly modifying loop conditions. Discover how to implement the constrain function to manage values, ensuring that your graphics stay within specified limits. This tutorial also delves into using for loops, including nested loops, to create dynamic shapes that interact with user inputs like mouse movements. Enhance your programming skills and bring your creative projects to life!

Loops - slide 1
01 / 14Loops
1 / 17

Télécharger la présentation

Loops
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Loops Art &Technology, 3rd SemesterAalborg University Programming https://art.moodle.aau.dk/course/view.php?id=33 David Meredith dave@create.aau.dk

  2. Reading • Chapter 6 of Shiffman, Learning Processing

  3. Doing a similar thing over and over again What if we wanted to draw 100 lines?

  4. Using a while loop while (condition) {doStuff();doMoreStuff();}

  5. Drawing 100 lines

  6. Avoid infinite loops • Variable i is never changed so remains 1 • So never gets to equal 10 • So line drawn repeatedly in the same place • And ellipse is never drawn

  7. Avoiding infinite loops • Now i is incremented on each iteration of the while loop • So i eventually gets to 10, the condition becomes false and the loop terminates • Then the ellipse can be drawn

  8. constrain(value, min, max) • The constrain(val, min, max) function returns a value that is preferably val, but • if val is greater than max, constrain returns max and • if val is less than min, constrain returns min

  9. Using a while loop with several variables • The variable i is used to control the position of each circle and is incremented on each iteration of the while loop • The variable c is used to control the shade of the circle and this is increased by 20 on each iteration • When c gets to be greater than 255, its value starts “going round the clock” again • Remember thatx % y gives the remainder after dividing x by y

  10. for loop i++ means the same as i = i + 1 i-- means the same as i = i – 1 i += 3 means the same as i = i + 3 i -= 1 means the same as i = i – 1 i *= 2 means the same as i = i * 2 i /= 2 means the same as i = i / 2 and so on…

  11. Using a for loop

  12. Scope

  13. More on scope

  14. Loops within loops • Remember that draw() is executed once on every frame • Here, each time draw() executes, an array of discs is drawn using two nested for loops • The brightness of each disc increases the closer it is to the current mouse position, so the bright spot moves around with the mouse