Welcome back to Software Development!
Loops
Loops 3 types of loops:
Loops 3 types of loops: while
Loops 3 types of loops: while used when you don’t know how many times to loop
Loops 3 types of loops: while for used when you don’t know how many times to loop for
Loops 3 types of loops: while for used when you don’t know how many times to loop for used when you do know how many times to loop
Loops 3 types of loops: while for foreach used when you don’t know how many times to loop for used when you do know how many times to loop foreach
Loops 3 types of loops: while for foreach used when you don’t know how many times to loop for used when you do know how many times to loop foreach used to work on parts of a group, one at a time
All Loops…
All Loops… Use a loop control variable
All Loops… Use a loop control variable First: Initialize the loop control variable
All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop
All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping
All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable
All Loops… Use a loop control variable First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable If you don’t, you won’t be able to exit the loop…infinite loop
While Loop
While Loop Used when you don’t know how many times you need to loop
While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop
While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition
While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop
While Loop Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting
While Loop while ( check loop control variable) Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting while ( check loop control variable)
While Loop Example ArrayList myList = new ArrayList(); // create the list myList.Add(4); // fill the list int position; // create loop control variable position = 0; // init loop control variable while (position < myList.Count ) // loop until thru the list { Console.WriteLine(“Next #: ”, myList[position]); position++; // increment counter }
Task Write a program using a while loop to print the first 5 integers Name the project “loops” Display each number on its own line with “Next #: Before the loop, display the message “While loop”
For Loop
For Loop Used when you *do* know how many times you need to loop
For Loop Used when you *do* know how many times you need to loop for (
For Loop for ( init the loop control variable ; Used when you *do* know how many times you need to loop for ( init the loop control variable ;
For Loop for ( init ; check the loop control variable ; Used when you *do* know how many times you need to loop for ( init ; check the loop control variable ;
For Loop for ( init ; check ; change the loop control variable) Used when you *do* know how many times you need to loop for ( init ; check ; change the loop control variable)
For Loop for ( init ; check ; change ) Used when you *do* know how many times you need to loop for ( init ; check ; change )
For Loop
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( { Console.WriteLine(“Looped” + counter + “ times.”); }
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); }
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable
For Loop ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); }
Task Add to your “loops” program: After your while loop… Add a for loop to print the first 5 integers Display each number on its own line with “Next #: Before the loop, display the message “For loop”
Foreach Loop
Foreach Loop Used to work on parts of a group, one at a time
Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement
Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable
Foreach Loop foreach ( Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach (
Foreach Loop foreach ( create loop control variable Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable
Foreach Loop foreach ( create loop control variable in Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in
Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in group to work with )
Foreach Example
Foreach Example ArrayList myList = new ArrayList(); // create list
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( ) { … }
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … }
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … } Create loop control variable
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in { … } Create loop control variable
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable Automagically check & change loop control variable
Foreach Example ArrayList myList = new ArrayList(); // create list myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { if ( numFmList == matchNum ) … }
Task Add to your “loops” program: After your for loop… Create an ArrayList and fill it with the first 5 integers Use a foreach loop to print the first 5 integers from the ArrayList Display each number on its own line with “Next #: Before the loop, display the message “Foreach loop”
Clear and Unclear Windows