Practical Programming COMP153-08S

30
Practical Programming COMP153-08S Week 4: Recap, Accessing Files, Menus

description

Practical Programming COMP153-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. Recap - scope. - PowerPoint PPT Presentation

Transcript of Practical Programming COMP153-08S

Page 1: Practical Programming COMP153-08S

Practical ProgrammingCOMP153-08S

Week 4: Recap, Accessing Files, Menus

Page 2: Practical Programming COMP153-08S

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?

Page 3: Practical Programming COMP153-08S

Recap – storage locations

Page 4: Practical Programming COMP153-08S

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

Page 5: Practical Programming COMP153-08S

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

Page 6: Practical Programming COMP153-08S

Recap - Control Structures

There are 3 main control structures in programming

– Sequence

– Selection (choice)

– Iteration (repeating)

Page 7: Practical Programming COMP153-08S

Recap - Sequence

• Default control structure

• One commend follows another

Page 8: Practical Programming COMP153-08S

Recap - Selection

• Condition Action

Page 9: Practical Programming COMP153-08S

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…

Page 10: Practical Programming COMP153-08S

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

Page 11: Practical Programming COMP153-08S

Recap - IF Selection Structure

Page 12: Practical Programming COMP153-08S

Recap - IF/Else IF Selection Structure

Page 13: Practical Programming COMP153-08S

Recap - Case Selection Structure

Page 14: Practical Programming COMP153-08S

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…

Page 15: Practical Programming COMP153-08S

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.

Page 16: Practical Programming COMP153-08S

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

Page 17: Practical Programming COMP153-08S

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)

Page 18: Practical Programming COMP153-08S

• 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

Page 19: Practical Programming COMP153-08S

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?

Page 20: Practical Programming COMP153-08S

Using Files to Store Information

• Computer Programs typical use 3

types of files

– Sequential Access Files

– Random Access Files

– Binary Access Files

Page 21: Practical Programming COMP153-08S

Sequential Access

Page 22: Practical Programming COMP153-08S

Sequential Access

Page 23: Practical Programming COMP153-08S

Random Access

Page 24: Practical Programming COMP153-08S

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

Page 25: Practical Programming COMP153-08S
Page 26: Practical Programming COMP153-08S

Why Use Menus?

• Space Constrictions

• Consistency, Clarity, Ease of Use

• Speed

Page 27: Practical Programming COMP153-08S

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

Page 28: Practical Programming COMP153-08S

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

Page 29: Practical Programming COMP153-08S

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

Page 30: Practical Programming COMP153-08S

THE END

of the lecture