Data structures vb
-
Upload
nickywalters -
Category
Documents
-
view
5.139 -
download
0
Transcript of Data structures vb
1
Data structures
in VB.NET
22
Processing
Once the data has been entered into the program, we need to “do something with it”
This is the data processing part
Most of what you want to do to it has been done before – many times!
There are some standard “structures” that it is useful to know about
33
How a program runs
Variables have a space reserved in memory (they are declared and initialised)
They might be given a value
The next lines in the program or part of the program that is running are read and executed one at a time
44
Conditional statements
We might want a program to make some choices 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
55
Conditional statementsDon’t forget the End If!It is also possible to add another condition, a kind of ‘either or’ choice:
If condition ThenLine of code to execute
ElseAnother line of code if the above
condition is falseEnd If
66
Conditional statements
Whole banks of If…Then statements can be built up to test for various conditions
If there are many, this can get confusing and messy
Far better in this case to use the Select Case statement
77
Conditional statements
Syntax:Select Case variable
Case possible value or rangeLine of code to execute
Case another possible valueAnother line of code
End Select
88
Loop structures
These are used when we want lines of code to be executed many times
How many times depends on either various conditions being met or we might want it to run a set number of times
There are loops for all occasions and programmers have their favourites!
99
Loop structures
If we want the loop to run a fixed number of times, we use a For…..Next loop
This will use a variable as a counter
Syntax:For first value To last value
Line of code to executeNext value
1010
For...Next example
1111
Loop structuresIf you want the loop to carry on an unspecified number of times until a condition is met,
use a Do….While LoopBeware! 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
1212
Loop structures
Syntax: Do While value=whateverLine of code to execute
Loop
Or (my favourite):While value=whatever
Line of code executeEnd While
1313
While…End While Example
1414
Loop structuresLoops can also be nested
This is where it can get really confusing
One loop can search through one set of data
But it can be running inside another loop
This is useful where data is in a tableOne loop deals with rows
The other deals with columns
1515
Loop structures
When nesting loops, it is important to get the code looking tidy
The 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
1616
Code Example
This 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?
1717
Programming terms
In this session we have covered the three ways that programs flow
Sequence – running one line after another
Selection – using conditional statements
Iteration – using loops