We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Control Statements
Control statements are used in programming languages to
cause the flow of execution in advance. These are the
branch based statements where branches are depend on the
state of a program. In Java, control statements can be
divided under the following three categories:
1. Selection Statements
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.
if Statement
The if statement is a powerful decision making statement
and is used to control the flow of execution of statement.
Syntax
if (condition)
statement ;
Here, .
* condition is a boolean expression.
‘« if condition is true, then the statement will execute.
« if condition is false, then the statement will be by passed.
if-else Statement :
The if-else statement can be used to route program
execution through two different paths.
Syntax
if(condition)
statementl; //if condition is true
else
statement2; //if condition is false
Here,
* condition is a boolean expression.
* if condition is true, then statement! or if block will
execute.
* if condition is false, then statement? or else block will
execute. :
Scanned with CamScannerNested if Statement a
‘The if statement can be nested in Java, which means one if
or if else statement inside another if or if else statement ca!
be used.
Syntax
if(conditionl)
if(condition2)
{
statement1:
}
else
{
statement2:
}
}
else
{
if(condition3)
{
statement3:
}
else
{ ”
statement4; :
]
}
Here,
* if condition! is true, then condition2 will execute.
* if condition2 is true, then statement! will execute,
* if condition? is false, then statement2 will execute.
+ if conditiont is false, then in else block condition3 will be
evaluated.
+ if condition3 is true, then statement3 will execute.
* if condition3 is false, then statement4 will execute.
if else if Ladder
‘When a series of decisions are involved, we have to use
more than one if else statement.
An if statement can be followed by an optional elseif.
else statement, which is very useful to test various
conditions.
Syntax
if(condition1)
statement 1
else if(condition2)
statement2;
else if(condition3)
statement3
else
statementn +1;
Scanned with CamScannerHere,
: top.
« ‘The if statements are executed from the top.
«As one of the conditions controlling the if is true,
nt will be executed.
thei cc
wil be executed The final ele acts as a default condition
all the conditions become false, then it will be executed,
switch Statement ;
In Java, the switch statement is another selection statement
that defines different paths of execution for a program. It
can be used in place of chain of if-else statements.
the
Syntax
switch(expression)
case valuel: //statement sequence
break:
case value2: //statement sequence
break;
case valueN: //statement sequence a
break; |
default: //default statement sequence —
y |
Here, i
* The expression must be of type byte, short, int or char.
+ Each case value must be a unique literal (that is, it must
bea constant, not a variable).
+ The break statement is used inside the switch to
terminate a statement sequence.
* If none of the constants matches with the value of the
expression, then the default statement will be executed.
Conditional. Operator (?:)
Conditional operator is a substitute of normal if else
statement, ie, the shorthand form of if else statement is the
conditional operator (2). Its also known as ternary
operator.
eg. if (condition1)
expression1:
else
expression2;
Tr can be written as,
Conditionl? expressionl : expression?
2. Iteration Statements
‘The most basic method of looping in Java is iteration. The
"ePetition of a block of statements either a fixed number of
ses OF until a specific condition occurs is called iteration
or looping.
A looping process includes the followin
* Initialisation of expression,
* Evaluation of the loop condition,
g four steps:
Scanned with CamScanner« Execation of the loop when the specified loop condi
pseaion ition
« Icrementing/Decrementing the expression,
Java provides three kinds of loops as follows:
for Loop
When fixed number of iterations are to be performed, for
Joop is used. '
‘Syntax
for(initialisation; condition; increment /decrement)
{ //body of for loop
Here,
for loop grouped the following three common parts
together into one statement:
(@ Inittialisation The control variables is initialise first,
using assignment statement such as i= tand count 0,
The variables i and count are known as loop-control
variables.
Initialisation expression is executed only once.
(i) Condition The value of the control variable is tested
using test condition. If the condition is true, then the
body of the loop is executed, otherwise the for loop is
terminated and the execution continues with the
statement that immediately follows the for loop.
(ii) Increment or Decrement When the body of the
loop is executed, the control is transferred back to the
for statement after evaluating the last statement in the
loop. Now, the control variable is
incremented/decremented using an assignment
‘statement and the new value of the control variable is
again tested to see whether it satisfies the loop
condition or not. If the condition is satisfied, the body
of the loop is again executed. This process continues
till the value of the control variable fails to satisfy the
test condition.
While Loop
The while loop is used to repeatedly execute a block of
statements based on a condition. The condition is evaluated
before the iteration starts.
Syntax
while(condition)
; /Iwhile loop -body
Here, :
* The condition is evaluated and if che condition is true
.Pethe body ofthe while loop is executed:
en the condition becomes false, the program caf
SS to the statement written immediately
While loop body.
Scanned with CamScannerdo-while Loop
The do-while loop always executes its body atleast onte,
becatise its conditional expression is at the bottom of the
loop.
Syntax
do
{
/Tbody of do-while loop
}while(condition):
Here,
* Each iteration of the do-while loop first executes the
body of the loop and then evaluates the conditional
expression.
* If this expression is true, the loop will repeat.
* Otherwise, the loop will terminate.
3.Jump Statements
Java supports three jump statements, ie. break, continue
and return, These statements transfer control to another
part of the program.
break Statement
This statement is generally used to break the loop of while
do-while, for or switch statement.
It has three uses as follows:
+ It terminates a statement sequence in a switch statement.
+ It can be used to exit a loop.
+ Itcan be used as a ‘civilised’ form of goto statement.
By using break, you can force immediate termination of a
loop, by passing the conditional expression and any
remaining code in the body of the loop. The break
statement should be used to cancel a loop only when some
sort of special situation occurs.
continue Statement
In Java, continue statement is used to skip the part of loop.
Unlike break statement, it does not terminate the loop,
instead it skips the remaining part of the loop and controls
go back to check the condition again.
Continue statement performs below mentioned actions:
* In while and do-while loops, a continue statement causes
control to be transferred directly to the conditional
expression that controls the loop.
* With the break statement, continue may specify a label to
describe which enclosing loop to continue.
+ Itisa jumping statement and is used as keyword.
« It skips the loop and re-executes loop with new
condition.
* In for loop, continue statement takes control to
increment/decrement step, i.e. after continue i+/i-- will
be get executed.
Scanned with CamScanner