Welcome back to Software Development!
Due Today… Reading assignment: Clear/Unclear windows on pages 87-97
Tomorrow…
Tomorrow… Test on loops
Loops Review
Loops Review 3 types of loops:
Loops Review 3 types of loops: while
Loops Review 3 types of loops: while used when you don’t know how many times to loop
Loops Review 3 types of loops: while for used when you don’t know how many times to loop for
Loops Review 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 Review 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 Review 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 sentry variable
All Loops… Use a sentry variable Also called the loop control variable
All Loops… Use a sentry variable First: Initialize the sentry variable Also called the loop control variable First: Initialize the sentry variable
All Loops… Use a sentry variable First: Initialize the sentry variable Also called the loop control variable First: Initialize the sentry variable If you don’t, you likely won’t be able to even get into the loop
All Loops… Use a sentry variable First: Initialize the sentry variable Also called the loop control variable First: Initialize the sentry variable If you don’t, you likely won’t be able to even get into the loop Next: Check the sentry variable to see if keep looping
All Loops… Use a sentry variable First: Initialize the sentry variable Also called the loop control variable First: Initialize the sentry variable If you don’t, you likely won’t be able to even get into the loop Next: Check the sentry variable to see if keep looping Finally: Change (often increment) the sentry variable
All Loops… Use a sentry variable First: Initialize the sentry variable Also called the loop control variable First: Initialize the sentry variable If you don’t, you likely won’t be able to even get into the loop Next: Check the sentry variable to see if keep looping Finally: Change (often increment) the sentry 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 sentry variable outside and before the loop
While Loop Used when you don’t know how many times you need to loop Init the sentry variable outside and before the loop Check the sentry variable in the while statement condition
While Loop Used when you don’t know how many times you need to loop Init the sentry variable outside and before the loop Check the sentry variable in the while statement condition Change the sentry 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 sentry variable outside and before the loop Check the sentry variable in the while statement condition Change the sentry variable in the body of the while loop Often increment the sentry when simply counting
While Loop while ( check sentry ) Used when you don’t know how many times you need to loop Init the sentry variable outside and before the loop Check the sentry variable in the while statement condition Change the sentry variable in the body of the while loop Often increment the sentry when simply counting while ( check sentry )
While Loop Example int counter; // create sentry variable counter = 0; // init sentry variable while ( counter < 5 ) // loop until counter >= 5 { Console.WriteLine(“Looped {0} times.”, counter); counter++; // increment counter }
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 sentry ; Used when you *do* know how many times you need to loop for ( init the sentry ;
For Loop for ( init the sentry ; check the sentry ; Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ;
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry )
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry ) In the for statement:
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry ) In the for statement: 1st part: Init the sentry variable
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry ) In the for statement: 1st part: Init the sentry variable 2nd part: Check the sentry variable
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry ) In the for statement: 1st part: Init the sentry variable 2nd part: Check the sentry variable 3rd part: Change the sentry variable
For Loop Used when you *do* know how many times you need to loop for ( init the sentry ; check the sentry ; change the sentry ) In the for statement: 1st part: Init the sentry variable 2nd part: Check the sentry variable 3rd part: Change the sentry variable Often increment the sentry when simply counting
For Loop int counter; // create sentry variable // Loop until counter >= 5 for ( counter = 0 ; counter < 5 ; counter++ ) { Console.WriteLine(“Looped {0} times.”, counter); }
For Loop int counter; // create sentry variable // Loop until counter >= 5 for ( counter = 0 ; counter < 5 ; counter++ ) { Console.WriteLine(“Looped {0} times.”, counter); } Init sentry variable
For Loop int counter; // create sentry variable // Loop until counter >= 5 for ( counter = 0 ; counter < 5 ; counter++ ) { Console.WriteLine(“Looped {0} times.”, counter); } Init sentry variable Check sentry variable
For Loop int counter; // create sentry variable // Loop until counter >= 5 for ( counter = 0 ; counter < 5 ; counter++ ) { Console.WriteLine(“Looped {0} times.”, counter); } Init sentry variable Check sentry variable Increment sentry variable
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 sentry variable is created in the foreach statement
Foreach Loop Used to work on parts of a group, one at a time The sentry variable is created in the foreach statement The foreach automatically checks and changes the sentry variable
Foreach Loop foreach ( Used to work on parts of a group, one at a time The sentry variable is created in the foreach statement The foreach automatically checks and changes the sentry variable foreach (
Foreach Loop foreach ( create sentry Used to work on parts of a group, one at a time The sentry variable is created in the foreach statement The foreach automatically checks and changes the sentry variable foreach ( create sentry
Foreach Loop foreach ( create sentry in Used to work on parts of a group, one at a time The sentry variable is created in the foreach statement The foreach automatically checks and changes the sentry variable foreach ( create sentry in
Foreach Loop foreach ( create sentry in group to work with ) Used to work on parts of a group, one at a time The sentry variable is created in the foreach statement The foreach automatically checks and changes the sentry variable foreach ( create sentry in group to work with )
Foreach Example string myString; // create sentence variable myString = “This is a multi-word sentence”; // init sentence // Loop through the sentence word by word foreach ( string word in myString.Split() ) { Console.WriteLine(“The next word is: {0}”, word); }
Foreach Example string myString; // create sentence variable myString = “This is a multi-word sentence”; // init sentence // Loop through the sentence word by word foreach ( string word in myString.Split() ) { Console.WriteLine(“The next word is: {0}”, word); } Create sentry variable
Foreach Example string myString; // create sentence variable myString = “This is a multi-word sentence”; // init sentence // Loop through the sentence word by word foreach ( string word in myString.Split() ) { Console.WriteLine(“The next word is: {0}”, word); } Create sentry variable Automagically check and change sentry variable
Your Task
Your Task Get a multi-word sentence from the user Require that they enter a multi-word sentence Allow up to 3 tries then quit Reprint the sentence with: The first letter of every word capitalized All other letters lower case You have till the end of the period…
Existing Tasks Fix your MathGame program so that the user must enter a number.
Clear and Unclear Windows