Download presentation
Presentation is loading. Please wait.
Published byArnold Gardner Modified over 8 years ago
1
National Diploma Unit 4 Introduction to Software Development Data Structures – Loops and selection
2
Data Processing Once data is entered we need to do something with it – this is data processing Most programming you will do has been done many times before by others – there is no need to ‘reinvent the wheel’ You can use some standard ‘structures’ to do common tasks
3
How a program runs Each variable has its own space reserved in memory once it is declared – Dim intcapacity as Integer We give this variable a value by initialising – intcapacity = 50000 The next lines in the program are then read and executed one line at a time
4
Common statements Using common, pre-formatted statements which perform set actions save us from having to design programs ‘from scratch’ Today we will look at: Conditional statements – what we do depends on what the situation is Using loops – to repeat actions over and over until we ask VB to stop
5
Conditional statements Use these when you want the program to make a choice based on data it gets if age is over 17 issue driving license The syntax in VB is: If Condition Then Line of code to process data End If - don’t forget to end your If!
6
Further conditions We can add another condition to give us 2 or more choices – an else statement If condition Then Line of code which does something Else Another line which executes if the first condition is false End If
7
Select case statements We could use a whole bank of ifs and elses to sort out complex data processing – but this would be confusing and messy! It’s much better to use the Select Case statement instead
8
Select case statements The syntax for select case is: Select Case variable Case first possibility Line of code to execute Case second possibility Line of code to execute instead End Select
9
Loop structures Use these when you want a line of code to be executed many times How many times depends on: Whether set conditions are met OR You can set a fixed number of times Loops are very versatile – get to know them and they will save you time
10
For…..Next loops Use this if you want to run a loop a fixed number of times It needs a variable to use as a counter to track the number of repeats Syntax of For…Next is: For first value To last value Line of code to execute Next value
11
Do …..while loops If you don’t know in advance how many times the loop will have to run, use a Do…While loop Be careful that you don’t create an infinite loop – it will run forever and crash your PC!
12
Do….While loop structure The syntax for do…while loop is: Do While value = whatever Line of code to execute Loop Or, alternatively While value = whatever Line of code to execute Wend
13
Writing Loops It’s important to get your code looking tidy so that you – and others can read it easily It will also make errors easier to spot Use indentation for this After the loop body begins press Tab to make the loop body inset At the last line remove the Tab
14
Example of good layout For Count = 1 to 10 Age = Age + 1 Form.Print “Age is ”; Age Next Count
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.