cs31 lecture 2

# 2 2 3 4 4 5 5 6 7 7 8 9 10 11 12 13 14 iif

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: eeded 21 A more complete algorithm 1. 2.. 2 3. 4.. 4 5.. 5 6. 7.. 7 8. 9. 10. 11. 12. 13. 14. iif g < 0 then f return ‘unkown’ return endif endif iif m <= 0 then f mpg := defaultMpg mpg else else if g = 0 then if mpg := 99 mpg else else mpg := m / g mpg endif endif endif gasLeft := c – g return gasLeft * mpg return 22 Some new keywords x if … then … else … endif then else endif Control structure: depending on the condition execute one or the Control other block other An if finishes by an endif Formally: iif <condition> then f <instructions if condition is true> <instructions else <instructions if condition is false> endif 23 MilesToRefuel MilesToRefuel x In an actual car, MilesToRefuel is called frequently, and its In result is displayed on the dashboard result x “Algorithm” (for my car) 1. Every 5 seconds do 2. mtr := MilesToRefuel(GetSensor(‘miles’), GetSensor(‘tank’), 18, 32) mtr 3. displayDashboard(mtr) displayDashboard(mtr) 4. done 24 What’s next? x This algorithm is a pseudo-code for an actual, complete This pr...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online