Java Flow Control
Flow control is a key part of most programming languages and Java offers several ways to do it, this web page will cover the following topics
The page is not a complete tutorial on how to use the control statements but quick look up on the syntax of the command, please refer to the Java documentation if you want a complete tutorial of the below commands.
IF statement | if (booleanExpression ) { System.out.println("Inside a IF statement "); Note: if you only have one line of code for the IF statement you can obmit the brackets but i think this makes the code not very clear (I like brackets) |
IF-ELSE statement | if ( price < 100 ) { ## You can also remove the brackets when you only have one line of code for each true or false ## Using a if-else-if Note: always indent conditional code it makes it much more pleasant on the eyes |
Ternary operator | result = ( a < b ; a ? b); if the condition a < b is true then the value of a is assigned to result Note: The ternary operator (conditional operator) is a short-hand of a if-else statement |
SWITCH statement | int x = 3; Note: switch statements can only evaluate byte, short, int and char |
WHILE statement | int x = 0; while ( x < 10 ) { Note: it is possible that the while body may never be executed |
DO-WHILE statement | int x = 0; do { Note: The while body will always be executed once, also notice the semi-colon at the end of the while expression |
FOR loop | for ( int x = 0; x < 10; x++) { # More complex FOR loop Note: you can have only one logical expression but it can be very complex. The for statement is made up of three parts
for ( control variable initialization ; loop continuation ; increment/decrement of the control variable ) |
BREAK statement | int counter = 1; Note: The break statement when executed in a while, do/while or switch statement causes immediate exit from that structure and continues on to the next statement after the while, do/while or switch structure. |
CONTINUE statement | int counter = 1; Note:The continue statement when executed in a while, do/while or switch skips the remaining body statements and proceeds to the next iteration of the loop. |
LABELED statement | stop: // labeled compound statement Note: Labeled break/continue statements are to breakout of nested set of structures, the beginning of the nested structure starts with a label which is a place that the break statement will exit from. |
A recursive method is a method that calls itself either directly or indirectly through another method. Every recursive method must have base case, a condition under which no recursive call is made, this prevents the recursive going into a infinite loop.
A recursive approach is taken when it is much simpler to write a recursive loop, as every recursion solution could be replaced by a iternation solution, the coding of a iternation approach can be more complex.
One final note regarding recursion is that although the code is simpler, the code will use more memory and execute slower as the overhead of calling many methods is a penalty hit.
Recursion example | public class FactorialTest { public static long factorial(long number) // this method will be called many times. |