JAVA PROGRAMMING
Code : 05201201
MCA [ Semester-I ]
Faculty of IT & Compute Science
PARUL UNIVERSITY
Code : 05201201
UNIT-1 Introduction to Java
[Datatypes, Operators, Statements]
Lecture – 8
• Control statements :
• Selection ,
• Iteration and
• Jumping Statements
CONTROL STATEMNTS
• 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 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
Selection Statements
• Also called as Decision making statements because it provides the
decision making capabilities to the statements.
• In selection statement, there are two types:
1. if statement
2. switch statement
• Diffent formats of if statement:
• Simple if
• Nested if
• If..else
• If..else if Ladder
• switch statement:
switch(condition)// condition means case value
{
case value-1:statement block1;break;
case value-2:statement block2;break;
…
default:statement block-default;break;
}
Cont..
• Nested switch Statements
switch(count)
{
case 1:
switch(target)
{ // nested switch
case 0:
[Link]("target is zero");
break;
case 1: // no conflicts with outer switch
[Link]("target is one");
break;
}
break;
case 2: // ...
Iteration Statements [looping]
• The process of repeatedly executing a statements and is called
as looping. The statements may be executed multiple times (from
zero to infinite number).
• Looping is also called as iterations.
• In Iteration statement, there are three types of operation:
• for loop
for(initialization;condition;iteration)
{
Statement block;
}
• while loop: while(condition){ Statement block;}
• do-while loop:
do
{
Statement block;
}
while(condition);
jumps in statement
• “break” keyword use for exiting from loop.
• “continue” keyword use for continuing the loop.
• Labelled block to jump at specific statement:
• We can give label to a block of statements with any valid name.
LOOP1: for(i=1;i<100;i++)
{
[Link](““);
for(j=1;j<100;j++)
{
[Link](“$ ”);
if(i==j)
{
continue LOOP1;
}
}
return statement
The return statement causes program control to transfer back to
the caller of the method.
• At any time in a method the return statement can be used to cause
execution to branch back to the caller of the method.
• Thus, the return statement immediately terminates the method in
which it is executed.
class Return {
public static void main(String args[]) {
boolean t = true;
[Link]("Before the return.");
if(t) return; // return to caller
[Link]("This won't execute.");
}
}
The output from this program is shown here:
Before the return.