Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Flow Control Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Flow Control Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java By Venkatesh Mansani Naresh i Technologies

Statements In Java
Java Statements are divided into 3 categories:
1) Selection Statements
2) Iteration Statements(Loops)
3) Jump Statements

1) Selection Statements:
i) if Statement
ii) if else Statement
iii) if else if …. else Statement
iv) Nested if Statement
v) switch Statement

2) Iteration Statements(Loops):
i) while loop
ii) do while loop
iii) for loop
iv) Enhanced for loop (or) for each loop
v) Nested loops

3) Jump Statements:
i) break Statement
ii) break LABEL Statement
iii) continue Statement
iv) continue LABEL Statement
v) return Statement

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

i) if Statement:
It is used to express the condition. If block is executed if the condition is true.

Example:
class Demo
{
public static void main(String args[])
{
int a=5;
if(a>0)
{
System.out.println(“Positive Number”);
}
}
}

ii) if else Statement:


It is also used to express the condition. If block is executed if the condition is true
otherwise else block is executed.

Example:
class Demo
{
public static void main(String args[])
{
int a=5;
if(a>0)

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

{
System.out.println(“Positive Number”);
}
else
{
System.out.println(“Negative Number”);
}
}
}

iii) if else if …. else Statement:


It is used to execute one block of code among multiple blocks of code. Else block
is executed if all the conditions are false.

Example:
class Demo
{
public static void main(String args[])
{
int a=5;
if(a>0)
{
System.out.println(“Positive Number”);
}
else if(a<0)
{

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

System.out.println(“Negative Number”);
}
else
{
System.out.println(“Zero”);
}
}
}

iv) Nested if Statement:


If statement in another if statement is called as nested if statement.

Example:
class Demo
{
public static void main(String args[])
{
int a=5;
if(a>0)
{
if(a%2==0)
System.out.println(“Even Number”);
else
System.out.println(“Odd Number”);
}
else

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

{
if(a<0)
System.out.println(“Negative Number”);
else
System.out.println(“Zero”);
}
}
}

i) while loop:
The body of while loop is repeatedly executed until the condition becomes false.

Example:
class Demo
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}

ii) do while loop:

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

The body of do while loop is repeatedly executed until the condition becomes
false.
Do while loop is executed at least once

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

iii) for loop:


The body of for loop is repeatedly executed until the condition becomes false.
It supports to write initialization expression, test expression and updation
expression in a one line.

Example:
class Demo
{
public static void main(String args[])
{

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}

i) break Statement:
It terminates the nearest enclosing loop or switch statement.

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

ii) break LABEL Statement:


It terminates the specified LABEL loop. Here break is a keyword & LABEL is an
identifier.

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Example:
class Demo
{
public static void main(String args[])
{
FIRST: for(int i=1;i<=3;i++)
{
SECOND: for(int j=1;j<=10;j++)
{
if(j==5)
break FIRST;
System.out.println(j);
}
}
}
}

iii) continue Statement: It passes the control to the next iteration of a loop.
Example:
class Demo
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{

venkatesh.mansani@yahoo.com Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

if(i==5)
continue;
System.out.println(i);
}
}
}

iv) continue LABEL Statement:


It passes the control to the next iteration of specified LABEL loop.

Example:
class Demo
{
public static void main(String args[])
{
FIRST: for(int i=1;i<=3;i++)
{
SECOND: for(int j=1;j<=10;j++)
{
if(j==5)
continue FIRST;
System.out.println(j);
}
}
}
}

venkatesh.mansani@yahoo.com Naresh i Technologies

You might also like