Procedures cont and Loops - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Procedures cont and Loops

Description:

Output 'Player choice: Rock' to lstOutput. choice = Generate random number between 1 and 3 If ... Output 'TIE GAME!' to list box. ElseIf (choice = 2) Then ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 24
Provided by: mil94
Category:
Tags: cont | loops | procedures | tie

less

Transcript and Presenter's Notes

Title: Procedures cont and Loops


1
Procedures (cont) and Loops
  • CS0004
  • Chapter 4, 6.1, and 6.3

2
Administration
  • Graded First Part of Project
  • Get them after class
  • Will review design
  • Extended Final Project Deadline
  • Until next Wednesday (Feb 7th)
  • Will send email about submitting project
  • Questions?

3
Project 1 (interface)
ListBox
4
Project 1 (psuedocode)
  • NOTE 1 Rock 2 Paper 3 Scissors
  • Event Rock_Click
  • Clear items in lstOutput
  • Output "Player choice Rock" to lstOutput
  • choice Generate random number between 1 and
    3 If (choice 1) Then
  • Output "Computer choice Rock" to lstOutput
  • Output "TIE GAME!" to list box
  • ElseIf (choice 2) Then
  • Output "Computer choice Paper" to lstOutput
  • Output "COMPUTER WON!" to lstOutput
  • Else
  • Output "Computer choice Scissors" to
    lstOutput
  • Output "PLAYER WON!" to lstOutput
  • End If
  • Output "Click any button to play again!"

5
Project 1 (psuedocode)
  • Need to define it for other events
  • Paper Button Click
  • Scissors Button Click
  • Should follow from the rock button click

6
Review
  • What are parameters?
  • What is the difference between ByVal and ByRef?
  • What is the difference between a sub-procedure
    and a function?
  • Why do we have procedures and functions?

7
Modular Design
  • Save on typing
  • Break big tasks into small procedures and
    functions
  • Keep from making errors (easy to test)
  • Examples?

8
Lets try it out
  • Design a program to convert Fahrenheit to Celsius
  • Use a function to compute the conversion
  • Use a sub procedure to display the result

9
Loops
  • Many, Many times you want to repeat the same
    piece of code over and over again
  • We observed this with procedures already
  • Three Types of Loops
  • Do While
  • Do Until
  • For Next

10
Do While
  • This allows you to loop until some conditional
    statement becomes false
  • Do While Condition
  • CODE BLOCK
  • Loop
  • Executes until Condition is false
  • Checks Condition at beginning of loop

11
Do While (example)
  • Display the number 1 to 7 in a list box
  • Dim num As Integer 1
  • Do While num lt 7
  • lstNumbers.Items.Add(num)
  • num 1
  • Loop

12
Do While
Is the condition True?
No
Yes
Execute Code in Loop
Execute Code After the Loop
13
Do Until
  • This allows you to execute some code and continue
    to execute over and over until a condition is
    true
  • Do
  • CODE BLOCK
  • Loop Until Condition
  • This executes until condition is True
  • Does not check condition before entering loop

14
Do Until (example)
  • Display the number 1 to 7 in a list box
  • Dim num As Integer 1
  • Do
  • lstNumber.Items.Add(num)
  • num 1
  • Loop Until num gt 7

15
Do Until
Execute Code in Loop
Is the condition True?
No
Yes
Execute Code After the Loop
16
Nested Loops
  • Just like if statements we can nest loop
  • Do While cond1
  • Do While cond2
  • block1
  • Loop
  • Loop

17
For Loops
  • Many times you loop a specified number of times
    (as in examples)
  • Counting with Counters
  • Dim num As Integer 1
  • Do While num lt 7
  • Stuff
  • num 1
  • Loop
  • There is a better way called For Loops

18
For Loop
  • For loops automatically count for us
  • For num 1 to 7
  • Stuff
  • Next
  • No need to count ourselves
  • Stops with num gt 7

19
Step Values
  • Dont necessarily have to count by 1
  • For num 0 To 10 Step 2
  • stuff
  • Next
  • This counts by 2
  • Will count 0,2,4,6,8,10

20
Negative Steps
  • What if we want to count backwards
  • Use a negative step
  • For num 10 To 1 Step -1
  • stuff
  • Next
  • Will stop when num gt 1
  • 10,9,8,7,6,5,4,3,2,1

21
Control Variable
  • In For loops the counter variable is also known
    as the control variable
  • For num 1 to 7
  • Stuff
  • Next
  • Do NOT modify this variable in the loop
  • Could loop forever
  • Unpredictable behavior

22
Forever Loops
  • This is when the loop never stops
  • Dim num As Integer 1
  • Do While num lt 7
  • lstNumbers.Items.Add(num)
  • Loop
  • This will never stop
  • Need to be careful
  • Called Infinite Loop

23
Example
  • Lets try it out, display a group of numbers
    between two points given a step value
Write a Comment
User Comments (0)
About PowerShow.com