Control statements are used in a programming language in order to execute a block of sequence several times according to the requirement. Java’s program control statements can be put into the following categories i.e Selection, Iteration and Jump.

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

Iteration statements are used to enable the program execution to repeat one or more statements (using loops).

Jump statements are basically used to execute a program in a non-linear manner.

java control statements

Selection Statements –

There are two selection statements in Java that is if and switch.These statements allows a programmer to control the flow of a program’s execution based upon the conditions being used only at the run time.

If –

An if statement tests a particular condition, if the condition evaluates to true, then a course of action is followed that is a statement or set of statements are executed. Otherwise the couse of action is ignored and else part is executed

It’s general form is-

if(condition) statement 1 ;

else statement 2 ;

Switch –

Java provides a multiple branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.

Syntax of switch statement is as follows :

switch ( expression )

{

case constant1 : statement sequence 1 ;

break;

case constant2 : statement sequence 2;

break;

:

:

:

case constant n-1 : statement sequence n-1;

break;

default : statement sequence n

}

The expression is evaluated and its values are matched against the values of the constants specified in the case statements. When a match is found, the statement sequence is associated

Default statement is executed when no match is found.

Iteration –

The iteration statement allows a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also known as loops or looping statements.

Java provides three kinds of loops i.efor loop, while loop and do-while loop . These all three loops construct of java repeat a set of statements as long as specified condition remains true

For Loop

The forloop is the easiest to understand of the Java loops. All its loop-controlled elements are gathered in one place.

Syntax –

for ( initialization ; condition ; iteration) {

// body

}

 

While Loop –

The second loop available in Java is the while loop. It is an entry-controlled loop too just like the for loop .

Syntax –

while( condition ) {

// body of loop

}

Do-while Loop –

It is an exit controlled loop. It means that do-while loop will always give at least one output that is it is executed at least once. Reason behind this is that the condition is checked after the statement being executed for the first time.

Syntax –

do {

// body of loop

}

while (condition) ;

Jump Statements –

Java supports three jump statements :break, continue and return. These statements transfer control to another part of your program .

Break –

Break statement is used to stop the flow of a program. It is used to stop a program at a certain point .The basic syntax is break ; wherever required.

Continue –

Sometime it is useful to force an early iteration of a loop. That is you might want to continue running the loop but stop processing the remainder of the code for that particular iteration.

The basic syntax is continue ;wherever required.

Return –

The last control statement is return. The return statement is used to explicitly return from a method . That is, it causes program control to transfer back to the caller of the method.

The basic syntax is return ;

 

 

 

Recommended Post   Argument Passing
SHARE
Previous articleJava Arrays
Next articleMethods in Java

LEAVE A REPLY

Please enter your comment!
Please enter your name here