Lesson 10
Java Program Flow Conclusion
This module showed you how to alter the flow of Java programs by exploring branches and loops.
You began the module by learning how to use if
branches and switch
statements.
You learned that if
branches are useful when you are branching based on a couple of possible conditions, while switch
statements come into play when you have more than two branch conditions.
From there, the module shifted gears and introduced you to the different types of loops supported in Java: for
loops, while
loops, and do
loops. You then finished up the module by learning how to break out of loops and continue loops with the break and continue statements, respectively.
Timing is everything
A common programming need is to perform some sort of summation. We have computed the sum of several sequences of numbers in previous examples. While the summation process is relatively straightforward, it can be difficult for novice programmers. More importantly, we will use it here to provide an insight to the programming process.
Programming, by its nature, is an abstract process. The programmer will need to look at a static code listing and infer its dynamic execution. This can be difficult for many people. One way of assisting the developer in writing code is to consider the following three issues:
- What do we want to do?
- How do we want to do it?
- When do we want to do it?
Here, we will ask and apply the answers to these three questions to address the summation problem. However, these questions are equally applicable to other
programming problems.
Let us focus on calculating the average age for a group of students, which will involve the summation process. Assume the ages are stored in age array and then initialized as shown in the following code snippet:
final int size = 5;
int age[] = new int[size];
int total;
float average;
age[0] = 23;
age[1] = 18;
age[2] = 19;
age[3] = 18;
age[4] = 21;
Then the summation can be calculated as follows:
total = 0;
for (int number : age) {
total = total + number;
}
average = total / (age.length * 1.0f);
Notice that total
is explicitly assigned a zero value. Each iteration of the for loop will add the next age to total. At the completion of the loop, total will be divided by the length of the array times 1.0f to compute the average. By using the array
length the code expression does not need to be changed if the array size changes. Multiplying by 1.0f is necessary to avoid integer division.
Glossary terms
This module discussed how the following terms relate to Java:
- Branch statement: Java supports two different kinds of branch statements.
The if-else is the most commonly used, and it is useful in situations where you need to conditionally execute one section of code or another.
Java's other branch statement, the switch statement, is designed to choose between multiple branches of code.
- Compound statement: A compound statement is a block of code that is surrounded by curly braces ({}).
Compound statements allow you to use multiple lines of code in situations where a single statement is expected. A good example is the code executed in the branch of an if statement.
- Conditional operator: The conditional operator acts like a shorthand if-else statement. It evaluates a test expression and conditionally evaluates another expression based on the test.
- Loop: There are three types of loops used in Java: for loops, while loops, and do loops.
- Break statement: The break statement is used to exit out of a branch or loop.
Controlling Java Program Code - Quiz