550 likes | 829 Views
D101 Software Development. Loops. Content. Review (Last weeks stuff) Brackets While Loop Do While Loop For Loop For Each Loop. Learning Outcomes. Describe the uses of the four (4) different sets of brackets used in C# Implement the Thread Sleep method Use a breakpoint
E N D
Content • Review (Last weeks stuff) • Brackets • While Loop • Do While Loop • For Loop • For Each Loop
Learning Outcomes • Describe the uses of the four (4) different sets of brackets used in C# • Implement the Thread Sleep method • Use a breakpoint • Implement a while loop • Implement a do while loop • Implement a for loop • Implement a for each loop
Review (last weeks stuff) • Arrays Application
Review • Add Days button
Review • Clear (Days) button • Clear (Months) button
Review • Move >> button • Show Selected Day button
Review • AddMonthsbutton
Review • Show Selected Month button
Review • First & Last (items in the listbox)
Brackets Curly, Curvy, Square and Triangle
Brackets • In C# we work with four (4) different sets of brackets • Curly { } • Curvy ( ) • Square [ ] • Triangle < > • Each set have different meanings and can only be used in certain situations
Curly Brackets { } Curly { } Brackets always line up vertically Except for Arrays
Curvy Brackets ( ) Curvy ( ) Brackets are used for Methods And conditions
Square Brackets [ ] Empty only when declaring, otherwise always incase an int [index] Square [ ] Brackets are used for Arrays
Triangle Brackets < > Triangle < > Brackets are used individually as greater than or less than symbols
Thread Sleep Pauses the execution of code for a certain period of time
Thread Sleep • In order to help us see what happens with loops we’re going to use the Thread Sleep method • Thread Sleep pauses the execution of code for a given number of milliseconds • To enable this method we need to add the Threading name space
Threading Namespace • Add to the top of the Form1.cs class • This lets us use all the predefined Threading code
Thread Sleep • After the threading namespace has been added we can use the Thread Sleep method to pause code execution 1000 ms = 1 sec
While Loop While (condition is true) { //do something over and over }
While Loop • Sometimes in code we want to repeat the same instruction over and over until we reach a desired result • E.g. keep increasing the width of a picture box by 10 pixels until its width is greater than 200 pixels • We can achieve this using a loop • A while loop keeps repeating the same codewhile a condition is true
While Loop The condition is re-evaluated every time through the loop • Syntax:while(condition){//code to repeat} • Looks similar to an if statement • Major difference is the code is repeated until the condition becomes false
While Loop • Repeats increasing the picture box width by 10 until the width grows bigger than 200
Introducing the Breakpoint • Clicking in the column to the left of your code will toggle a breakpoint • Breakpoint show up as Red Dots • Breakpoints will break the execution of code until the ‘Go’ button is repressed • This is very helpful when trying to figure out what your code is doing
Introducing the Breakpoint • Breakpoints
Introducing the Breakpoint • Breakpoints (during code execution)
Introducing the Breakpoint • The Locals Window (during code execution)
Introducing the Breakpoint • Breakpoints (during code execution) • Mouse hover will show more information
While Loop • The code inside a while loop should change something to do with the condition • If the loop code doesn’t change anything to do with the condition an infinite loop can occur • An infinite loop will crash your application and maybe even the whole computer
Infinite Loop Bad • Loop will never end
While Loop (with an Array) • Rememberfrom lastweek 12 LOCWhat if it was adding the students in this class?
While Loop (with an Array) • While loop 4 LOCRegardless of whether the Array is 12 or 12,000
While Loop (with an Array) • A closer look • Declare and initialise an int called cnt (counter) • cnt will be used as the Array index • While cnt (the counter) is less than 12 • Add month cntto the combo box • Increasecntby 1
While Loop (with an Array) • A slightly better way If the number of elements in the Array changes, the code in loop will still work (i.e. the loop limit isn’t hard coded) Use Array Length
While Loop VS Do While Loop • The While Loop checks the condition at the start of each code loop • Therefore, a While Loop is a pre-test loop • The Do While Loop checks the condition at the end of each code loop • Therefore, a Do While Loop is a post-test loop
Do While Loop Do { //some code } while (a condition is true)
Do While Loop • Syntax:do{//code to repeat}while(condition); • The code will always be executed at least once Note the semicolon
Do While Loop • Code Example: • Width will increase by 10 pixels every button click even after the 200 pixel limit is reached
For Loop for(inti = 0; i < aNumber; i++) { //do some code }
For Loop • When looping through Array’s the For Loop is the most commonly used • The For Loop is a pre-test loop • Syntax:for (inti = 0; i < limit; i++){//code to repeat}
For Loop • for (inti = 0; i < limit; i++) • inti = 0 • This declares and initialises an inti to zero to use as a counter • i < limit • This is the condition, it can be any valid condition involving i • i++ • This is what happens to the counter (i) at the end of each loop before the condition is re-checked
For Loop • Code Example: 2 LOCRegardless of whether the Array is 12 or 12,000
For Loop • In most for loops the count integer is usually called i (stands for index) • However, you do not have to call it i
For Loop VS While Loop • Two different ways to code the same thing
For Each Loop For each (object oneObj in anArrayOfObject) { //do something }
For Each Loop • The For Each Loop is a condensed version of the for loop • It is specifically used for repeating the same operation on each member of an array • Syntax:foreach (classoneObjinarrayOfObjects){//code to do for each object}
For Each Loop • Code Example:
For Each Loop VS For Loop • Another way to write the same code
Introducing the Tab Control If you thought of the T.A.B. before your thought of a tab you should visithttp://www.gamblingproblem.co.nz/