1 / 22

Programming

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.

phiala
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Problem Solving with Computers

  2. Sometimes … • What a program such as Excel provides is not enough • You need to do a little programming!

  3. Variable data type Short for Dimension Variable identifier Declarations • Tells the computers the names and data types of temporary variables • Example Dim myVar Integer

  4. 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.

  5. And now … Demo!

  6. 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?

  7. And this! • Problem: You wish to create a function that will turn a month number into a month name. • Hint: If-Then-Else

  8. 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

  9. Q: What is a Loop? • There are three basic loop structures in programming: • For • While • Repeat

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Countervariable Finalvalue Initialvalue Step For Loop For iCnt = 0 to 4 ‘Do somethingNext Ahhhhhh! That’s better! iCnt < iSize F T Do something

  16. 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

  17. Question? • If the initial value in a For loop is greater than its final value, will the body ever execute? • Do this for homework!

  18. 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.

  19. Repeat loop • A repeat loop is a control structure where the body is repeated until the condition is true Body Condition F T

  20. 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

  21. Example: Adding Sequences iCnt = 0 iTot = 0 Do iCnt = iCnt + 1 iTot = iTot + iCnt Until iCnt = iSize Body Condition F T

  22. 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

More Related