1 / 27

Week 2 – Basic Constructs 1

Week 2 – Basic Constructs 1. Sequential Conditional branching Looping. Last week. Introduced Stepwise refinement There is often more than one solution to a problem Discussion and working in groups is a useful technique to include in problem-solving. Sequences. Instruction 1

chyna
Download Presentation

Week 2 – Basic Constructs 1

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. Week 2 – Basic Constructs 1 • Sequential • Conditional branching • Looping

  2. Last week • Introduced • Stepwise refinement • There is often more than one solution to a problem • Discussion and working in groups is a useful technique to include in problem-solving.

  3. Sequences • Instruction 1 • Instruction 2 • Instruction 3

  4. Sequences • The instructions follow on from each other, always the same.

  5. Sequences • Starting at the first and ‘flowing’ down.

  6. Conditional Statements • If test 1 is true then do instruction 1 • If test 2 is true then do instruction 2 if false then do instruction 3

  7. Conditional Statements • The flow of the program can be altered by whether a test is true or not.

  8. Looping • Instruction 1 • Instruction 2 • Go back to instruction 1 • Sometimes we want to repeat a list of instructions.

  9. Looping – with a test • Instruction 1 • Instruction 2 • Go back to instruction 1 while test is true • Repeat a list of instructions, but only if a test is true.

  10. Problem 2.1 • A routine to calculate a tip based on the overall service. • No tip if the service is poor • 10% tip if the service is okay • 15% tip if the service is good or better

  11. Problem 2.1– Problems • What does poor, okay or good mean?

  12. Problem 2.1– Problems • How do we quantify these? • We could use marks out of 10. • <3 for poor • 3<=marks<=8 for okay • >8 for good

  13. Problem 2.1– Problems • What is our tip a percentage of? • We know this is going to be a percentage of the price of the bill.

  14. Analysis • Inputs needed • Price of the meal • Marks for service

  15. Analysis • Outputs • Price + tip

  16. Analysis • Rules • <3 for poor • 3<=marks<=8 for okay • >8 for good

  17. Design • Input the price • Input the marks out of 10 • If marks<3then Tip=0; • If 3<=marks<=8 then Tip =Price*0.1; • If marks>8 then Tip=Price*0.15 • Display Tip • Display Price

  18. Problem 2.2 • What would we have to do to the previous routine if we want it to repeated calculated tips?

  19. Problem 2.2 • One of the basic constructs is the loop. • The next question is what to loop? • In this case all the routine needs to loop.

  20. Start of loop 1 • Input the price • Input the marks out of 10 • If marks<3 • Tip=0; • If 3<=marks<=8 • Tip =Price*0.1; • If marks>8 • Tip=Price*0.15 • Display Tip and price • Go back to start of loop 1

  21. convention • instructions that are the result of a particular conditional statement or loop are indented below it signify they belong to that instruction.

  22. Problem 2.3 • Design a system to make the robots you met last week move towards a wall detect the wall is there and reverse backwards and turn to the right 90 degrees.

  23. Problem 2.3 • Let’s experiment: In small groups use drawings (does need to be neat) to express your ideas while you thinking about this as a group. Then write down your choosen solution as a set of instructions.

  24. Start of one possible solution (needs expanding) • Move the robot forward • Check if the wall is detected • If wall is detected then • Go back wards • Turn to the right 90 degrees • Repeat this whole process

  25. Problem 2.4: • Write down a detailed list of instructions to open a bottle. • Have a go yourselves • Have you thought of all situations?

  26. Problem 2.5 • Wall-following robot • Keeps the wall to its right and follows the contour of the room. • It can touch the wall.

  27. Problem 2.6 • Try problems 2.3 or 2.5 with different sensors.

More Related