A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java’s program control statements can be put into the following categories: selection, iteration, and jump.

Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.

Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops).

Jump statements allow your program to execute in a nonlinear fashion.

Java’s Selection Statements

Java supports two selection statements: if and switch. These statements allow you to control the flow of your program’s execution based upon conditions known only during run time.

if

The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:
          if (condition) statement1;
         else statement2;
The if works like this: If the condition is true, then statement 1 is executed. Otherwise, the statement 2 (if it exists) is executed. In no case will both statements be executed. For example, consider the following: 

                                                           

Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are they both set to zero.

Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. Here is an example: 

The if-else-if Ladder

A common programming construct that is based upon a sequence of nested ifs is the if-elseif ladder. It looks like this: 

Here is a program that uses an if-else-if ladder to determine which season a particular month is in. 

Switch

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements. Here is the general form of a switch statement: 

 Here is a simple example that uses a switch statement: