SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Loops and Iteration in Programming

harding-raymond avatar
10 diapositives · 212 Views · Nov 27, 2014

Learn about the different types of loops used in programming, including for, while, and do/while loops. Practice examples and output analysis provided. Understand block and scope concepts within loops.

Understanding Loops and Iteration in Programming - slide 1
01 / 10Understanding Loops and Iteration in Programming
1 / 17

Télécharger la présentation

Understanding Loops and Iteration in Programming
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/Iteration • Used to repeat an action • Must have a STOP condition • Three flavors - for, while, do/while 01/05/100 1

  2. stmt before loop test ? loop body stmt following loop Anatomy of a while loop • 1 check the test • 2 if the test is true • exec the body • when the body has finished • go to step 1 • if the test is false • exit the loop int n = ?; // try n as 6 while (n >= 0) { n -= 2; cout << n << endl; } cout << “final n is “ << n << endl; The test is always a “keep going” condition. To determine the termination condition, negate the test. I.e. the loop will keep going as long as n >= 0 the loop will terminate when n becomes negative (n < 0) 01/05/100 2

  3. while Loops • The test is checked at the very beginning and then again each time after the after the entire loop body has been executed • The test is NOT checked in the middle of the loop body • This is true for all the loops (for, while, and do/while), not just the while loop x = ?; // try x as 45 while (x < 50) { x++; cout << x << endl; x++; cout << x << endl; } 01/05/100 3

  4. Practice what’s the output? int a = 20, b = 50; while (a < b) { if (a % 5 == 0) { cout << “s”; } else if (a % 3 == 0) { cout << “e”; } else { cout << “w”; } a += 4; } cout << “\n”; cout << “a’s final value is “ << a << endl; 01/05/100 4

  5. PracticeWhat’s the output? int d = 90; while (d < 80) { d ++; } cout << “d is “ << d << endl; int x = 90; while (x < 100) { x -= 5; } cout << “final value for x is “ << x << endl; 01/05/100 5

  6. Summing (even) numbers with a while loop Example of an indeterminate loop - the user’s input will determine how many times the loop executes int sum = 0, evensum = 0, number; cout << “ First number please “; cin >> number; while (number > 0) { sum += number; if (number % 2 == 0) { evensum += number; } cout << "number please "; cin >> number; } cout << "sum is " << sum << endl; cout << “sum of even #’s is “ << evensum << endl; 01/05/100 6

  7. stmt before loop test ? loop body stmt following loop Error checking with a while loopif the number is out of range, enter the while loopotherwise, skip the loopreenter loop when the user’s number continues to be out of range const int cMax = 10, cMin = 5; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; while (number < cMin || number > cMax) { cout << "error in input \n"; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; } cout << “The user entered “ << number << endl; 01/05/100 7

  8. Block/Scope • { } define a scope void main() { int n = 9; // this n belongs to main { int n = 56; // this n belongs to this block nested in main cout << n << endl; // use the inner most n } cout << n << endl; // don’t know about inner n, use n from main } this is not a good idea, having nested blocks that reuse the same variable name

  9. Scope/Blocks identifiers declared in a nested block cannot be referred to outside the block but identifiers in an outer block are accessible in a nested block void main() { int a = 5; { int x = 9; cout << “inside inner block\n”; cout << x << “ “ << a << endl; } // ILLEGAL, x is not visible in this block cout << x << “ “ << a << endl;} }

  10. Where you really use blocks • You won’t use free-standing nested or sequential blocks demo’ed in the last slides, but you do use blocks in constructs that have a pair of { } void main() { int sum = 0; while (sum < 80) { int response; cout << “Please enter a number\n”; cin >> response; sum += response; } cout << response << endl; // ILLEGAL , response is declared inside of the while loop block and cannot be used outside of the while loop cout << sum << endl; // sum can be used both in and out of the while loop }