Sunday, 2 December 2012

Loops

Loops
There will be a situation where we want to repeat  particular statements again and again. For this we have to write those statements again, instead we use the loops to do the same.
To print your name 10 times without using a loop goes like this:
class example
{
  public static void main( )
    {
       System.out.println(“  BCBS”);                    
       System.out.println(“  BCBS”); 
       System.out.println(“  BCBS”);                       10 times it should be repeated.
      System.out.println(“  BCBS”);
      ………………………
    ……………………….
    …………………………
}
}
Using loops we simplify the program.
Types of Loops :
1.     for loop
2.     while  loop
3.     do while loop

for loop
for(intial value ; condition ; update)
note there is no semicolon after for statement.
The above program can be written like this.
class example
{
   public static void main( )
     {
         int i;
          for(i=1; i <=10 ;i++)                    
        System.out.println(“  BCBS”);  
    }       
}   
The initial value is 1, BCBS will be printed  since the condition is true, i.e  i is less than 10. Then it gets incremented to 2, again condition is true so BCBS will be printed again. It goes on till I is equal to 10. Once it is updated the value becomes 11, which does not satisfy the condition. so the loop ends here, control goes to the next line.
while loop
This the entry controlled loop, because the loop will be executed only if the condition is true.
initial value;
while(condition)
{
      update;
}
Note there is no semicolon after while statement.
class example
{
   public static void main( )
     {
         int i=1;                                initial value is 1.
       while(i<=10)                 loop will be executed as long as i remains less than 10.
        {

 
       System.out.println(“  BCBS”);  
        i++;                       i is incremented by 1.
        }
    }       
}   
after i++ , the control will go back to while statement to check the condition.
do while loop
This loop is called exit controlled loop. Because the statements will be executed once ,even if the condition is false .In the end of the loop, it checks the condition.
initial value;
do
{
                  update;
} while(condition) ;                           There is a semicolon.
1.    

    s=x/1 + x/2 + ..... x/n
class sum
     {
       public static void main( int x,int n)            x and n values should be given
         {
                int i, s=0;          initial value should be given.
               for(i=1;i<=n;i++)
                   s=s+(x/i);
                 System.out.print(“Sum=”+s);
        }
   }
Note : if the question is 
+  then
s=s+(Math.pow(x,i)/i);
2.     Separate the digits in a number.
(e.g) 139,
1, 3,9 should be printed separately.
while(n>0)
{
   d= n % 10;           139 % 10 gives the remainder, that is 9.
  System.out.println(d);
  n=n / 10;          Gives the quotient ,that is 13
} 
since n is above zero it keeps repeating the statements, finally when n is less than zero ,it skips the loop.
Using the above logic the sum of the digits, reverse the number programs can be done.
3.     Fibonacci series :
0, 1, 1, 2, 3, 5, 8, 13 …………………….
Add the previous two numbers to get the third number.
a=0;
b=1;
System.out.println(a );
System.out.println(a );
ctr=3;         since two numbers are given already, the counter should start                     
                   from 3,  
while(ctr<=n)
{
         c=a+b;
         System.out.println(c);
         a=b;
         b=c;                 swap the values.
         ctr++;
}
  
  a
   b
  c=a+b
   0
    1
      1
   1
    1
      2
   1
    2
      3



   

 

     




No comments:

Post a Comment