Java ControlStatements

   
 The control statement are used to controll the flow of execution of the program . This execution order depends on the supplied data values and the conditional logic. Java contains the following types of control statements:

1- Selection Statements
2- Repetition Statements
3- Branching Statements 

Selection statements:

 If Statement:

 This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed .

Syntax:
  if(conditional_expression)
  {
    <statements>;
    ...;
    ...;
   }

  If-else Statement:
   
The "if-else" statement is an extension of if statement that provides another option when 'if' statement evaluates  to "false" i.e. else block is executed if "if" statement is false.  

Syntax:
   if(conditional_expression){
  <statements>;
  ...;
  ...;
  }
   else{
  <statements>;
  ....;
  ....;
   } 

Switch Statement:
This is an easier implementation to the if-else statements. The keyword "switch" is  followed by an expression that should evaluates to byte, short, char or int primitive data types ,only. In a switch block there can be one or more labeled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed ,if no case matches then the default statement (if present) is executed.

Syntax:
  switch(control_expression){
  case expression 1:
  <statement>;
  case expression 2:
  <statement>;
   ...
   ...
  case expression n:
  <statement>;
  default:
  <statement>;
  }//end switch


Repetition Statements:

while loop statements:

This is a looping or repeating statement. It executes a block of code or statements till the given condition is true. The expression must be evaluated to a boolean value. It continues testing the condition and executes the block of code. When the expression results to false control comes out of loop. 


Syntax:
   while(expression){
  <statement>;
  ...;
  ...;
  }

do-while loop statements:

This is another looping statement that tests the given condition past so you can say that the do-while looping statement is a past-test loop statement. First the do block statements are executed then the condition given in while statement is checked. So in this case, even the condition is false in the first attempt, do block of code is executed at least once.

Syntax:
  do{
  <statement>;
  ...;
  ...;
  }while (expression);

for loop statements:

This is also a loop statement that provides a compact way to iterate over a range of values. From a  user point of view, this is reliable because it executes the statements within this block repeatedly till the specified conditions is true .

Syntax:
    for (initialization; condition; increment or decrement)
{
  <statement>;
  ...;
  ...;
  }

initialization: The loop is started  with the value specified.
condition: It evaluates to either 'true' or 'false'. If it is false then the loop is terminated. 
increment or decrement: After each iteration, value increments or decrements. 

Branching (or) Jump Statements:

Break statements: 

The break statement is a branching statement that contains two forms: labeled and unlabeled. The break statement is used for breaking the execution of a loop (while, do-while and for) . It also terminates the switch statements. 

Syntax:
   break;  // breaks the innermost loop or switch statement.
   break label;   // breaks the outermost loop in a series of nested loops.

This is a branching statement that are used in the looping statements (while, do-while and for) to skip the  current iteration of the loop and  resume the next iteration . 

Syntax:
  continue;

Return statements:

It is a special branching statement that  transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that can not return. the returned value type must match the return type of  method. 

Syntax:
 return;
return values;
return;   //This returns nothing. So this can be used when method is declared with void return type.
return expression;   //It returns the value evaluated from the expression.





أحدث أقدم