Practical Programming COMP153-08S
description
Transcript of Practical Programming COMP153-08S
Practical ProgrammingCOMP153-08S
Week 4: Recap, Accessing Files, Menus
A Recap on the first 4 weeks
• What is a control?
• What is a property?
• What is an event?
• How is data stored in our programs?
• Why do we have different data types?
Recap – storage locations
Leaving messages
– Beside your computer• information just for you
– On your refrigerator• Share with small group
– University Noticeboard• Share information with
Uni students & staff
Recap - scope
Scope in VB programming
Local variable– Beside your computer
– For use only inside one procedure
– Use Dim to declare
Form-level variable– the flat fridge
– For use by more than one procedure
– Use Private to declare
– Declare at top of form
Recap - Control Structures
There are 3 main control structures in programming
– Sequence
– Selection (choice)
– Iteration (repeating)
Recap - Sequence
• Default control structure
• One commend follows another
Recap - Selection
• Condition Action
Recap - Selection
But sometimes expressions can be ambiguous
Son, please go to the shop and buy us 4 apples and 3 pears or 3 bananas
In English we can use higher-level knowledge,
pauses in speech, intonation, etc…
• How can we do this in programming?
By defining Rules of Precedence…
1. Exponential e.g. 23
2. Negation e.g. − -3
3. Multiplication and Division, e.g. C * A , A / C
4. Integer Division (just returns an integer) e.g. \
5. Modulus arithmetic (just returns remainder) e.g. Mod
6. Addition and Subtraction
7. Concatenation (the joining of strings) e.g. “Te” & “ ” & “Taka”
8. Equal to, greater than etc e.g. =, >, <, =<, =>, <>
9. Not
10. And, AndAlso
11. Or, OrElse
12. Xor
Arithmetic, comparison & logical operators
Recap - IF Selection Structure
Recap - IF/Else IF Selection Structure
Recap - Case Selection Structure
Which Selection Structure is Best?
• 2 statements…
– If condition THEN action1 Else action2 …EndIf
– Select Case expression …End Select
• Use the If Selection Structure when the expression has
only 1 or a small number of possible outcomes
• Use the Case Selection Structure to handle many
outcomes requiring different actions for each value…
Recap - Repetition
• To prepare the pancakes sift the flour and baking powder into a large bowl. Make a well in the centre. Combine the first egg, milk and oil and stir into the flour. Repeat this for second egg. Mix until a smooth batter is formed, then pour into a jug. Brush a heated pancake tin with oil. Pour in enough of the batter to thinly cover the base of the pan. Cook for 2-3 minutes until the mixture sets. Turn over using a metal spatula and cook for a further 1-2 minutes. Remove and place on a plate and cover with a square of greaseproof paper. Continue until all the batter has been used. Keep the pancakes warm.
Two Types of Loops
• Counter Controlled– Performs the loop for a certain count
• Sentinel Controlled – Keep looping until a value or set of values is
reached
Recap - Repetition Structure
For counter = start To end [Step value][actions]
Next counter
Do While|Until condition (Pre-test)[actions]
Loop
Do[actions]
Loop While|Until condition (Post-test)
• Count out the 100 sit-ups – For NumberOfSitups = 1 to 100 [Step 1]
do the situpNext NumberOfSitups
• While you have energy do sit-ups– Do While Energy = good
do the situpLoop
• Do sit-ups until you are exhausted– Do
do the situpLoop Until Energy = exhausted
Recap - Repetition Structure
Memory
Currently we have used variables and form
controls to store information
What if we want to access some information after
the program has shut down?
What if we want to share the information with
other programs, or other users?
Using Files to Store Information
• Computer Programs typical use 3
types of files
– Sequential Access Files
– Random Access Files
– Binary Access Files
Sequential Access
Sequential Access
Random Access
Sequential Access Files
• Information is stored serially, or in sequence
• Must be accessed from start to finish
• Information is usually stored as text, hence often
called text files
• Three separate processes– Reading from a file
– Writing to a file
– Appending to a file
Why Use Menus?
• Space Constrictions
• Consistency, Clarity, Ease of Use
• Speed
Adding Menus to the interface
1. Locate the MainMenu Control from the toolbox and drag it onto the form
2. Click on Type Here and type in the name of the first menu
3. Add further Menu Items underneath and to the side of the first menu item
4. Adjust the text properties of the Menu Items
5. Create sub procedures that are activated by the Menu Items
Access Keys
• These allow commands to be entered without using the mouse
• Very simple to code– Place an & symbol in the text property of the button
• Very simple to use– Alt + letter
Shortcut Keys
• These are similar to access keys but are used in menu items
• Very simple to code– In the Short Cut property of the Menu Item select
the Shortcut Sequence– Best to select Ctl and a letter
• Very simple to use– Clt + letter
THE END
of the lecture