Repetition
Overview Iteration 4 kinds of loops for while do while foreach Infinite Loops
Iteration One thing that computers do well is repeat commands Programmers use loops to accomplish this 4 kinds of loops in C# for loop while loop do while loop foreach loop
Criteria for loops 1.Usually have some initial condition Starting a Beginning in a certain state 2.Must have a test to continue 3.Must make progress towards finishing
Loops in Everyday Life Bad children are told to write sentences on the board I will not pour Clorox in the fish tank Have to write this sentence either A certain number of times Until the teacher is happy As many as you can during break
The for loop Good when you know exactly how many times you need to execute something Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again Initialization is where the is given a starting value The test determines whether or not to continue The increment can be any amount, including negative, and occurs after the loop statements execute
Satisfying the Teacher Example: 1000 sentences? No problem int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( I will not pour Clorox ); // Remember, ++ is the same as // = + 1
But I want them numbered! No problem int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works true 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 1 I will not pour Clorox in the Fish Tank
Why this works 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works true 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 2 I will not pour Clorox in the Fish Tank
Why this works 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works true 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 3 I will not pour Clorox in the Fish Tank
Why this works 4 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
When will it end? We see that this will go on for a while It s a little more interesting later around 1000
Why this works true 999 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works 999 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 999 I will not pour Clorox in the Fish Tank
Why this works 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works true for last time 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works (are we finished?) 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 1000 I will not pour Clorox in the Fish Tank
Why this works 1001 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );
Why this works false 1001 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); // Jump down here and continue
Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank.... 999 I will not pour Clorox in the fish tank. 1000 I will not pour Clorox in the fish tank.
The while loop Good for when you don t know how many times to repeat Teacher says Write until I m happy Has format: while (<boolean value>) { // stuff to repeat over and over
Example bool teacherhappy = false; int linenumber = 1; while (!teacherhappy) { Console.WriteLine (linenumber + I will not ); linenumber++; teacherhappy = attitudefunction ( ); // assume attitudefunction can change // teacherhappy
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );
Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank
Infinite Loops This loop isn t making a lot of progress! Loops that repeat forever are called infinite loops Apparently lock up Output: 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank.. Continue forever
Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++;
Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++;
Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++; Output: 1 I will not pour Clorox in the fish tank
Problem Solved int = 1; 2 while ( < 1000) { Console.WriteLine ( + I will not ); ++; // Remember, ++ is the same as // = + 1
Example: Re-Writing 1-1000 (using a while loop) int = 1; 2 while ( < 1000) { Console.WriteLine ( + I will not ); ++;
Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 2
Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 2 Output: 2 I will not pour Clorox in the fish tank
Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 3
How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;
How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;
How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;
Problem Solved int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++; Output: 999 I will not pour Clorox in the fish tank
How does it end? int = 1; while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++;
How does it end? int = 1; while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++;
How does it end? int = 1; now false while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++; // So we never print out // 1000 I will not pour Clorox in the fishtank
Another Problem Solved int = 1; now true while ( <= 1000) { 1000 Console.WriteLine ( + I will not ); ++;
The do-while loop Similar to while loop Must execute at least one time (test is at bottom) Has format: do { while (<boolean value>);
Example (count from 1 to 3) int = 0; 0 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 0 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3); Output: 1
Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3); Output: 2
Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3);
Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); // Note: is now 3, but we still have // to finish out the loop it doesn t skip
Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); Output: 3
Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); now false, so loop is finished
The foreach loop Often we have a collection of data We d like to do something to each one of the items in the collection Certainly we can do this with a for loop But C# provides a new loop foreach References an item in the collection Provides access to it Loops through all elements in the collection
foreach template foreach (<TYPE> <NAME> in <COLLECTION>) { WORK TO BE DONE ON EACH ITEM
A sample application
foreach example Assuming we had a listbox control called listboxnames with strings in it foreach (string currentname in listboxnames.items) { MessageBox.Show(currentName.ToUpper()); Displays all of the strings in capitalization to the user in MessageBoxes.
foreach vs. for loop The previous foreach loop on the collection using a for loop: for(int i=0; i < listboxnames.items.count; i++) { string currentname = (string)(listboxnames.items[i]); MessageBox.Show(currentName.ToUpper());
Summary for loops good for when you know how many times you want to repeat while and do-while good for when you don t foreach loop useful for processing collections All loops must finish, or they become infinite loops All loops must have a test to continue, or they become infinite loops