Ads

Sunday, 8 October 2017

Control Statement || Select, Sequence & Loop


A program executes from top to bottom except when we use control statements, we can control the order of execution of the program, based on logic and values.

A)Selection Statement :

It allows you to control the flow of program code execution on the basis of outcome of an expression.

Selection statements can be divided into the following categories:
  • If-else statements
  • If-else-if statements
  • Switch statements
1)The if...else  statement : 

In if-else statements, if the specified condition in the if statement is false, then the statement after the else keyword (that can be a block) will execute.


Syntax :

               if(condition)
               {
                               // Statement;
                }
                else
                {
                               // Statement;
                }

2)The if...else...if statement :

This statement following the else keyword can be another if or if-else statement.
That would look like this:

if(condition)

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

Whenever the condition is true, the associated statement will be executed and the remaining conditions will be bypassed. If none of the conditions are true then the else block will execute.

 3) The Switch statements :

The switch statement is a multi-way branch statement. The switch statement of Java is another selection statement that defines multiple paths of execution of a program. It provides a better alternative than a large series of if-else-if statements.


syntax :


                   switch(Condition)
                   {
                               case 1:
                                               ...........
                                               ...........
                              
                               case 2:
                                               ...........
                                               ...........
                              
                               case 3:
                                               ...........
                                               ...........
                               
                               case N:
                                               ...........
                                               ...........                              
                               default:
                                               ...........
                                               ...........
                        }

B) Jump Statement :

There are 3 types of jump statement in java. These statements are used to change the flow of program i.e to jump to another location of the program.

#1)Break  :
The one use of the break in switch statement we have seen. It can be used to break other also. When you use to break in a loop it simply terminates that loop unconditionally. So, mainly these are uses of break statement :

         1)Switch
         2)Terminate a loop
         3)Transfer control at a particular statement

#2)Continue :
This statement skips the current iteration in the loop and continues the loop to the next iteration. The So, when we want to skip some statements based on a condition. Continue statement can use.

 #3)Return  :
This statement causes the control to be transferred back from a method to the caller of the method.
The return statement is used in main() method so it will be returned to the Java run-time system as it calls the main method.





C) Looping  Statement :

Java provides the following looping statements:   

#1)For Loop Statement : 

A for loop executes a statement (that is usually a block) 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(intialize; Condition; incre../decre..)
{
       //Statement;
}

#2)While loop statement :
It continually executes a statement (that is usually a block) while a condition is true. The condition must return a boolean value. 

Syntax :

while(Condition)
{
     //Statement;
}

#3) Do...while loop statement :
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(condition)
{
          //Statement;
}while(Condition);

 


                            

Post a Comment

Click Here