Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

The statements inside your source files are generally executed from top to bottom, in the order that

they
appear. Control flow statements, however, break up the flow of execution by employing decision making,
looping, and branching, enabling your program to conditionally execute particular blocks of code

Whenever the computer runs a Java program, it goes straight from the first line of code to the last.
Control statements allow you to change the computer's control from automatically reading the
next line of code to reading a different one. Lets say you only want to run some code on
condition. For example, let's say that you are making a program for a bank. If someone wants
to see his records, and gives his password, you don't always want to let him see the records.
First, you need to see if his password is correct. You then create a control statement saying
"if" the password is correct, then run the code to let him see the records. "else" run the code
for people who enter the wrong password. You can even put one control statement inside
another.

A program is a group of statements that are executed to achieve a predetermined task. Statements
in a program are generally executed in a sequential manner, which is called sequential execution or
sequential control flow. However, by putting the decision-making statement in the program, the
normal flow of the program can be controlled. Statements that control the flow of the program are
called control statements.

Control statements are used in programming languages to cause the flow of control to advance and
branch based on changes to the state of a program. In Java, control statements can be divided
under the following three categories:

Different types of control statements:

• Selection /decision making statements (if, if-else and switch),


• looping / Iteration statements (while, do-while and for) and
• Transfer/ Jump statements (break, continue and return).

Selection statements in Java

Selection statements are used in a program to choose different paths of execution based upon the
outcome of an expression or the state of a variable. Java supports two selection statements: if and
switch.

• The if statement

The if statement is a conditional statement. It is used to execute a statement or group of


statements based on some condition. The general form of the if statement is given below:

if (condition)
statement1;
else
statement2;

The condition to the if statement must be an expression that evaluates to a boolean value. If the
condition evaluates to true, the statement following the if statement is executed. However, if more
than one statement is needed to be executed, then these statements must be put within a pair of
curly braces forming a block of code. The else statement is optional, but if it is present, it must be
placed immediately after the code block attached to the if statement. The code block attached to
the else statement is executed when the condition to the if statement evaluates to false.

• The switch statement

The switch statement is used to select multiple alternative execution paths depending on the value
of a variable or expression. The general form of a switch statement is given below:

switch(expression){
case val1:
code_block1
case val2:
code_block2
.
.
.
default:
code_default;
}

where, the expression to the switch must be of a type byte, short, char, or int. A code block is
attached to the switch statement that contains multiple case statements and an optional default
statement. Each of the values (following the case statements) must be unique within the code
block and must be assignment compatible with the type of the expression without the loss of
information.

The switch statement executes by comparing the value of the expression with each of the
constants after the case statements. If a match is found, the statements following that case
statement are executed. Otherwise, the statements following the default statement (if present) are
executed. Conventionally, the default label is put after all the case labels, but it may appear
anywhere in the block.

The break statement is used within the code block attached to the switch statement to terminate a
statement sequence.

Iteration statements

Iteration statements enable program execution to repeat one or more statements, i.e., iteration
statements form loops. In Java, there are three iteration statements: for, while, and do.

• The for statement


The for loop is the most versatile looping construct. It is used to continuously execute a block of
code until a particular condition is satisfied. It comprises three parts: initialization, condition, and
iteration.

The initialization portion is generally an expression that sets the value of the loop control variable.
The loop control variable acts as a counter and controls the execution of the loop. The initialization
portion executes only once. The condition portion must be a boolean expression. It usually tests
the loop control variable against a target value and hence works as a loop terminator. The
iteration portion is usually an expression that increments or decrements the loop control variable.

The general form of the for loop is:

for(initialization; condition; iteration)

{
//body of the loop
}

All the three components, i.e., initialization, condition, and iteration are optional. In case there is
only a single statement in the body of the loop, the curly braces can be omitted.

The for loop executes in the following three steps:

 When the loop first starts, the initialization expression is executed and then the
control is transferred to step 2.
 The condition is evaluated. If the condition evaluates to true, the body of the loop
executes and the program control is transferred to step 3. If the condition evaluates to false, the
loop terminates.
 The iteration expression executes and then the control is transferred to step 2.
• The while statement

The general form of the while loop is as follows:

while(condition)

{
block

where, the condition may be any expression that evaluates to a boolean value. The code block
attached to the while statement is repeatedly executed unless the condition evaluates to false.

• The do statement

The general form of the do statement is given below:

do
{
code_block
} while(condition)

This statement is similar to the while statement discussed above. The only difference in this case
is that the code block attached to the do statement is executed before the condition is checked.
Therefore, even in the case of the condition evaluating to false, the code block executes at least
once.

Jump statements

Jump statements are used to unconditionally transfer the program control to another part of the
program. Java has three jump statements: break, continue, and return.

• The break statement

A break statement is used to abort the execution of a loop. The general form of the break
statement is given below:

break label;

It may be used with or without a label. When it is used without a label, it aborts the execution of
the innermost switch, for, do, or while statement enclosing the break statement. When used with a
label, the break statement aborts the execution of any enclosing statement matching the label.

Note: A label is an identifier that uniquely identifies a block of code.

• The continue statement

A continue statement is used to alter the execution of the for, do, and while statements. The
general form of the continue statement is given below:

continue label;

It may be used with or without a label. When used without a label, it causes the statement block of
the innermost for, do, or while statement to terminate and the loop’s boolean expression to be re-
evaluated to determine whether the next loop repetition should take place. When used with a
label, the continue statement transfers control to an enclosing for, do, or while statement matching
the label.

• The return statement

A return statement is used to transfer the program control to the caller of a method. The general
form of the return statement is given below:

return expression;

If the method is declared to return a value, the expression used after the return statement
must evaluate to the return type of that method. Otherwise, the expression is omitted. .

You might also like