Catch us on

Wednesday, March 30, 2016

Loops in java

Hello guys today I will tell you something about loops used in java for repetitions. Suppose you have to print your name 100 times on the output screen. Then it would be little cumbersome to use System.out.println(“My name is XYZ”);  100 times so it could achieved easily using loops.
Java has three kinds of loops:-
Ø  for loop: this loop is used when we know the no. of repetitions to be done. for loop has three statement  1.instialisation 2.condition 3.updation.
Syntax :   for(initialization;condtion;updation)
                 {
                    //Statements to be executed
                }
Example : for(i=1;i<=100;i++)
                         {
                         System.out.println(“My name is XYZ”);
                         }
This statement will print ypu name for 100 times.
Ø  while loop: this loop can also be used for above task but it is generally used when we do not know the number of repetitions.
Syntax :   while(condition)
              {
                    //Statements to be executed
                }
Example: int i=1;
                 while(i<=100)
                      {
                          System.out.println(“My name is XYZ”);
                          i++;
                     }
Ø  do-while loop: this loop is used when need to execute any statement atleast one time and further repetitions depend upon some condition.
Syntax: do
                    {
                    //Statements to be executed
                }while(condition);
If we use do-while loop for repetitions then first compiler will execute the statement written inside the do braces without checking any conditions after that further repetitions would be carried on if the condition present inside the while part is true.
 Example: int i=1;
                do
                      {
                          System.out.println(“My name is XYZ”);
                          i++;
                     }while(i<=99);

THANK YOU FOR READING THIS ARTICLE.
PLEASE SHARE IT ON FACEBOOK,TWITTER AND OTHER SOCIAL NETWORKS AND RECOMMEND YOUR FRIENDS TOO.
Kishan Agarwal



Please Share


No comments:

Post a Comment