220 likes | 335 Views
Programming. Problem Solving with Computers. Sometimes …. What a program such as Excel provides is not enough You need to do a little programming!. Variable data type. Short for Dim ension. Variable identifier. Declarations.
E N D
Programming Problem Solving with Computers
Sometimes … • What a program such as Excel provides is not enough • You need to do a little programming!
Variable data type Short for Dimension Variable identifier Declarations • Tells the computers the names and data types of temporary variables • Example Dim myVar Integer
2. Assign value to variable on left 1. Evaluate expression on right Assignment Gross Pay = Hours * HourlyWage • Gee, doesn’t this look like an equation? • But it’s not; it’s an action statement.
And now … Demo!
Try this! • Problem: You have been asked to determine how long it will take to pay back a $1000 loan assuming … • Interest rate of 15% • Monthly loan payments of $100 • Do you know enough to solve this?
And this! • Problem: You wish to create a function that will turn a month number into a month name. • Hint: If-Then-Else
Q: What is a Loop? • A control structure that allows for a sequence of steps to be repeated a certain number of times • This sequence of steps is called the body of the loop
Q: What is a Loop? • There are three basic loop structures in programming: • For • While • Repeat
While loop • A while loop is a control structure where the body is repeated as long as the condition is true Condition F T Body
While loop • When the condition is false, the body is bypassed, and flow continues with the next part of the algorithm Condition F T Body
Example: Adding Sequences k 0 total 0 while (k<size) k k + 1 total total + k (k<size) F T k k + 1 total total + k
Example: Adding Sequences iCnt = 0 iTot = 0 Do While iCnt<iSize iCnt = iCnt + 1 iTot = iTot + iCnt Loop iCnt < iSize F T iCnt = iCnt + 1 iTot = iTot + iCnt
For Loop iCnt = 0 Do While iCnt < iSize ‘Do something iCnt = iCnt + 1Loop Wait! This isn’t a For loop? iCnt < iSize F T Do something
Countervariable Finalvalue Initialvalue Step For Loop For iCnt = 0 to 4 ‘Do somethingNext Ahhhhhh! That’s better! iCnt < iSize F T Do something
For Loop • Say, that last example didn’t have a step value, did it? • The step in a For loop is optional. If omitted, a default step of +1 is assumed by the computer • What values would iCnt have in the following? • For iCnt = 0 To 10 Step 2
Question? • If the initial value in a For loop is greater than its final value, will the body ever execute? • Do this for homework!
Exercise • The function factorial (n!) is defined as the product of the integers 1 through n. • Create two functions to compute n!, the first version using a for loop and the second using a while loop.
Repeat loop • A repeat loop is a control structure where the body is repeated until the condition is true Body Condition F T
Repeat loop • When the condition is true, the body is bypassed, and flow continues with the next part of the algorithm Body Condition F T
Example: Adding Sequences iCnt = 0 iTot = 0 Do iCnt = iCnt + 1 iTot = iTot + iCnt Until iCnt = iSize Body Condition F T
Try this! • Problem: To create a worksheet to determine cost of an order for a customer, based on … • Items purchased, • Cost and quantity for each item, and • Shipping and handling • Note: Shipping and handling is based on weight