Monday, October 28, 2013

Flow Controls

A program language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java's program control statements can be put into the following categories

  • selection.
  • Iteration
  • jump
Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Iteration statements enable program execution to repeat one or more statement (That is, iteration statements form loops). Jump statements allow your program to execute in a nonlinear fashion, All of java's control statements are examined here.

Java's Selection Statements 

Java supports two selection statements: if and switch, These statements allow you to control the flow of your program's execution based upon conditions known only during run time.

if Statements 

The if statement was introduced earlier. if is examined in detail here. The if statement is java's conditional branch statements it can be used to route program execution through two different paths, Here is the general from of the if statement:
if(condition){
        // Statement One
}else{
      // Statement Two
}

Here each statement may be a single statement or a compound statement enclosed in curly braces (That is, a block ). The condition is any expression that returns a boolean value.The else clause is optional.

The if works like this: if the condition is true, then statement One is executed. Otherwise, Statements Two (if it exists) is executed. in no case will both statements be executed .

Example 
class IfElseStatement{

    public static void main(String[] args) {
     
        int a = 10;
        int b = 5;
     
        if(a>b){
            a = 0;
        }else{
            b = 0;
        }
     
        System.out.println("A and B :"+ a+" "+b);
 
    }
}

Here, if a is less than b, Then a is set zero.Otherwise, b is set zero.In no case are they both set to zero.
Most often, the expression used to control the if will involve the relational operator:  However, this is not technically necessary, it is possible to control the if using a single boolean variable, as shown.

class IfElseStatement{

    public static void main(String[] args) {
     
       boolean b = true;
        if (b) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
 
    }
}
Remember, only one statement can appear directly after the if or the else if you want to include more statements, you'll need to create a block, as shown

class IfElseStatement{

    public static void main(String[] args) {
     
       int x = 10;
       if(x>5){
           int i = 10;
           i+=20;
           System.out.println("i is : "+ i);
       }else{
           int e = 34;
           e+= 45;
           System.out.println("e is :"+ e);
       }
    }
}
Here, both statements withing the if block will execute if date is greater than 30. is is always better to include the curly braces when using the if, even when there is only one statement in each clause, This makes it easy to add and statement later.
class IfElseStatement{

    public static void main(String[] args) {
     
       int date = 10;
   
       if(date > 30){
          changeToNextMoth();
          date = 1;
       }else{
           waitMorDate();
           date+=1;
       }
    }
 
    public static  void changeToNextMoth(){
        int moth = 4;
        moth+=1;
    }
 
    public static void waitMorDate(){
        System.out.println("wait...");
    }
 
}
It seems clear that the statement System.out.println("wait..."); was intended to executed inside the esle clause, because of the indentation level. However , since java doesn't recognize whitespace, and there is no way for the compiler to know what was intended, This will compile fine, but it will behave incorrectly when run. The above example is corrected as follows:
class IfElseStatement{

    public static void main(String[] args) {
     
       int date = 10;
   
       if(date > 30){
          changeToNextMoth();
          date = 1;
       }else{
           waitMorDate();
           date+=1;
       }
    }
 
    public static  void changeToNextMoth(){
        int moth = 4;
        moth+=1;
    }
 
    public static void waitMorDate(){
        System.out.println("wait...");
    }
 
}

No comments:

Post a Comment