Gadgets

Thursday, 21 February 2013

Java Control Statements


if-else statement: - This statement is useful to execute a task. Task means 1 or more statements depending upon whether a condition is true or not.this statement execute only one time
Syntax: -          if(condition)
                       {
                        Statements 1;
                       }
                       else
                            {
                        Statements 2;
                           }
Example Program:-
public class IfCondition {
    public static void main(String args[])
    {
        int a=30,b=20;
        if(a>b)
        {
           System.out.println("a is greater");
        }
        else{
            System.out.println("b is greater");
        }
    } 
}
 switch statement: - switch statement is useful to execute a particular task from several available tasks depending upon the value of a variable.
Break is statement to come out from a switch statement.
Switch statement is useful to handle menu driven programs. Menu means a list of items or options for the user select.

Syntax: -
            switch(var)
{
            case value1:
                        Statements 1;
                        break;
            case value2:
                        Statements 2;
                        break;
                        |
                             |
            case value n:
                        Statements n;
                        break;
            default :
                        Statements;
}
Example Program:-
public class Switch_Statement {
    public static void main(String a[])
    {
     char item='c';
     switch(item){
         case 'p':
             System.out.println("pen");
             break;
         case 'c':
             System.out.println("computer");
             break;
         case 'b':
             System.out.println("book");
             break;
         default:
           System.out.println("no item"); 
              }
    }
  }
break statement: -
             it is useful to come out of a loop. it is useful to come out of a switch statement.
             it is used in a nested blocks to go to end of a block
Syntax: -
            break  blockname;
Example Program:-
public class Break_Statement {
    public static void main(String a[]){
        int i;
        for(i=1;i<=10;i++){
            if(i>5){
                break;
                      }
        System.out.println(+i);
    }
    }
}
Continue statement: -
            This statement is useful to continue the next repetition of a loop. When the statement will subsequent statements in the loop are not executed.
Example Program:-
public class Continue_state {
    public static void main(String a[]){
        int i;
        for(i=1;i<=10;i++){
            if(i<5){
                continue;
                      }
        System.out.println(+i);
    }
    }
}

return statements: - it is used in 2 ways.
            1) It is used to come out of a method to a calling method.



Note: - If return is used in main the entire program is terminated.
            2) return statement can be used to return some value to the calling method.
Example program: -
public class Return1_Statement {
    public static void main(String args[])
    {
        BankAccount ba=new BankAccount();
        ba.input("narendar",50000);
        ba.deposit(10000);
        ba.display();
    }
   
}
class BankAccount{
    int balance,total;
    String acc_name;
    void input(String acc_name,int balance){
     
       this.acc_name=acc_name;
       this.balance=balance;
    }
    int deposit(int amt){
      total=amt+balance;
      return total;
    }
    void display(){
        System.out.println("account name :"+acc_name);
        System.out.println("Balance :"+total);
    }

No comments:

Post a Comment