Control statements

1/5/17

  • Control statements use selection,iteration and jump.
  • selection program allows your program to different path of execution based upon the outcome of the expression.
  • Iteration statements enable the program execution to repeat one or more statements.
  • Jump statements allows your program to execute in non linear fashion.

If statements:

  • Each statement may be single statement or compound statement enclosed in curly faces or blocks.The condition is any expression that returns a boolean value.
  • If condition is true then statement 1 is executed otherwise statement 2 is executed .In other case both statements be executed.

              int a , b;
    
           if    (a>b)  a=0;
    
            else    b=0;
    

Nested ifs:

  • It is an if statement that is the target of another if or else.Nested ifs are very common in programming.
  • When you nest ifs the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else.

    if (i==10){
    If ( j<20) a=b;
    if (k< 100) c=d;
    else a=c;
    }
    a=d;
    

The if else if ladder:

  • The if statement are executed as top down.
  • One of the conditions controlling the if is true,the statement associated with that if is executed.and the rest of ladder is byepassed.
  • If none of the conditions is true then the final else statement is executed.

Switch:

  • Java's multiway branch statement.
  • It provide a easy way to dispatch execution to different parts of your code based on the value of expression.
  • Java prior to Java JDK7 expression must be of type byte,int,short,char or enumeration.

Examples:

Switch (expression){
case value 1:
// statement sequence
break;
.
.
case value n:
// statement sequence
break;
default:
default  statement;
}

Nested Switch statements:

  • You can use a switch as part of the statement sequence of outer switch.This is called nested switch.
  • Switch defines its own block,no conflict arise between the case contains in the inner switch and in the outer switch.
  • Two case constants have same switch can have identical value.
  • A switch statement is usually more efficient than a set of nested ifs.

Iteration Statements:

  • Statements are while,dowhile and for.
  • These statements create what we commonly called loops.

While:

  • It repeat a statement or blockwhile controlling expressions is true.
  • General form

               While ( condition)
               {
               body of loop
               }
    
  • The condition can be any boolean expression

  • The body of the loop will be executed as long as the conditional expression is true.

  • If condition becomes false the control passes to the next line of the code after following loop.

  • While (i++ < --j);

  • Until the value of i is equal to or greater than j the loop runs and compare each other values.

Do while:

  • The do while loop executes the body of loop and evaluates the conditional expression by each iteration.
  • The general form

     do{
    
       body of loop
       }while(condition);
    

Example:

do{

  System.out.println("tick" +n);

  n--;
  }while(n>0);
  • It can be further written as (--n>0) which combines the decrement of n and the test for zero in one expression.

For:

  • The general form

               for(initialization; condition:iteration){
               //body
    }
    
  • Initialization (when loop starts initialization portion of loop is executed and set the value of loop control variable that controls the counter(number of times execute)
  • Condition evaluate a boolean expression
  • iteration simply increment or decrement the loop controlled variable.

Declaring the loop control variable inside the for loop:

  • It is possible to declare the variable inside the initialization portion of the for.
  • The general syntax is

                           for(int n=10;n<0;++n)
    
  • when loop control variable is not needed means declare the loop inside the for.

Using the comma:

  • It will be include where more than one statement in the initialization and iteration portion of the for loop.

Example:

       int a,b;
       for(a=4,b=5;a<b;a++,b--)

Loop variations:

  • It do not need to be used for only those purposes.
  • The condition controlling for loop can be any boolean expression
  • The body of syntax:
boolean done=false
for(int i=1;!done;i++){
//.....
if(   interrupted()  )done=true;
}

For each version of the loop:

  • It is introduced in java5 or JDK5.
  • A each style loop is designed to cycle through collections of objects such as arrays
  • it also use C# implement a for each loop using keyword foreach.
  • In these no new keyword is required and no preexisting code is broken.

The general form of for each version is

   for(type itr-var:collection) statement block.

Iterating over Multidimensional arrays:

  • Each iteration obtain a next array not an individual element.the object obtained will be array of N dimensions to N1 dimensions.

Applying Enhanced For:

  • The for can only cycle through an array sequentially from start to finish.You might think that use is limited.
  • A large number of algorithms are require exactly these mechanism.For loop is used for searching an unsorted array for a value.
  • It run until the value is found.

Example:

int a[]={2,5,6,7};
int val=5;
boolean found=false;
for(int a: val){
found=true;
break;

Jump statements:

Using break:

  • It terminate a state sequence in switch statement.
  • It exit a loop.
  • It can be used as a civilized form of goto commands.

Using break to exit a loop:

  • By using break statement u can terminate the loop bypassing the conditional expression and any remaining code in body of loop.

  • Break in inside loop means it terminates the loop and program control resumes at next statement.

  • The break statement in inner loop causes termination of the loop.And outer loop is unaffected.

  • Too many break statement may have the tendency to destruct the code.

Example:

if( i==10) break;

Using a break as a form of goto:

  • Java does not have goto statements because it provide a way to branch in an arbitrary and unstructured manner.
  • The goto codes are difficult to understand and maintain.

Using continue:

  • Might want to continue running the loop but stop processing the remainder of the code in its body for iteration.
  • In while and do-while loop the control can be transferred directly to conditional expression that control the loop.
  • The syntax

                                   If( i%2==0) continue;
    
  • The code uses % operator to check i is even.

Return:

  • It is the last control statement.
  • It causes a program control to transfer back to caller of the method.As it categorized jump statement.
  • A return statement can also return a value to the calling function.
  • The syntax

          If(t) return;
    

results matching ""

    No results matching ""