Friday, August 11, 2017

Control Statements


Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution.

Types of control statements

  1. selection control statements.
  2. iteration control statements.
  3.  jump control statements.  

1. Selection control statements

Selection statements can be divided into the following categories.

  1. The if statements
  2. The if-else statements
  3. The if-else-if statements
  4. The switch statements

1. If statement

if statement performs a task depending on whether a condition is true or false.

Syntax:

                                    if (condition)   

                                    statement1;

2. The if-else statements.

if the specified condition in the if statement is false, then the statement after the else keyword  will execute.

Syntax:

if (condition)  

 statement1;  

else  

statement2;



3. The if-else-if statements

This statement following the else keyword can be another if or if-else statement.

Syntax

if(condition)
   statements;
else if (condition)
   statements;
else if(condition)
   statement;
else
   statements;



4. The Switch Statements

When there are several options and we have to choose only one option from the available ones, we can use switch statement.

Syntax:

switch (expression)

   {

 case value1: //statement sequence     

break;   

case value2: //statement sequence

break;      

 ………….…..   

case valueN: //statement sequence  

   break;   

default: //default statement sequence 

 } 



2. Iteration Statements

Repeating the same code fragment several times until a specified condition is satisfied is called iteration.

the following loop for iteration statements

  1. The while loop
  2. The for loop
  3. The do-while loop
  4. The for each loop





1. The while loop

It continually executes a statement while a condition is true. The condition must return a Boolean value.

Syntax:

while (condition)  

{   

statements; 

 }



2. The do-while loop

The only difference between a while and a do-while loop is that do-while evaluates its expression at the bottom of the loop instead of the top. The do-while loop executes at least one time then it will check the expression prior to the next iteration.

Syntax

      do  

        {  

             //Statements

        }  

        while ( condition);  



3. The for loop

A for loop executes a statement as long as the Boolean condition evaluates to true. A for loop is a combination of the three elements initialization statement, Boolean expression and increment or decrement statement.

Syntax:

for(<initialization>;<condition>;<increment or decrement statement>)

     {     
//block of code
}



4. The For each loop

This was introduced in Java 5. This loop is basically used to traverse the array or collection elements.

Syntax

for(data_type variable : array | collection)

{

}  







3. Jump Statements

Jump statements are used to unconditionally transfer the program control to another part of the program.

Java provides the following jump statements

  1. break statement
  2. continue statement
  3. return statement



1. Break Statement

The break statement immediately quits the current iteration and goes to the first statement following the loop.

2. Continue Statement

The continue statement is used when you want to continue running the loop with the next iteration and want to skip the rest of the statements of the body for the current iteration.

3. Return Statement

The return statement is used to immediately quit the current method and return to the calling method. It is mandatory to use a return statement for non-void methods to return a value.

No comments:

Post a Comment