Flow control in java:
================
The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
Flow control describes the order in which the statements should execute.
Flow controls are categorized into 3 types
1.Selection Statements
===================
a.if
b.if-else
c.switch
2.Iteration Statements
===================
a.while
b.do-while
c.for loop
d.for each loop
3.Transfer statements
==================
a.break
b.continue
c.return
d.try
e.catch
f.finally
1.Selection Statements:
-----------------------------
if statement:
----------------
syntax:
---------
if(condition)
{
;;;
;;;
}
in if condition should be always boolean value,if we are passing other than boolean we will get compile time error.
//sample example on if condition
public class IfDemo {
public static void main(String[] args) {
int a=10,b=20;
if (a<b)
{
System.out.println("a is bigger");
System.out.println("value of a is : " +a);
System.out.println("value of b is : " +b);
}
}
}
If else:
======
syntax:
--------
if(condition)
{
;;;
;;;if body
}
else
{
;;;;else body
;;;
}
in if else,condition always should be boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
if the condition is true then if block will be executed and if the condition is false else block will be executed.
//sample example on if else condition
public class IfElseDemo {
public static void main(String[] args) {
int a=10,b=20;
if (a<b)
{
System.out.println("a is bigger");
System.out.println("value of a is : " +a);
System.out.println("value of b is : " +b);
}
else
{
System.out.println("b is smaller");
}
}
}
switch case:
==========
If you have multiple options in that if you want select a option based then go for switch case
If several options are available then it is recomonded to go with switch case.
syntax:
---------
switch(condition)
{
case name1:
statements;
break;
case name1:
statements;
break;
case name1:
statements;
break;
case name1:
statements;
break;
default:
statements;
break;
}
in switch case condition can be byte,short,char,int,String and enumurated types as well.
case name and condition type should be same other wise we will get compile time error.
if no case is matched then default case will be executed.
break is a statement when ever it finds break,simply it comes out of the switch case.
//sample example on switch case
package blog;
public class SwitchDemo {
public static void main(String[] args) {
int month = 1;
String monthString;
switch (month) {
case 1: monthString = "January";
System.out.println(monthString);
break;
case 2: monthString = "February";
System.out.println(monthString);
break;
case 3: monthString = "March";
System.out.println(monthString);
break;
case 4: monthString = "April";
System.out.println(monthString);
break;
case 5: monthString = "May";
System.out.println(monthString);
break;
case 6: monthString = "June";
System.out.println(monthString);
break;
case 7: monthString = "July";
System.out.println(monthString);
break;
case 8: monthString = "August";
System.out.println(monthString);
break;
case 9: monthString = "September";
System.out.println(monthString);
break;
case 10: monthString = "October";
System.out.println(monthString);
break;
case 11: monthString = "November";
System.out.println(monthString);
break;
case 12: monthString = "December";
System.out.println(monthString);
break;
default: monthString = "Invalid month";
System.out.println(monthString);
break;
}
}
}
Iterative Statements:
=================
There are 4 kinds of iterative statemets are available
1.while
2.do while
3.for loop
4.for each loop
while:
---------
If we dont know the number of iterations in advance then we can go for while loop
syntax:
--------
while(condition)
{
;;;;
;;;;while body
increment/decrement;
}
In while condition should be always boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
If the condition is true then while body will be executed.
//sample example on while loop
package blog;
public class WhileDemo {
public static void main(String[] args) {
int i=1;
while (i<=10) {
System.out.println(i);
i++;
}
}
}
do-while
-------------
If we want to execute loop body atleast once then we can go for do while
syntax:
--------
do
{
;;;;
;;;;
increment/decrement;
}while(condition);
In while also condition should be always boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
//sample example on do while
package blog;
public class DoWhileDemo {
public static void main(String[] args) {
int i=0;
do
{
System.out.println("hello");
i++;
}while(i<10);
}
}
for loop:
------------
If we know the number of iterations in advance then the best suitable loop is for loop.
syntax:
--------
for(initialization section;condition;increment/decrement)
{
;;;
;;;loop body
;;;
}
Initialisation section will be executed only once.
Here we can declare and initialise the variables.
condition should be always boolean,if we are giving other than boolean we will get compile time error.
Based on our requirement we can increment or decrement the values.
//sample example on for loop
package blog;
public class ForLoopDemo {
public static void main(String[] args) {
String string = "selenium ramesh";
for (int i = 0; i < string.length(); i++) {
System.out.println("selenium experts");
}
}
}
for each loop:
===========
By using for each loop we can iterate the values one by one from the collection objects.
syntax:
--------
for(data_type variable : array | collection)
{
;;;
;;;
}
//sample example on for each loop
package blog;
import java.util.ArrayList;
public class ForEachDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(10);
arrayList.add(10);
arrayList.add(20);
arrayList.add(20);
arrayList.add(30);
arrayList.add(30);
System.out.println(arrayList);
//iterating values using for each loop
for (Object object : arrayList) {
System.out.println(object);
}
}
}
Transfer statements:
=================
break:
======
when ever break statement is encountered inside a loop, the loop is immediately terminated.
We can use break statements inside switch and inside loops.
//sample example on break.
package blog;
public class BreakDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i==8) {
break;
}
System.out.println(i);
}
}
}
continue:
========
when ever continuestatement is encountered inside a loop,it skips that current iteration and continues it s execution with out breaking the loop.
//sample program on continue
package blog;
public class BreakDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i==8) {
continue;
}
System.out.println(i);
}
}
}
finally:
======
finally is a keyword which is applicable for
1.classes: when ever you declare class as final we can not perform inheritence ie inheriretence is not possible
2.variables:when ever you declare variable as final we can not perform re assignment of the variable
3.methods:when ever we declare method as final we can not perform over riding.
try:
====
Try is a block .
In try block we need to keep always risky code inside the try block
Try block always associates with catch block.
syntax:
--------
try
{
risky code;
;;
;;
}
catch:
=====
catch is a block,which is always associates with try.
It always catch the exception raised from try block
*****if you need more information on exception handling please visit our post on exception handling******
================
The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
Flow control describes the order in which the statements should execute.
Flow controls are categorized into 3 types
1.Selection Statements
===================
a.if
b.if-else
c.switch
2.Iteration Statements
===================
a.while
b.do-while
c.for loop
d.for each loop
3.Transfer statements
==================
a.break
b.continue
c.return
d.try
e.catch
f.finally
1.Selection Statements:
-----------------------------
if statement:
----------------
syntax:
---------
if(condition)
{
;;;
;;;
}
in if condition should be always boolean value,if we are passing other than boolean we will get compile time error.
//sample example on if condition
public class IfDemo {
public static void main(String[] args) {
int a=10,b=20;
if (a<b)
{
System.out.println("a is bigger");
System.out.println("value of a is : " +a);
System.out.println("value of b is : " +b);
}
}
}
If else:
======
syntax:
--------
if(condition)
{
;;;
;;;if body
}
else
{
;;;;else body
;;;
}
in if else,condition always should be boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
if the condition is true then if block will be executed and if the condition is false else block will be executed.
//sample example on if else condition
public class IfElseDemo {
public static void main(String[] args) {
int a=10,b=20;
if (a<b)
{
System.out.println("a is bigger");
System.out.println("value of a is : " +a);
System.out.println("value of b is : " +b);
}
else
{
System.out.println("b is smaller");
}
}
}
switch case:
==========
If you have multiple options in that if you want select a option based then go for switch case
If several options are available then it is recomonded to go with switch case.
syntax:
---------
switch(condition)
{
case name1:
statements;
break;
case name1:
statements;
break;
case name1:
statements;
break;
case name1:
statements;
break;
default:
statements;
break;
}
in switch case condition can be byte,short,char,int,String and enumurated types as well.
case name and condition type should be same other wise we will get compile time error.
if no case is matched then default case will be executed.
break is a statement when ever it finds break,simply it comes out of the switch case.
//sample example on switch case
package blog;
public class SwitchDemo {
public static void main(String[] args) {
int month = 1;
String monthString;
switch (month) {
case 1: monthString = "January";
System.out.println(monthString);
break;
case 2: monthString = "February";
System.out.println(monthString);
break;
case 3: monthString = "March";
System.out.println(monthString);
break;
case 4: monthString = "April";
System.out.println(monthString);
break;
case 5: monthString = "May";
System.out.println(monthString);
break;
case 6: monthString = "June";
System.out.println(monthString);
break;
case 7: monthString = "July";
System.out.println(monthString);
break;
case 8: monthString = "August";
System.out.println(monthString);
break;
case 9: monthString = "September";
System.out.println(monthString);
break;
case 10: monthString = "October";
System.out.println(monthString);
break;
case 11: monthString = "November";
System.out.println(monthString);
break;
case 12: monthString = "December";
System.out.println(monthString);
break;
default: monthString = "Invalid month";
System.out.println(monthString);
break;
}
}
}
Iterative Statements:
=================
There are 4 kinds of iterative statemets are available
1.while
2.do while
3.for loop
4.for each loop
while:
---------
If we dont know the number of iterations in advance then we can go for while loop
syntax:
--------
while(condition)
{
;;;;
;;;;while body
increment/decrement;
}
In while condition should be always boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
If the condition is true then while body will be executed.
//sample example on while loop
package blog;
public class WhileDemo {
public static void main(String[] args) {
int i=1;
while (i<=10) {
System.out.println(i);
i++;
}
}
}
do-while
-------------
If we want to execute loop body atleast once then we can go for do while
syntax:
--------
do
{
;;;;
;;;;
increment/decrement;
}while(condition);
In while also condition should be always boolean ie eighther true or false.if we are passing other than boolean we will get compile time error.
//sample example on do while
package blog;
public class DoWhileDemo {
public static void main(String[] args) {
int i=0;
do
{
System.out.println("hello");
i++;
}while(i<10);
}
}
for loop:
------------
If we know the number of iterations in advance then the best suitable loop is for loop.
syntax:
--------
for(initialization section;condition;increment/decrement)
{
;;;
;;;loop body
;;;
}
Initialisation section will be executed only once.
Here we can declare and initialise the variables.
condition should be always boolean,if we are giving other than boolean we will get compile time error.
Based on our requirement we can increment or decrement the values.
//sample example on for loop
package blog;
public class ForLoopDemo {
public static void main(String[] args) {
String string = "selenium ramesh";
for (int i = 0; i < string.length(); i++) {
System.out.println("selenium experts");
}
}
}
for each loop:
===========
By using for each loop we can iterate the values one by one from the collection objects.
syntax:
--------
for(data_type variable : array | collection)
{
;;;
;;;
}
//sample example on for each loop
package blog;
import java.util.ArrayList;
public class ForEachDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(10);
arrayList.add(10);
arrayList.add(20);
arrayList.add(20);
arrayList.add(30);
arrayList.add(30);
System.out.println(arrayList);
//iterating values using for each loop
for (Object object : arrayList) {
System.out.println(object);
}
}
}
Transfer statements:
=================
break:
======
when ever break statement is encountered inside a loop, the loop is immediately terminated.
We can use break statements inside switch and inside loops.
//sample example on break.
package blog;
public class BreakDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i==8) {
break;
}
System.out.println(i);
}
}
}
continue:
========
when ever continuestatement is encountered inside a loop,it skips that current iteration and continues it s execution with out breaking the loop.
//sample program on continue
package blog;
public class BreakDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i==8) {
continue;
}
System.out.println(i);
}
}
}
finally:
======
finally is a keyword which is applicable for
1.classes: when ever you declare class as final we can not perform inheritence ie inheriretence is not possible
2.variables:when ever you declare variable as final we can not perform re assignment of the variable
3.methods:when ever we declare method as final we can not perform over riding.
try:
====
Try is a block .
In try block we need to keep always risky code inside the try block
Try block always associates with catch block.
syntax:
--------
try
{
risky code;
;;
;;
}
catch:
=====
catch is a block,which is always associates with try.
It always catch the exception raised from try block
*****if you need more information on exception handling please visit our post on exception handling******
No comments:
Post a Comment