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);
    }

Wednesday, 20 February 2013

while,do-while,for loop statments


Control statements: - Control statements are the statements.This statement are change the flow of execution.
1)      if-else statement
2)      switch statement
3)      break statement
4)      continue statement
5)      return statement
6)      while loop
7)      d0-while loop
8)      for loop

while loop: -This loop is useful to repeatedly execute a group of statements as long as a given condition is true.It first checks condition and then excute statement.
Syntax: -            while(condition)
             {
                                     Statements;
                      }
Example Program:-
public class While_Loop {
    public static void main(String a[]){
        int i=10;
        while(i<=20){
            System.out.println("numbers between 10 to 20 :"+i);
            i++;
        }
    }
}
do-while loop: - This loop repeatedly execute a group of statements as long as a condition is true.It first excutes statement  and then checks condition.
Syntax: -               do{
                                   Statements;
                     }
while (condition);
Example Program:-
public class Do_While {
    public static void main(String a[]){
        int i=10;
        do{
            System.out.println("numbers between 10 to 20 :"+i);
            i++;
        }while(i<=20);
    }
}
for loop: -
                This is used to repeatedly execute a group of statements as long as a condition is true.
Syntax: -
                for(init;cond;modi)
{
                Statements;
}
Init  is called initializing expression.
Cond  is called conditional expression.
Modi  is called modifying expression.
Example Program:-
public class For_Loop {
    public static void main(String a[]){
        int i;
        for(i=1;i<=10;i++){
            System.out.println(+i);
        }
    }
   
}