0% found this document useful (0 votes)
10 views12 pages

Control Statements

The document provides an overview of control statements in Java, which include decision-making statements, loops, and branching statements. It explains various types of control statements such as if, if-else, nested if, if-else-if ladder, break, continue, and return, along with their syntax and examples. Additionally, it covers different looping constructs like while, do-while, and for loops, including the enhanced for loop for iterating over arrays or collections.

Uploaded by

SHAHIDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Control Statements

The document provides an overview of control statements in Java, which include decision-making statements, loops, and branching statements. It explains various types of control statements such as if, if-else, nested if, if-else-if ladder, break, continue, and return, along with their syntax and examples. Additionally, it covers different looping constructs like while, do-while, and for loops, including the enhanced for loop for iterating over arrays or collections.

Uploaded by

SHAHIDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA – CONTROL STATEMENTS

 The JAVA control statements inside a program are usually executed


sequentially. Sometimes a programmer wants to break the normal flow and
jump to another statement or execute a set of statements repeatedly. The
statements which breaks the normal sequential flow of the program are called
control statements.
 Control statements enables decision making, looping and branching.

DECISION MAKING STATEMENTS:


These statements decides the flow of the execution based on some conditions.

 If then
 If then else
 switch

LOOPS:
Statements inside a loop are executed repeatedly provided with some condition
which terminates the loop.

 for
 while
 do while
BRANCHING STATEMENTS:
Branching statements are used to jump from the current executing loop.

 break
 continue

If:
if statement is the most simple decision making statement. It is used to
decide whether a certain statement or block of statements will be executed or not
i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition)
{
statement1;
}
else
{
statement2;
}
Example:
class PrintPassFail
{
public static void main(String arg[])
{
int marks = 62;
if(marks > 35)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
}
}
If-else:
The if statement alone tells us that if a condition is true it will execute a
block of statements and if the condition is false it won’t. But what if we want to do
something else if the condition is false. Here comes the else statement. We can use
the else statement with if statement to execute a block of code when the condition
is false.

Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

Example:
class IfElseDemo
{
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Nested if:
A nested if is an if statement that is the target of another if or else. Nested if
statements means an if statement inside an if statement. Yes, java allows us to nest
if statements within if statements. i.e, we can place an if statement inside another if
statement.

Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example:
class ClassifyPerson
{
public static void main(String arg[])
{
int age = 35;
char gender = 'F'; // M - Male, F - Female

if( age > 35 ) // outer if


{
if( gender == 'M' ) // LINE A
{
System.out.println("Man");
}
else
{
System.out.println("Woman");
}
}
else
{
if( gender == 'M' ) // LINE B
{
System.out.println("Boy");
}
else
{
System.out.println("Girl");
}

}
}

If-else-if ladder:
Here, a user can decide among multiple options.The if statements are
executed from the top down. As soon as one of the conditions controlling the if is
true, the statement associated with that if is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else statement will be
executed.
Syntax:
if( condition1 )
{
statement1; // BLOCK 1
}
else if( condition2 )
{
statement2; // BLOCK 2
}
else if( condition3 )
{
statement3; // BLOCK 3
}
else
{
statement4; // BLOCK 4
}
Example:
class PrintStudentGrade
{
public static void main(String arg[])
{
int marks = 65;

if( marks > 75 ) // CONDITION A


{
System.out.println("Distinction"); // LINE A
}
else if( marks > 60 ) // CONDITION B
{
System.out.println("First Class"); // LINE B
}
else if( marks > 50 ) // CONDITION C
{
System.out.println("Second Class"); // LINE C
}
else
{
System.out.println("Fail"); // LINE D
}

}
}
Break:
Break Statement is a loop control statement which is used to terminate the
loop. As soon as the break statement is encountered from within a loop, the loop
iterations stops there and control returns from the loop immediately to the first
statement after the loop.

Syntax:
break;

Example:
class BreakLoopDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}

Using break as a Form of Goto:


Java does not have a goto statement because it provides a way to branch in
an arbitrary and unstructured manner. Java uses label. A Label is use to identifies a
block of code.
Syntax:
label:
{
statement1;
statement2;
statement3;
.
.
}
Now, break statement can be use to jump out of target block.
Note: You cannot break to any label which is not defined for an enclosing block.

Example:
class BreakLabelDemo {
public static void main(String args[])
{
boolean t = true;
first :
{
second :
{
third :
{
System.out.println("Before the break statement");
if (t)
break second;
System.out.println("This won't execute.");
}
System.out.println("This won't execute.");
}
System.out.println("This is after second block.");
}
}
}

Continue:
Sometimes 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 in its body for this particular iteration. This is, in effect, a goto just past the
body of the loop, to the loop’s end. The continue statement performs such an
action.
class ContinueDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
if (i%2 == 0)
continue;
System.out.print(i + " ");
}
}}
Return:
The return statement is used to explicitly return from a method. That is, it
causes a program control to transfer back to the caller of the method.
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");

if (t)
return;

// Compiler will bypass every statement


// after return
System.out.println("This won't execute.");
}
}

Looping Statements in Java


While:
While loops are simplest kind of loop. It checks and evaluates the condition
and if it is true then executes the body of loop. This is repeated until the condition
becomes false. Condition in while loop must be given as a Boolean expression. If
int or string is used instead, compile will give the error.
Syntax:
while (condition)
{
statement1;
}

Example:
class whileLoopTest
{
public static void main(String args[])
{
int j = 1;
while (j <= 10)
{
System.out.println(j);
j = j+2;
}
}
}
Do…while:
Do…while works same as while loop. It has only one difference that in do…
while, condition is checked after the execution of the loop body. That is why this
loop is considered as exit control loop. In do…while loop, body of loop will be
executed at least once before checking the condition
Syntax:
Do
{
statement1;
}while(condition);

Example1:
class dowhileLoopTest
{
public static void main(String args[])
{
int j = 10;
do
{
System.out.println(j);
j = j+1;
} while (j <= 10)
}
}

Example2:
int i = 0;
do
{
System.out.println(“value:”+i);
i++;
} while (i<6)

For:
It is the most common and widely used loop in Java. It is the easiest way to
construct a loop structure in code as initialization of a variable, a condition and
increment/decrement are declared only in a single line of code. It is easy to debug
structure in Java.

Syntax:
for (initialization; condition; increment/decrement)
{
statement;
}

Example:
class forLoopTest
{
public static void main(String args[])
{
for (int j = 1; j <= 5; j++)
System.out.println(j);
}
}
ENHANCED FOR – LOOP:
Enhanced for loop is the simpler form of the usual ‘for’ loop, usually used to
iterate over an array or collection object.

int arr[] = {1,2,3,4,5}

for(int element:arr)

{ System.out.println(“Element of array:”+element);

You might also like