JAVA Control Flow Statements

Control Flow Statement
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.
This section describes the decision-making statements (if, if...else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language.

1) Decision-making statements:- i) if statement
ii) if....else statement
iii) Nested if....else statement
iv) else if ladder

i) if statement:- The general form of a simple "if" statement is
Syntax:-
    if (text expression)
     {
       statement block;
       }


ii) if....else statement:- The "if....else" statement is an extention of simple "if" statement. The general form is
Syntax:-
    if (text expression)
     {
       True-statement block;
       }
      else
      {
        False-statement block;
       }


iii) Nested if....else statement:- Series of decisions are involved, we may have to use more than one "if....else" statement in nested if else as follows:-
Syntax:-
    if (text condition1)
     {
          if (text condition2)
          {
              Statement block1;
           }
         else
         {
           Statement block2;
          }
       }
      else
      {
        Statement block3;
       }


iv) else if ladder statement:- Series of multiple decisions are involved.
Syntax:-
    if (text condition 1)
       Statement block-1 
     {
          else if (text condition 2)
          {
              Statement block-2;
           }
         else if(condition 3) 
         {
           Statement block-3;
               ..............................
          }
       }
      else if(condition n) 
      {
        Statement block n;
       }
      else
        Default Statement block 

2. Looping Statements:-
i) for loop
ii) while loop
iii) do .. while loop

i) for loop or statement:- The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

Syntax:-
for (initialization; termination; increment/decrement)
   {
    statements block; 
   }
ii) while loop:- The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

Syntax:-
while (expression) 
{
statement block;
}

iii) do .. while loop:-The Java programming language also provides a do-while statement, which can be expressed as follows:

Syntax:-
do {
statement block;
}
while (expression);
JUMP STATEMENTS IN JAVA
Java supports three jump statements: break, continue, and return. These statements transfer control to another part of the program.
Break

The break construct is used to break out of the middle of loops: for, do, or while loop. When a break statement is encountered, execution of the current loops immediately stops and resumes at the first statement following the current loop. That is, we can force immediate termination of a loop, bypassing any remaining code in the body of the loop.
/* Print 1 to 10 using Break Statement Java Example*/
class BreakStatement
{
            public static void main(String args[] )
            {
                        int i;
                        i=1;
                        while(true)
                        {
                                    if(i >10) break;
                                    System.out.print(i+" ");
                                    i++;
                        }
            }
}

Continue

This command skips the whole body of the loop and executes the loop with the next iteration. On finding continue command, control leaves the rest of the statements in the loop and goes back to the top of the loop to execute it with the next iteration (value).
/* Print Number from 1 to 10 Except 5 */
class NumberExcept
{
            public static void main(String args[] )
            {
                        int i;
                        for(i=1;i<=10;i++)
                        {
                                    if(i==5) continue;
                                    System.out.print(i +" ");
                        }
            }
}
Above program will display the value of variable i from 1 to 4. When the value of variable i becomes 5, continue statement will skip the body of the loop following continue statement i.e. it skips System.out.println(i) statement and again executes the loop with the next iteration (value) i.e .. 6.

Return

This statement is mainly used in methods in order to terminate a method in between and return back to the caller method. It is an optional statement. That is, even if a method doesn't include a return statement, control returns back to the caller method after execution of the method. Return statement mayor may not return parameters to the caller method.

2 comments:

  1. Hi, I want to express my gratitude to you for sharing this fascinating information. It's amazing that we now have the ability to share our thoughts. Share such information with us through blogs and internet services.
    Visit site

    ReplyDelete
  2. Most Interesting blog.
    You share great way through your blog to buy online computer and laptop products.
    PayPal Nederland

    ReplyDelete