Gadgets

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

No comments:

Post a Comment