Title: Introduction to Control Flow concepts
1Unit 7
- Introduction to Control Flow concepts
- Sequence
- Repetition
- Conditional
- Introduce values, minimally, at the same time
- See how these can be used to automatically fly
the rocket
2Programs must do something
- Concentrated so far on the structure of a model
- the components, the members of each component
- the way in which models evolve
- this is the unique part of the O-O model
- All programs, however, must do something in order
to be effective - examine now the different ways of doing
- and how they can be expressed in prog languages
3We've looked at doing already
- Remember playing with the Fly method from the
RocketController? - adjusting or creating a sequence of
operations/statements - A statement is a single indivisible unit of doing
- Execution model for a sequence of statements is
simple - they are executed, one at a time, from first to
last - Matches closely to some typical behaviours we see
in everyday life - following a simple step by step cooking recipe,
or our work plan for the day do this, then
this, then this
4Technical/Grammatical details
- A statement is always completed with a semi-colon
- A sequence of statements can be made to look like
a single statement by - enclosing it in braces
- this is used all the time, as we'll see later
5More advanced forms of doing
- Is this sequencing of operations enough?
- No, not typically, because
- It is inflexible
- no opportunity to adjust our behaviour according
to what we find in our environment - for example, a simple daily working plan does not
explicitly tell you what to do if there's a fire
alarm - remember the rocket we needed to know the
environment before setting out - It is inefficient
- if we want to do something repeatedly, we must
write the repetitions out in full - So what do we need?
6Conditional doing
- Certain doing is performed only if some condition
is met - Various ways we express this notion in English
- "I'll take an umbrella if it's raining"
- "If it's raining then I'll take an umbrella"
- "If it's raining or it's cloudy then I'll take an
umbrella, otherwise I'll take a sunhat" - "If it's raining then I'll take an umbrella,
otherwise if it's very sunny then I'll take a
sunhat - Notice
- They all express doing, with some condition
attached - Sometimes, nothing will be done at all
- Sometimes, we are choosing between alternatives
something will always be done
7Stating this in a prog. language
- The statement forms most often found
- "IF a condition holds THEN do something"
- note, if the condition doesn't hold, nothing is
done - "IF a condition holds THEN do something ELSE do a
different thing" - one or other of the two things must be done
- which one is decided when the condition is
evaluated see the next slide - There are others
- we'll leave them for now
8Notice the condition
- "a condition holds" from previous slide
- used in both forms what does it mean?
- a condition is a factual statement to be verified
as true or false - factual statements
- it is raining
- her hair is black
- there is an obstacle ahead
- a condition holds means that the statement was
verified as being true - When does the verification take place?
- at the point when the decision must be made
- when the conditional statement is executed
- crucially not until the program is running
9Values, expressions and evaluation
- To avoid confusion between doing statements, and
factual statements - the latter are called expressions
- they express an idea or a computation
- which is evaluated to produce a value
- In the case of a conditional expression
- the idea evaluates to 'true' or 'false'
- these are just two values in the great universe
of values that we'll be exploring
10Repetitive doing
- A statement of doing is repeated a number of
times - sometimes the number is known in advance
- sometimes we only know when to stop at some point
in the middle of the repetitive activity - in these cases, again, a condition is used to
determine when to stop perhaps continue
repeating the activity as long as the condition
holds, stopping when it no longer holds
11Consider these statements
- Fill the fridge with beer
- number of repeats known in advance no
- repeated action put another beer in fridge
- condition The fridge is not full
- Change the spark plugs in the car
- number of reps known in advance yes
- repeated action change the next plug
- Do up shirt buttons
- reps known in advance usually not!
- repeated action move hand to next button and do
it up - condition Some buttons unfastened
- Add up a series of numbers
- reps known in advance sometimes, sometimes not
- repeated action get next number, add it to
running total - condition More numbers available in the series
12How are these expressed?
- We need a formal way of expressing these
- while the fridge is not full, put another beer
in - for each cylinder, change spark plug in that
cylinder for new one - while there are buttons undone, pick an undone
one and fasten it - while there are more numbers in the list, read
the next one and add it to the total - Notice the structuring
- while the condition and the repeated activity
- for the number of reps is implicitly specified
13Step up in understanding
- The flat textual description comes to life only
when it is executed - you need to be able to imagine this happening
- the concept seems easy, but the reality is often
harder to absorb - For example,
- while the fridge is not full, put another beer
in - might result in 100 executions of "put another
beer in" we don't know from simply looking at
the text - The conditionals similarly
- we don't know precisely what will happen until
they are executed
14Conditional statements in C
- Single option conditional statement
- (do nothing if condition doesn't hold)
- This is a template. The words in italics are
expanded out in use to be a valid expression and
statement. Remember that a sequence of
statements can be viewed as one statement with
braces - Conditional with two alternatives
- The condition decides which of two actions to
perform - To execute these statements
- condition is evaluated first. The appropriate
statement is then executed, if any, depending on
the result
if( Condition ) Statement
if( Condition ) Statement else Statement
15Repetitive Statements in C
- We will look at just one for now
- typically used when we don't know in advance how
many repetitions required - To execute this statement
- first the condition is evaluated
- if it is true then
- the contained statement is executed
- we start executing the whole while statement from
the top again - else
- the condition is false and we complete execution
of the whole while statement. The contained
statement is NOT executed
while( Condition ) Statement
16See it in action - AdvancedRocketry
- Run ExecuteMission again
- this should be using AutoRocketController
- when the landing sequence engaged message comes
up, the Land method is executing - Before examining that method closely
- how would you try to land the rocket safely?
- in this rocket version, safely means
- upright, nose pointing upwards
- but could be travelling at any speed down or
sideways - (it's a sturdy rocket, this one!)
17Getting to the ground how?
- Requirements
- Must be upright
- Must be dropping downwards
- Must stop once we've reached the ground
- Speed sideways or downwards unimportant
- So?
- To be dropping downwards but upright
- Engines must be turned off, let gravity do the
work - As we coast downwards, must check periodically to
see whether we've reached the ground - Procedure
- Turn engines off
- Get upright
- Get to ground
18ARocket.EnginesOff() // Get upright
again while( ARocket.IsTilting()
) ARocket.TurnLeft( 1 ) there's more
- Examine the Land method
- Engines off is easy enough simply a method call
- Getting upright is trickier
- Don't know the starting attitude
- Hence continually test to see if the rocket is
tilting - if it is, then nudge it round by 1 degree
- eventually, after enough repeats, the rocket will
be upright - Note that IsTilting returns a true or false value
- known as a Boolean, or bool for short
19// Get down to the ground// The condition in the
while loop tests if we're above groundwhile(
AArena.NearestBelow( ARocket.GetPosition() ) gt 0
) ARocket.Coast( 1 )
- Finally, to get to the ground
- Must ask the Arena how close we are to the ground
- while we are still above it, we must continue to
Coast with engines off hence fall downwards - How do we ask the Arena about positioning
- NearestBelow method returns distance to the
nearest object below the supplied position. This
distance is a number - We supply the position of the rocket, using
GetPosition - The distance returned by NearestBelow is then
tested to see if it is greater than zero (ie
we're above the ground - This final test gives us the true/false boolean
value we need for the while statement
20Try a delicate rocket
- This rocket didn't care about landing speed.
- In ExecuteMission, at the bottom, change
- new AdvancedRocket
- to
- new DelicateAdvRocket
- Rerun it to see what happens
- this rocket crashes if landing speed downwards is
greater than 20 and sideways motion is greater
than 10
21See if you can do better
- Go to SmartLander.cs
- a derived class of AutoRocketController
- overriding ONLY the Land method
- initially it is the same as AutoRocketController
- Adjust ExecuteMission, so it uses this controller
instead of AutoRocketController - change needed at bottom of file
- See if you can work out how to slow the rocket
gently so it doesn't crash hard - Take the following hints one at a time, working
to get an answer on each before going to the next
22Hints
- You can brake the descent
- by firing the engines for a short time
- Do you always need to brake the descent?
- If not always
- what is the condition for firing the rocket?
- There is a testing method FallingFasterThan
- you pass a number it returns true if the rocket
is falling faster than this value - The answer is on the next slide
- only go on if you are totally stuck
- just try something out
23General plan
- Inside the repetitive loop taking the rocket to
the ground - you need to check how fast the rocket is falling,
if it's too fast, fire the rockets - sounds like a conditional statement
- what is the condition?
- what is the action
- if the condition is true?
- if the condition is false?
- code on next slide
24// Get down to the groundwhile(
AArena.NearestBelow( ARocket.GetPosition() ) gt 0
) if( ARocket.FallingFasterThan( 20 )
) ARocket.IncThrustLevel( 10 ) // Turn
engines on to ARocket.Coast( 1 ) // brake
the fall ARocket.EnginesOff() else
ARocket.Coast( 1 ) // fall with no
brakes
- Try this out, if you didn't get there
- Try also now to slow the sideways motion
- methods available to test speed sideways
- similar technique should be used
- when will you do the sideways braking?
- I suggest before this while statement, and after
you've got the rocket back upright - if you need it, there's some useful code in spare
code.txt
25Summary
- Explored the structure inside methods, the code
that does something - the statement
- sequences of statements
- conditional statements - if
- repetitive statements - while
- Necessarily looked at
- expressions, values and evaluation
- We'll explore these more in the next unit