1 / 55

Loops

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

verlee
Télécharger la présentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. D101 Software Development Loops

  2. Content • Review (Last weeks stuff) • Brackets • While Loop • Do While Loop • For Loop • For Each Loop

  3. 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

  4. Review (last weeks stuff) • Arrays Application

  5. Review • Add Days button

  6. Review • Clear (Days) button • Clear (Months) button

  7. Review • Move >> button • Show Selected Day button

  8. Review • AddMonthsbutton

  9. Review • Show Selected Month button

  10. Review • First & Last (items in the listbox)

  11. Brackets Curly, Curvy, Square and Triangle

  12. 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

  13. Curly Brackets { } Curly { } Brackets always line up vertically Except for Arrays

  14. Curvy Brackets ( ) Curvy ( ) Brackets are used for Methods And conditions

  15. Square Brackets [ ] Empty only when declaring, otherwise always incase an int [index] Square [ ] Brackets are used for Arrays

  16. Triangle Brackets < > Triangle < > Brackets are used individually as greater than or less than symbols

  17. Thread Sleep Pauses the execution of code for a certain period of time

  18. 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

  19. Threading Namespace • Add to the top of the Form1.cs class • This lets us use all the predefined Threading code

  20. Thread Sleep • After the threading namespace has been added we can use the Thread Sleep method to pause code execution 1000 ms = 1 sec

  21. While Loop While (condition is true) { //do something over and over }

  22. 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

  23. 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

  24. While Loop • Repeats increasing the picture box width by 10 until the width grows bigger than 200

  25. 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

  26. Introducing the Breakpoint • Breakpoints

  27. Introducing the Breakpoint • Breakpoints (during code execution)

  28. Introducing the Breakpoint • The Locals Window (during code execution)

  29. Introducing the Breakpoint • Breakpoints (during code execution) • Mouse hover will show more information

  30. 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

  31. Infinite Loop Bad • Loop will never end

  32. While Loop (with an Array) • Rememberfrom lastweek 12 LOCWhat if it was adding the students in this class?

  33. While Loop (with an Array) • While loop 4 LOCRegardless of whether the Array is 12 or 12,000

  34. 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

  35. 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

  36. 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

  37. Do While Loop Do { //some code } while (a condition is true)

  38. Do While Loop • Syntax:do{//code to repeat}while(condition); • The code will always be executed at least once Note the semicolon

  39. Do While Loop • Code Example: • Width will increase by 10 pixels every button click even after the 200 pixel limit is reached

  40. For Loop for(inti = 0; i < aNumber; i++) { //do some code }

  41. 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}

  42. 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

  43. For Loop • Code Example: 2 LOCRegardless of whether the Array is 12 or 12,000

  44. 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

  45. For Loop VS While Loop • Two different ways to code the same thing

  46. For Each Loop For each (object oneObj in anArrayOfObject) { //do something }

  47. 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}

  48. For Each Loop • Code Example:

  49. For Each Loop VS For Loop • Another way to write the same code

  50. 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/

More Related