Download presentation
Presentation is loading. Please wait.
1
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.eduherb@cs.pdx.edu
2
2 Conditionals We’ve learned about several types of conditionals: – If/Else statements – Ifs with no Else – If/ElseIf statements – Select Case blocks – Nested conditionals We learned how to write Boolean expressions and analyze them using truth tables
3
3 Case Block example Assume position is a variable with a value between 1 and some number from 2 to 20 SelectCasepostion Case 1 txtOutcome.Text = “Win”‘several lines of code could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else‘action of last resort txtOutcome.Text = “Out of the money” End Select
4
4 Flowchart for Select Case Evaluate Selector In Value List 1? In Value List 2? In Value List n? Perform action of last resort Action 1 Action 2 Action n yes nono nono nono ~ ~ ~~
5
5 Conditional Controls We’ve learned about radio buttons and check boxes. These controls let us specify options to our program. Only one radio button in a group can be clicked. Any number of check boxes in a group can be clicked.
6
6 Assignment 4 The complete specification of Assignment 4 is on the website. Assignment 4 builds on Assignment 3, using conditionals. As before, you will – design the interface, – write the use cases and tests, – list the objects and their events, – name and describe the global variables you will use, – create flowcharts for the nontrivial events, and – write and test the program. Submit everything in your zipped project folder
7
7 Example for Assignment 4 We’ll look at an example that performs similar operations as Assignment 4. The project for this example is on the website in the IceCream2 zip file.
8
8 Ice Cream Store Enhancement We can still enter number of scoops We can choose the flavor (one flavor) We can choose toppings (as many as we want) At the end we get an itemized bill in a list box, with our total and the tip amount.
9
9 BREAK 10 minutes
10
10 General Procedures The main idea: encapsulate some code in its own procedure (Sub or Function) We’ve seen this in event procedures already Why create our own procedures? – If we are repeatedly doing the same task, we can write the code in one place and then call the procedure repeatedly – If a procedure is too long and complex, we can break it into understandable parts
11
11 Problem-Solving Strategy Break a large, complex problem into smaller, more manageable parts Master a particular task that comes up repeatedly so you don’t have to think about how it works each time it occurs
12
12 SubProcedures A subProcedure has a name, possible parameters, and a body of code. Example procedure definition: Sub DisplaySum( ByVal num1 as Double, ByVal num2 as Double ) ‘Display two numbers and their sum Dim sum as Double sum = num1 + num2 lstResult.Items.Add( “The sum of “ & num1 &“ and “ & _ num2 & “ is “ & sum & “.” End Sub
13
13 Procedure Name Let’s consider the parts of the header: Sub DisplaySum( ByVal num1 as Double, ByVal num2 as Double) DisplaySum is the name of the procedure. We’ll start procedure names with capital letters and variable names with lower-case letters. The name is used when use (call) the procedure elsewhere in the program
14
14 Parameters Sub DisplaySum(ByVal num1 as Double, ByVal num2 as Double) The parameters are num1 and num2 A ByVal parameter is very similar to a local variable Its type is declared in the header (these are type Double) Its initial value is set in the procedure call Changes to ByVal parameters have no effect on variables elsewhere in the program; still, it is best as a programming practice not set them to new values inside the procedure
15
15 Procedure Body Sub DisplaySum( ByVal num1 as Double, ByVal num2 as Double ) ‘Display two numbers and their sum Dim sum as Double sum = num1 + num2 lstResult.Items.Add( “The sum of “ & num1 & “ and “ & _ num2 &“ is “ & sum & “.” End Sub Note the local variable sum The result of calling this procedure is to make some text appear in list box lstResult
16
16 Procedure Call A procedure is called from elsewhere in the program, for example from within some event procedure Here’s what it might look like: Dim aVar, bVar As Double aVar = 2 bVar = 3 DisplaySum( 4, 5 ) ‘parameter values are literal numbers DisplaySum( aVar, bVar – 2 ) ‘parameter values are expressions
17
17 What Happens in a Procedure Call The expressions for the parameter values are evaluated, and the parameters are set to those values The Dim statements for the procedure are used to create any local variables The code for the procedure is executed Control returns to the line after the procedure call
18
18 A Picture of the Control Flow Some program code Procedure call More program code Procedure code Set parameter values
19
19 Two Aspects of Procedures The Defintion: a separate piece of code where the procedure is defined, as we do with event procedures The Procedure Call: A piece of code that invokes a procedure Event procedures are invoked when the event happens. We’ll see examples of code with procedures in our next class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.