by
PRO
FE S
S OR

SECTION VII- Programming Flow Instructions
Step 24: Straight Line and Branch Programming
There are 2 kinds of programming, based on the path the code travels:
In JAVA the code Instructions used in coding are similar to C, or C++.
Straight Line Programming
Straight line Programming is when there is no decision making in our code. There is no branch in the path.
It is just a straight path from beginning to end. As an example of Straight-line Programming we can think about a program
that the only task performed are assignment, and mathematical manipulations.
Example of Straight Line code:
class Exercise
{
{
int b = 5;
System.out.println(a*b);
Branch Programming
Branch Programming is when decision making is implemented in our code. The code may generate one or many branches.
In JAVA, decision making or branching can be implemented by the existing control flow Instructions.
The Programming Flow Instructions that we wil be seeing in next Steps are categorized into the following groups:
These Instructions ought to be written in a method where they will be implemented. Or they can be enclosed between curly braces.
We are showing some Examples where these Instructions are written inside the 'main' method.
Obviously they can be written in any method.
by
PRO
FE S
S OR

SECTION VII- Programming Flow Instructions
Step 25: Decisions and Cases
by
PRO
FE S
S OR

SECTION VII- Programming Flow Instructions
Iteration
ITERATION
In JAVA iteration can be accomplished by three constructs:
The 'for' loop
The 'for' loop utilizes 3 fields for controlling any iteration. It's the most powerful of all the iteration constructs
in JAVA, as the programmer has more options.
Each of the fields are separated by a semi-colon.
{
Notice that within the for construct, the index was declared as an 'int'. That declaration could have been done outside the loop.
If index is declared outside the loop its value reamins within the scope after the loop is implemented.
If it's declared within the loop (as in the example above) then its value is only within the scope of the for loop.
See Example below:
{
{
for (index=10; index>2; index--)
{
System.out.println(index); // displays 2, the value from the last loop iteration
// Notice that if the index were declared inside the for loop, the statement above
// (which is outside the for loop) would make the program non-compilable as index
// would then be outside scope.
Let's have another example, this time using more than one variable (2 indeces) to illustrate the power of for loops.
Each variable used are of different data type.
{
{
char charIndex;
for (index=1, charIndex='A' ; index<=26; index++, charIndex++)
{
System.out.println(charIndex); // displays the English Alphabet letters
The 'while' loop
The 'while' loop evaluates a condition to decide whether or not to continue the iteration.
It's considered an entry condition iteration, as the evaluation occurs before entering the loop.
If the condition is met, it goes through the loop, otherwise it goes out of the loop.
Example of a while loop:
{
{
while (index<10)
{
System.out.println(index); // displays 2, 4, 6, 8, and 10
// If the display statement is placed before the increment statement,
// then, it would have displayed 0, 2, 4, 6, and 8 instead.
The 'do while' loop
The 'do while' loop as the 'while' loop evaluates a condition to decide whether or not to continue the iteration.
However, it's an exit condition iteration, as the evaluation occurs after the loop.
By virtue of this, the 'do while' loop is always implemented at least one time.
Example of a 'do while' loop:
{
{
do
{
System.out.println(index); // displays 2, 4, 6, 8, and 10
while (index>0 && index<10); // Notice the semicolon at the end of this statement.
In above example if we would have used 'while' loop instead of the 'do while' loop, the iteration would have never been
exercised, as one of the conditions "index>0" would never have been met, at the entry point of the loop.
by
PRO
FE S
S OR

SECTION VII- Programming Flow Instructions
Processor Control Instructions: break and continue
There are two Processor Control Instructions in JAVA: break and continue.
The 'break' Instruction
The 'break' instruction is used to exit any loop construct, or the switch construct.
When we discussed the 'switch' construct in a previous Step, we saw that 'break' was used to exit the construct from within any case.
In general, the 'break' instruction is implemented when the programmer wants to exit the loop for a particular condition.
See Example below:
{
{
boolean flag = true;
while (flag)
{
if (number==10)
The 'continue' Instruction
The 'continue' Instruction is used to escape one iteration of the loop.
See Example below:
{
{
boolean flag = true;
while (flag)
{
if (number==5)
Labels
In JAVA, there is no 'goto' instruction. As programmers have learned over the years, the 'goto' statement can produce unstructured and
inefficient code. Therefore, as JAVA was designed it was decided to eliminate the 'goto' Instruction. However, it's interesting to
notice that even though the 'goto' instruction is not implemented, it cannot be used as an identifier,
as JAVA would interpret it as a keyword.
In JAVA we can use Labels. They are tags followed by a colon, and they are used for naming loops.
They are used in conjunction with the 'break' and 'continue' instructions.
The Labels are conveniently employed in case of nested loops. When placing labels, make certain they are written right at the position
just before the loop starts.
See Example below:
class Exercise
{
{
boolean flag = true;
outerLoop: // label for this Outer Loop
while (flag)
{
innerLoop: // label for this Inner Loop
for (i=1; i<10; i++)
{
{
do
{
k++;
System.out.println(i + " " + j + " " + k);
// i displays 1 through 9 five times each number
// j displays 1 through 40 consecutively
// k displays 1 through 5 repetitively
while (k<5);
k=0;