Java programs, large or small, are written to follow standard forms of organization. Although it may be possible to write a Java program following only the default form of organization, such a program would be clumsy and largely useless.
The types of organization, known as control structures, fall into five categories: sequential structures, subprogram structures, selection structures, looping structures, and asynchronous structures.
Sequences in Java
A sequence in Java is basically a list of commands that computer executes in order from beginning to end. Most small daily activities are sequential, such as cooking a meal or brushing teeth. A computer, like a human, finds sequential operations to be the simplest and as such the sequence is the default structure for Java programs:
public static void main(String args[]){
int var1=1;
int var2=2;
}
The Java Virtual Machine declares each variable and assigns each value to those variables in order without variance. This is easy for simple programs, but is limiting if the programmer wants the program to respond to different situations.
Subprograms in Java
Similar to sequential structures, subprograms essentially consist of multiple methods within the main method. If a program executes a bunch of operations in sequential order, it follows that the program will execute a bunch of methods in order.
These methods can themselves have any number of methods nested within that execute sequentially; this type of structure is known as the subprogram.
public static void main (String args[]){
public class Method1(){
int var1=1;
public class Method2(){
...etc.
}
}
}
The JVM follows the methods to the innermost method and executes any operations there, and if that method specifies allows for returning results, passes the result to the nearest outer method and continues computing each method until it reaches the main method again.
Obviously, this is a very intricate form of programming, since each method can contain any number of structures like loops or branch statements, and there can be multiple subprograms. Good commenting practice is encouraged for these situations!
Selections in Java
Also known as branch, conditional or decision structures, selections allow the program to execute different operations depending on events or conditions. This entails the use of the switch-case statement and the classic If-Then-Else statement.
With its roots in classic mathematics, the if-then-else is used in Java to quantify simple decisions under certain conditions. If something occurs, do this; else, do something different. The simplest form the selection statement can take is just the if-then statement:
if (testVar){
//Commands to execute
}
Note that the parameter testVar looks to see if the variable exists and does not have 'false' assigned to it; the test can be modified to run when testVar does equal 'false': if(!TestVar). The if-then-else statement can be expanded to test and execute commands for many different situations using the if-then-else statement.
Looping in Java
Loops are another essential part of modern programming languages, with no exception in Java. Their premise is simple: the computer executes some operation or group of operations until told to do otherwise or stop.
The loop comes in several variations, each with their own slight variance in structure and purpose: for loops, while loops, do-while loops, and and the brand new for-each loop. While each has differences, all consist of loosely the same structure:
- A loop entry, or the point where the loop begins execution
- A loop test, where the computer tests to see if the loop should begin, repeat, or skip to the next command in the program
- Iteration, or a single cycle through the loop's group of operations
- A termination condition, or the specified situation or event that a loop reaches when it's time to stop looping
Summary
Although there is some variance in opinion on how many types of control structures Java has, what they are and which statements are which, this article serves as an introduction to some of the main concepts of control flow in Java programs. Sequences, subprograms, selection statements, and loops are all foundational concepts commonly found in any developed Java program.
Join the Conversation