Title: Loop Review Arrays 1 dimension Control Arrays User Defined Types Loops
1Loop ReviewArrays (1 dimension) Control
Arrays User Defined TypesLoops Arrays
FOR NEXT Case Statements
chapter
EIGHT
2Loop Review
- Given the pseudocode segment below
- FOR intLoop 1 to 10
- ADD intLoop to mintTotal
- NEXT intloop
- Diagram the logic (include the code done
automatically by the computer) - What is the value of intLoop after the code
executes? mintTotal? - How many times does the ADD statement execute?
3Loop Review
- Given the pseudocode segment below
- intLoop 1
- DO WHILE intLoop lt 10
- ADD intLoop to mintTotal
- ADD 1 to
intLoop - LOOP
- Diagram the logic (include the code done
automatically by the computer) - What is the value of intLoop after the code
executes? mintTotal? - How many times does the first ADD statement
execute?
4Loop Review
- Given the pseudocode segment below
- intLoop 1
- DO
- ADD intLoop to mintTotal
- ADD 1 to
intLoop - LOOP WHILE intLoop lt 10
- Diagram the logic (include the code done
automatically by the computer) - What is the value of intLoop after the code
executes? mintTotal? - How many times does the first ADD statement
execute?
5Loop Exercise
- Assume you were writing a simple computer game.
The computer picks a number between 1 and 100 and
you have to guess it. Flowchart the logic
necessary be sure to think about what kind of
loop control you would use. How would your logic
change if you were to allow the user only 5
guesses?
6Arrays
- Data structures are logical ways to store data.
They may be described as fixed or dynamic. - One type of data structure is an array. Arrays
are multiple occurrences of the same type of
variable called data elements. - Arrays use the concept of indexing and subscripts
to access a specific data element. - Arrays are declared with a DIM statement which
sets the dimension of the array (How many data
elements are in the array)
7Arrays
What are some of the rules - implied by the
DIM statement - about subscripts
8Arrays
- Provide the appropriate DIM statement to create
an array to allow for the following - you wish to store up to 20 names in your program
each name is no more than 35 characters. - you wish to store 100 days worth of temperature
information that could appear in the form of
999.99 (i.e. 102.90 degrees). - you wish to store the correct answers to 100
questions that are true false.
9Loops Arrays
- Given that you want to get each element of an
array (intRayGrades) and add it to the total
(mintTotal), then find the average grade, what do
you need to do?
10Loops Arrays
- Given that you want to get each element of an
array (intRayGrades) and add it to the total
(mintTotal), then find the average grade, what do
you need to do?
Generic Algorithm Steps before the loop Loop
steps in the loop End loop Steps after the loop
11Loops Arrays
- Given that you want to get each element of an
array (intRayGrades) and add it to the total
(mintTotal), then find the average grade, what do
you need to do?
Generic Algorithm Steps before the loop Loop
steps in the loop End loop Steps after the loop
VB Code (FOR NEXT Construct) DIM intRayGrades (5)
As Integer DIM sngAverage as Single FOR intCount
0 TO 5 mintTotal mintTotal _
intRayGrades(intCount) NEXT intCount sngAverage
mintTotal / 6 or mintTotal / (uBound(intRayGrade
s())1) Printer.Print FormatNumber(sngAverage,2)
12Control Arrays
- You may create arrays of screen and program
Controls at design time. - Use CTRL-C and CTRL-V
- Use same name and answer prompt with Yes
- you can see the subscripts in the properties
- Control arrays share events i.e. Click
optColor(0) optColor(1) optColor(2)
Option 1
Option 2
Option 3
13CASE Statement
- Another way to allow for complex nested ifs.
- Format
SELECT CASE Expression CASE constant, relation,
numeric or string statements
CASE constant, relation, numeric or string
statements ... CASE ELSE
statements END SELECT
Use the IS in the relation
14CASE Statement
- Another way to allow for complex nested ifs.
SELECT CASE True CASE optColor(0).value
lblFormColor vbBlack CASE
optColor(1).value lblFormColor
vbRed CASE optColor(2).value
lblFormColor vbWhite CASE ELSE
lblMessage.text No Selection END SELECT
IF optColor(0).value 1 then lblFormColor
vbBlack Else If optColor(1).value 1 then
lblFormColor vbRed Else If optColor(2).value
1 then lblFormColor vbWhite Else
lblMessage.text No Selection End If
15CASE Statement
- Assume that the variable INDEX receives an
integer value identifying which option button was
chosen.
PRIVATE SUB optColor_Click(Index as
Integer) SELECT CASE Index CASE 0
lblFormColor vbBlack CASE 1
lblFormColor vbRed CASE 2
lblFormColor vbWhite CASE ELSE
lblMessage.text No Selection END SELECT END
SUB
16User Defined Types
- Sometimes we want to store information that is
related to each other like names and grades
below. Remember though that the data elements in
an array must be the same type. How can we store
this information?
Name Grade
17User Defined Types
- Since there is no fundamental data type that can
handle this, we have to create our own data type.
These creations have many names user defined
types, composite data types, records. We build
our data types with the TYPE statement
Name Grade
TYPE NameGrade strName as String intGrade
as Integer END TYPE DIM udtClass(5) as NameGrade
18User Defined Types
- How do we gain access to the data elements?
TYPE NameGrade strName as String intGrade
as Integer END TYPE DIM udtClass (5) as NameGrade
How do we refer to Nancy? What is in
udtClass(5).intGrade? What data type is it? What
is in udtClass(1)? What data type is it?
udtClass(2).strName
88
Integer
Bob, 80
NameGrade
19User Defined Types
- How do we gain access to the data elements?
TYPE NameGrade strName as String intGrade
as Integer END TYPE DIM udtClass (5) as NameGrade
Create a loop to go through udtClass and find out
the grade for BOB. Be ready to discuss the type
of loop you choose and any special logic
processing you added.
20User Defined Type Practice(homework for 11/9)
- Provide appropriate user defined types and DIM
statements to allow for the following - you wish to store a customer name, with a
one-line street address, a zip code and a
telephone number for up to 20 customers in your
program each name is no more than 35 characters. - you wish to store temperature information that
could appear in the form of 999.99 (i.e. 102.90
degrees). Allow for temperatures at 8 am, 12
noon, and 8 pm for each of 100 days - you have been asked to write a program to store
5-answer multiple choice questions, the correct
answer, and the difficulty level for each of the
100 questions.
21Review
- Arrays
- a data structure to store multiple occurrences of
same type data - have to use indices to access the data
- use loops to process arrays (For Next / For Each)
- Data Types
- create your own data types - parallel arrays
- Case Statements
- efficiently replaces NESTED IFs not multiple IFs
22Extra Credit (10 Points)
- Create a program that
- creates a list of ten numbers between 1 and 100
- displays them on the screen
- then upon pressing a sort command, displays the
numbers from lowest to highest on the screen - Youre not allowed to use the Sort property on a
list or combo list!! - requirements
- use at least two arrays use at least two
different types of loops - have a print button that prints out both lists
- use the RANDOMIZE and RND function to create the
original list - Due November 9, 2000 in class
- Grades are 0, 5 or 10 points
23Randomize and Rnd
- RND is a function that returns a value of data
type single between 0 and 1.00 - RANDOMIZE is a command that simply lets the RND
function produce pseudo-random numbers. - For the extra credit problem, you only will use
the RANDOMIZE command once, the RND will be in a
loop and should be executed at least 10 times.