Data structures vb

17
1 Data structures in VB.NET

Transcript of Data structures vb

Page 1: Data structures vb

1

Data structures

in VB.NET

Page 2: Data structures vb

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

Page 3: Data structures vb

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

Page 4: Data structures vb

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

Page 5: Data structures vb

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

Page 6: Data structures vb

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

Page 7: Data structures vb

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

Page 8: Data structures vb

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!

Page 9: Data structures vb

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

Page 10: Data structures vb

1010

For...Next example

Page 11: Data structures vb

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

Page 12: Data structures vb

1212

Loop structures

Syntax: Do While value=whateverLine of code to execute

Loop

Or (my favourite):While value=whatever

Line of code executeEnd While

Page 13: Data structures vb

1313

While…End While Example

Page 14: Data structures vb

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

Page 15: Data structures vb

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

Page 16: Data structures vb

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?

Page 17: Data structures vb

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