Monday 30 January 2012

Control statements in Java

•Control statements in Java are used to alter or change the flow of execution in a program.
•In Java the control statements are classified into:
1) Selection statements / Conditional statements
2) Iteration Statements
3) Jump Statements

Selection Statements:
•Selection statements control the flow of execution based on the truth value of the condition.
•Java supports two selection statements:
1) if
2) switch

Selection Statements (if):
•if statement syntax is as follows:

if(condition)
{
    statement(s);
}
else
{
    statements(s);
}


•If the condition is evaluated to “true”, the statements in the if block are executed.
•If the condition is evaluated to “false”, the statements in the else block are executed



•In the previous syntax, if the condition of “outer if” is true, the statements in “outer if” are executed, then the condition of “inner if” statement is evaluated. If it is true, then statements in “inner if” will be executed otherwise, statements in inner else will be executed.
•If the condition of “outer if” is false, then the statements in outer else block are executed.

Selection Statements (if-else-if):
•The if-else-if ladder is based on the nested if’s concept. The if-else-if ladder is used to test for multiple conditions.

Syntax:
if(condition)
{
    statement(s);
}
else if(condition)
{
    statement(s);
}

else
{
    statement(s);
}


•In the previous syntax, the condition of “if” statement is evaluated. If it is “true” the statements in the “if” block are executed. Otherwise, the condition of the following else-if block will be evaluated and so on…
•If none of the conditions evaluate to true, then the final “else” block will be executed.

Selection Statements (switch):
•“switch” statement is a multi-way branch statement. It is more efficient than a if-else-if ladder. The syntax for “switch” statement is as follows:

switch(expression)
{
    case value1:
      statement(s);
      break;
    case value2:
      statement(s);
      break;
    …
    default:
      statement(s);
}


•In the previous syntax, “expression” must be only of type byte, short, int or char.
•The type of “values” in case statements must match the type of “expression”.
•“break” keyword is used to jump from the current line to the next line after the switch statement.
•“break” and “default” are both optional.
•If we don’t write “break” then all the case statements will be executed.

Iteration Statements:

•The iteration statements in Java are used to iterate/loop through a set of statements.
•In Java there are three iteration statements. They are:
1) while
2)do-while
3)for
4)foreach

Iteration Statements (while):
•“while” loop is used to loop through a set of statements until a certain condition is false. Syntax is as shown below:

while(condition)
{
    statement(s);
}

Iteration Statements (do-while):
•do-while loop is also used to loop through a set of statements like the while loop. But the only difference between do-while and while loops is, all the statements in the do-while loop are executed atleast once.
•Syntax is as shown below:

do
{
    statement(s);
}while(condition);

Iteration Statements (for):
•“for” loop is also used to iterate/loop through a set of statements. Syntax of “for” loop is as shown below:

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

•The “initialization” part of the for loop is executed first, and is executed only once.
•The condition is evaluated next and if it is true, the body of for loop is executed.
•Next either increment or decrement of the initialization variable(s) is performed and again the condition is evaluated and so on…

Iteration Statements (for-each):
•A for-each loop is used when we need to perform a common operation on a collection of elements or objects.
•A for-each loop is an extension of the “for” loop.
•This for-each loop is introduced from JDK 1.5 version.

Syntax of for-each loop is as follows:

for(type var : collection)
{
   statement(s);
}


•The “type” of var must be matching with the “type” of collection.
•A collection can be an array or it can be any of the collections from the “collection framework”.

Example for “for-each” loop:

int a[] = {1,2,3,4,5};
for(int i : a)
{
    System.out.println(i);
}



•In the previous example, ‘a’ is an integer array consisting of elements 1,2,3,4,5.
•The for-each loop iterated through all the elements one by one starting from the first element 1 and ending with last element 5.
•So, when the for-each loop begins, the first element 1 will move into the variable i, and that element is printed to the console. In the next iteration, 2 comes into i and it is printed to the console and so on…

Jump statements:
•The jump statements are used to jump from one part of the program to another part of the program without being based on the evaluation of conditions.
•In Java there are three unconditional jump statements:
1) break
2) continue
3) return

Jump statements (break):
•“break“ keyword has three used in Java. They are:
1) It is used to exit from the case of a switch statement.
2) It is used to break from a loop.
3) It is used as a “civilized” form of goto.




•“break” statement is used to exit from a loop or nested loops.
•Generally “if” statements are used for checking a condition to break from the loop using the keyword “break”.

Example:
for(int i=0;i<10;i++)
{
    if(i==5)
    break;
    System.out.println(i);
}


•In the previous example when i becomes 5, the control exits the for loop and continues with the next line after the for loop.
•So, the output of previous program is:
0
1
2
3
4



•“break” can also be used as an alternative to the “goto” statement.
•Java does not support “goto” as it is supported by “C” language.
•“goto” has certain disadvantages like:
1) By using “goto” in the program, it becomes hard to understand.
2) The program becomes hard to maintain/change.
3) “goto” does not allow certain compiler optimizations.


•There are some situations in which “goto” can be used. One of such situations is, when we need exit from a set of deeply nested loops/blocks.
•So, Java has a another version of “break” which allows it to work in the same way as “goto” works.
•Syntax of the break which supports “goto” is as follows:
break label;
•“label” is any valid identifier name in Java.
•The label must be written outside the enclosing block of the “break” statement.


Jump statements (continue):
•“continue” is used to skip a set of statements inside the body of the loop.
•“continue” can be used inside while, do-while and for loops.
•Similarly as in the case of “break” statement, we will check a condition using “if” statement to use the “continue” statement.
•When continue is used inside a loop, it will transfer the execution to the starting of the loop by skipping all the code after the continue statement.


•Let’s see an example for continue statement:
for(int i=0;i<5;i++)
{
    if(i==3)
    continue;
    System.out.println(i);
}

In the above program, when i becomes 3, the print statement is skipped. So, the output will be:

0
1
2
4

3 is skipped due to continue.

Jump statements (return):
•The “return” keyword is used inside a method, to return a value to the calling method.
•When a return statement is executed, it returns the value to the calling method and exits the called method.
•So, the execution continues with the next line after the calling method.





3 comments:

  1. perfect explanation about object oriented concepts .its very useful.thanks for your valuable information.
    java training in chennai | java training in velachery

    ReplyDelete
  2. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    Data Science Training in Indira nagar
    Data Science Training in btm layout
    Data Science Training in Kalyan nagar
    Data Science training in Indira nagar
    Data science training in bangalore

    ReplyDelete