Data structures vb

6,111 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
6,111
On SlideShare
0
From Embeds
0
Number of Embeds
4,006
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Data structures vb

  1. 1. Data structures in VB.NET 1
  2. 2. ProcessingOnce the data has been entered into theprogram, we need to “do something with it”This is the data processing partMost of what you want to do to it has beendone before – many times!There are some standard “structures” that itis useful to know about 2
  3. 3. How a program runsVariables have a space reserved inmemory (they are declared and initialised)They might be given a valueThe next lines in the program or part ofthe program that is running are read andexecuted one at a time 3
  4. 4. Conditional statementsWe might want a program to make somechoices based on the data it receives e.g. If intAge > 17 then…..The full syntax in VB is: If condition Then Line of code to execute End If 4
  5. 5. Conditional statementsDon‟t forget the End If!It is also possible to add another condition,a kind of „either or‟ choice: If condition Then Line of code to execute Else Another line of code if the above condition is false End If 5
  6. 6. Conditional statementsWhole banks of If…Then statements can bebuilt up to test for various conditionsIf there are many, this can get confusing andmessyFar better in this case to use the Select Casestatement 6
  7. 7. Conditional statementsSyntax: Select Case variable Case possible value or range Line of code to execute Case another possible value Another line of code End Select 7
  8. 8. Loop structuresThese are used when we want lines of code tobe executed many timesHow many times depends on either variousconditions being met or we might want it torun a set number of timesThere are loops for all occasions andprogrammers have their favourites! 8
  9. 9. Loop structuresIf we want the loop to run a fixed number oftimes, we use a For…..Next loopThis will use a variable as a counterSyntax: For first value To last value Line of code to execute Next value 9
  10. 10. For...Next example 10
  11. 11. Loop structuresIf you want the loop to carry on an unspecifiednumber of times until a condition is met,use a Do….While Loop Beware! It is possible to create loops that carry on to infinity Infinite loops aren‟t the end of world – the computer may crash so you might lose some unsaved work Pressing Ctrl + Break sometimes works to stop it 11
  12. 12. Loop structuresSyntax: Do While value=whatever Line of code to execute LoopOr (my favourite): While value=whatever Line of code execute End While 12
  13. 13. While…End While Example 13
  14. 14. Loop structuresLoops can also be nestedThis is where it can get really confusingOne loop can search through one set of dataBut it can be running inside another loopThis is useful where data is in a table One loop deals with rows The other deals with columns 14
  15. 15. Loop structuresWhen nesting loops, it is important to get thecode looking tidyThe best way to do this is with indentation The section of code after the first line is tabbed in The last line is tabbed out 15
  16. 16. Code ExampleThis is a neat piece of code (of course, I wrote it )It contains two loops – an inner and an outerCan you try to work out what it does? 16
  17. 17. Programming termsIn this session we have covered the threeways that programs flow Sequence – running one line after another Selection – using conditional statements Iteration – using loops 17

×