190 likes | 300 Vues
This lecture covers essential programming concepts such as conditionals, including If/Else statements, Select Case blocks, and their applications in decision-making processes. We will explore the use of radio buttons and check boxes for user input, and the importance of encapsulating code in procedures for reusability and organization. The session also introduces problem-solving strategies for breaking complex tasks into manageable parts and defines sub-procedures with parameters, showcasing practical examples to illustrate these concepts.
E N D
CS 106, Winter 2009Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu 1
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
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
Flowchart for Select Case Evaluate Selector In Value List 1? yes Action 1 no In Value List 2? yes Action 2 ~ no ~ ~ ~ In Value List n? yes Action n no Perform action of last resort
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.
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
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.
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.
BREAK 10 minutes
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
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
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
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
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
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
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
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
A Picture of the Control Flow Some program code Set parameter values Procedure call Procedure code More program code
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.