Aug 26, 2011

Control Statements in Java

A program is a group of statements that are executed to achieve a predetermined task. Statements in a program are generally executed in a sequential manner, which is called sequential execution or sequential control flow. However, by putting the decision-making statement in the program, the normal flow of the program can be controlled. Statements that control the flow of the program are called control statements.
Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program. In Java, control statements can be divided into following three categories:
               1.   Selection statements
               2.   Iteration statements
               3.   Jump statements
Selection statements in Java
            Selection statements are used in a program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Java supports two selection statements: if and switch.  

The if statement
         The if statement is a conditional statement. It is used to execute a statement or group of statements based on some condition. The general form of the if statement is given below:
                        
                       if (condition)
                          statement1;
                       else
                         statement2;
The condition to the if statement must be an expression that evaluates to a boolean value. If the condition evaluates to true, the statement following the if statement is executed. However, if more than one statement is needed to be executed, then these statements must be put within a pair of curly braces forming a block of code. The else statement is optional, but if it is present, it must be placed immediately after the code block attached to the if statement. The code block attached to the else statement is executed when the condition to the if statement evaluates to false.
Argument to the if statement
           The argument to the if statement must be an expression that evaluates to a boolean value. Therefore, for the declaration int a = 1, the following statements show some of the valid boolean expressions to the if statement:                        
                               if (a > 1)
                                System.out.println(“Greater than 1″);
                               if (a < 1)
                               System.out.println(“Less than 1″);
                               if (a != 1)
                               System.out.println(“Not equal to 1″); 

The switch statement
             The switch statement is used to select multiple alternative execution paths depending on the value of a variable or expression. The general form of a switch statement is given below:
                              
                              switch(expression){
                              case val1:
                                             code_block1
                              case val2:
                                             code_block2
   
                              default:
                                            code_default;
                             }
where, the expression to the switch must be of a type byte, short, char, or int. A code block is attached to the switch statement that contains multiple case statements and an optional default statement. Each of the values (following the case statements) must be unique within the code block and must be assignment compatible with the type of the expression without the loss of information.
The switch statement executes by comparing the value of the expression with each of the constants after the case statements. If a match is found, the statements following that case statement are executed. Otherwise, the statements following the default statement (if present) are executed. Conventionally, the default label is put after all the case labels, but it may appear anywhere in the block.
The break statement is used within the code block attached to the switch statement to terminate a statement sequence.
The argument to the switch must be an integral expression that must evaluate to a 32-bit or smaller integer type: byte, short, char, or int. Moreover, the legal range of the argument’s data type must cover all the case constants used in the code block. For example, the following code fragment will not compile successfully:
                                               
                    byte b = 10;
                    switch( b ){
                             case 10 :
                             System.out.print(“ten”);
                             break ;
                             case 1000 :
                             System.out.print(“thousand”) ;
                             break ;
                          }

In the given code, the compiler will object to the value 1000 because it is not in the range of a byte data type.
The constants following the case statements may be integer literals or they can be variables defined as static and final. Moreover, these constants must be unique within the code block. For example, the following code block will not compile successfully:
                                              
                        1.    byte b = 10;
                        2.    switch( b ){
                        3.       case 10 :
                        4.                   System.out.print(“ten”);
                        5.                   break ;
                        6.       case 10 :
                        7.                   System.out.print(“10″) ;
                        8.                   break ;

                        9.    }
In the given code, the compiler will object to the second appearance of the value 10 in line number 6.
Iteration statements
        Iteration statements enable program execution to repeat one or more statements, i.e., iteration statements form loops. In Java, there are three iteration statements: for, while, and do.  

The for statement
          The for loop is the most versatile looping construct. It is used to continuously execute a block of code until a particular condition is satisfied. It comprises three parts: initialization, condition, and iteration.
The initialization portion is generally an expression that sets the value of the loop control variable. The loop control variable acts as a counter and controls the execution of the loop. The initialization portion executes only once. The condition portion must be a boolean expression. It usually tests the loop control variable against a target value and hence works as a loop terminator. The iteration portion is usually an expression that increments or decrements the loop control variable.The general form of the for loop is:
                                       
                                             for(initialization; condition; iteration){
                                                   //body of the loop
                                             }
All the three components, i.e., initialization, condition, and iteration are optional. In case there is only a single statement in the body of the loop, the curly braces can be omitted.
The for loop executes in the following three steps:
              1.When the loop first starts, the initialization expression is executed and then the control is transferred to step 2.
                   2. The condition is evaluated. If the condition evaluates to true, the body of the loop executes and the program control is transferred to step 3. If the condition evaluates to false, the loop terminates.
                      3.The iteration expression executes and then the control is transferred to step 2.

The while statement
         The general form of the while loop is as follows:
                                              while(condition){
                                                   block
                                               }
where, the condition may be any expression that evaluates to a boolean value. The code block attached to the while statement is repeatedly executed unless the condition evaluates to false.
 
The do statement
          The general form of the do statement is given below:
                                                do{
                                                      code_block
                                                     } while(condition)
This statement is similar to the while statement discussed above. The only difference in this case is that the code block attached to the do statement is executed before the condition is checked. Therefore, even in the case of the condition evaluating to false, the code block executes at least once.
Jump statements
          Jump statements are used to unconditionally transfer the program control to another part of the program. Java has three jump statements: break, continue, and return.

The break statement
         A break statement is used to abort the execution of a loop. The general form of the break statement is given below:
                                   
                                                 break label;
It may be used with or without a label. When it is used without a label, it aborts the execution of the innermost switch, for, do, or while statement enclosing the break statement. When used with a label, the break statement aborts the execution of any enclosing statement matching the label.
Note: A label is an identifier that uniquely identifies a block of code.   

The continue statement
        A continue statement is used to alter the execution of the for, do, and while statements. The general form of the continue statement is given below:
                                                  continue label;
It may be used with or without a label. When used without a label, it causes the statement block of the innermost for, do, or while statement to terminate and the loop’s boolean expression to be re-evaluated to determine whether the next loop repetition should take place. When used with a label, the continue statement transfers control to an enclosing for, do, or while statement matching the label.  

The return statement
           A return statement is used to transfer the program control to the caller of a method. The general form of the return statement is given below:
                                                  return expression;
If the method is declared to return a value, the expression used after the return statement must evaluate to the return type of that method. Otherwise, the expression is omitted.

0 comments :

Post a Comment