icon picker
review and control structures

Looking back at the programs so far, anticipating control structures
Before moving on to some new ideas in programming in java it will be helpful to consider some features of the code so far.

Input Process Output

In unit 2 the programs had a common theme: Input >> Process >> Output or IPO
IPO is a simple program plan where a program
accepts data entered by a user,
the data is changed in some way to produce a result and
then finally the result is output or displayed to the user

flow of control: sequence

These simple IPO programs have features that are worth noticing:
IPO is a sequence, Input then Process then Output, even though there can be small variations.
Each step in the IPO sequence happens once and every step is always followed in the same order.
This is an unconditional single-pass sequence.
It is like following a straight road followed once from beginning to end with no diversions.
Loading…

Next steps

The phrase “flow of control” for a program is about how the sequence of instructions are tracked and carried out from beginning to end.
The program in this unit introduce two important additions to this simple plan: iteration and conditional code which are both control structures.

iteration

Iteration is similar to repetition, an action is carried out more than once.
The “flow of control” will feature some kind of loop where the program’s action returns to a previous step and code is executed again.
Think about cars looping around a race track.
Loading…

conditional code

The program will have sections of code which are optional. Sometimes the code will be carried out, sometimes not.
Think about a cyclist who has the option of a track route or a road route.
If it is true that you are in a hurry then take the road route.
If it is true that you don’t like cycling in traffic then take the track route.
trip.png

control structures: iteration and conditional

These two features (iteration and conditional code) are two of the most powerful features of code.
A common and important feature of both features is that they change the behaviour of a block of code
iterative control structure
step 1
step 2
start of loop
step 3
step 4
step 5
end of loop
step 6
step 7
Steps 3, 4 and 5 are nested as a block within the control structure and will be repeated
Steps 1, 2, 6 and 7 behave sequentially and do not repeat
A possible sequence is: 1 2 3 4 5 3 4 5 3 4 5 6 7
conditional control structure
step 1
step 2
start of conditional code
step 3
step 4
step 5
end of conditional code
step 6
step 7
Steps 3, 4 and 5 are nested as a block within the control structure and a condition will be evaluated to decide if the steps inside should be carried out or skipped
Steps 1, 2, 6 and 7 behave sequentially and must be carried out
A possible sequence is: 1 2 3 4 5 6 7 another is 1 2 6 7

control structure in Java

blocks of code in java are identified by braces { } or “curly brackets”, here are braces used to mark the start and end of a loop
for (int i = 0; i < 10; i++) {
System.out.println("Hey! ");
}

// or

for (int i = 0; i < 10; i++)
{
System.out.println("Hey! ");
}
Newcomers to languages like java often have problems with braces so always read the example code carefully and even more care is need when copying and pasting code like this.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.