Control Statements
Control Statements
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-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;
}
}
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.");
}
}
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;
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.
for(int element:arr)
{ System.out.println(“Element of array:”+element);