Loop Concept in Java:-

6

Loop Concept in Java:-

It is used to display range based program using iterative statements under the repeatable block.

this block will be repeated until the condition will false.

It is used to display range based data, for example if we want to display 1 to 100 or 100 to 1 then we use Loop Statement.

Loop is also called Control Structure. 


Loop uses three statements mostly.
         
                                        Forward                                                 Backward


1  Initialization                  i=min                                                     i=max



2 Condition                       i<=max                                                  i>=min


3 Iteration                         i=i+1 or i++                                             i=i-1 or i--



Type of Loop:-


1 Entry Control:-

   First Check Condition Then execute Statement

   1.1  While Loop:-


This loop will check the condition first then execute the statement. while loop mostly used in an in-finite condition-based program in java.


The syntax for Finite While loop:-

init;

while(condition)
{
     statement;
     increment;
}


Example of Finite While Loop:-

class WhileExample
{
   public static void main(String args[])
   {
        int i=10;
        while(i>1)
        {
              System.out.println("Shiva " + i);
              i--;

        }


   }



}
...............................................................................

In-Finite While Loop:-

while(true)
{
     if(condition)
       break;

}



Example of In-finite Loop with break using While:-

class WhileExample
{

   public static void main(String args[])
   {
         int i=1;
         while(true)
         {
              System.out.println(i);
              if(i>=10)
               break;
           
             i++; 

         }

   }



}


Infinite Loop Program Without using Break Statement

class WhileExample
{

   public static void main(String args[])
   {
         int i=1;
         boolean f=true;
         while(f)
         {
              System.out.println(i);
              if(i>=10)
               f=false;
           
             i++; 

         }

   }



}

   1.2  For:-

using for loop we can provide simple syntax structure as compare to do--while and while loop.

for loop contain all sub-statement using a single statement.

we can easily create a nested pattern using for loop.
       1    2              4
for(init;condition;iteration)
{
     3
     statement;
}


note:-  we always use for loop for finite-condition based programs.


But if the interviewer asks to create infinite for loop then you can create:-


class LoopExample
{
   public static void main(String args[])
   {
        int i=10;
        for(;true;)
        {
              System.out.println("Shiva " + i);
             
              i--;

        }


   }



}

WAP to print Fibonacci series ?

class Fibonacci
{
   public static void main(String args[])
   {
       int a=-1,b=1,c;
       for(int i=1;i<=10;i++)
       {
          c=a+b;
          System.out.println(c);
          a=b;
          b=c;

       }


   }


}

......................................................................

for with multiple initializations:-

for(int i=1,j=5;i<=10;i++,j--)
{
      System.out.println(i+" ,"+j);

}


for(int i=1,j=5;i<=10 && j>=2;i++,j--)
{
      System.out.println(i+" ,"+j);

}


ASSIGNMENT:-

1)  WAP to check prime number without using the third variable?

2)  WAP to display the table of each Fibonacci series digit?

Solution:-

class Fab
{

   public static void main(String args[])
   {
      int a=-1;
      int b=1;
      int c;
      for(int i=1;i<=5;i++)
      {
          c=a+b;
       
       
          if(c>0)
          {
             System.out.println("Table of "+c);
             for(int j=1;j<=10;j++)
             {

                 System.out.println(c*j);
             }
     

          }
          a=b;
          b=c; 


      }


   }



}

3)  WAP to find first, second, and third max number in pin code?


logic:-

class MaxPin
{
   public static void main(String args[])
   {
      int pin=7812345;
      int max=0,smax=0,tmax=0 ;
      while(pin!=0)
      {
         int r = pin%10;
         if(max<r)
        {
         smax=max;
         max=r;

        }
       if(smax<r && max!=r)
        {
            tmax=smax;
            smax=r;
        }
         if(tmax<r && smax!=r && max!=r)
        {
            tmax=r;
        }
         System.out.println(r);
         pin=pin/10;
      }
      System.out.println("max "+max +" second max "+smax + " third max "+tmax);

   }
}




4) WAP  to check that a particular digit exists in a mobile number or not?





   1.3  Foreach:--

This loop is used to display elements of array and collection, in the normal program we will not prefer for each loop.

for(var : arrayvariable)
{


}




2 Exit Control:-

   First Execute Statement Then Check Condition

  2.1 do---while:-

       init;
       do
       {
               Statement;
               Increment;
       }while(condition);



WAP to display 1 to 5 and 5 to 1 using a single do-while loop?

class Dowhileloop
{
  public static void main(String args[])
  {
       int i;
       i=1;
       do
       {
            if(i<=5)
            System.out.println(i);
            else
            System.out.println(11-i);
            i++;   

       }while(i<=10); 

   } 


}

WAP to print factorial with expression

5*4*3*2*1=120

Solution:-
class Fact
{
   public static void main(String args[])
   {
      int f=1;
      int num=5;
      int i=num;
      String s=""; 
      do
      {
        f=f*i;
        s= s+i+"*"; 
        i--;

      }while(i>1);

      System.out.println(s+"1="+f);   


   }




}


WAP to find the max digit in pin code?

Only Logic:-
int num=123454;
int max=0;
do
{
  int r = num%10;
  if(max<r)
    max=r;
  num=num/10;

}while(num!=0);

....................................



WAP to check prime number with complete expression

5    5%2,5%3,5%4

WAP to print 1 to 5,5 to 1,5 to 1 then 1 to 5 using a single do-while loop?




.........................................................................................................................


Special Statement in Loop?

1.1)  break:-

  It is used to explicit terminate the loop according to condition.
  the break statement will be used under the switch and loop statement.

 Example:-

  for(int i=1;i<=10;i++)
     {
          if(i==3 && i==5)
          break;
         System.out.println(i)

    }


Another program of Break?

class BreakDemo
{

    public static void main(String args[])
    {
        int i;
        for(i=1;i<=10;i++)
        {
           if(i==3 && i==5 || i==7 && i==2)
           break; 
           System.out.println(i);
        } 

        
    } 


}

1.2)  continue:-

It is used to skip the data and continue loop .it is mostly used to skip the particular data from the loop?

Some Example:-

for(int i=1;i<=10;i++)
{
    if(i==3 || i==5)
      continue;
    System.out.println(i);

}

class BreakDemo
{

    public static void main(String args[])
    {
        int i;
        for(i=1;i<=10;i++)
        {
           
           if(i==3 || i==5 || i==7 && i==2)
           continue;
           if(i==3)
           break;   
           System.out.println(i);
        } 

        
    } 


}





Interview Question of For Loop:-

What will be the result of this:-

class BreakDemo
{

    public static void main(String args[])
    {
        int i;
        for(i=1;i<=10;i++);
     
        {
           System.out.println(i);
        }

     
    }

}


Nested for loop:-

Using this we will write more than one for loop using nested sequence, It has a collection of outer for loop and inner for loop.

Outer for loop will execute once, but inner for loop will be executed whenever the condition will be true.

When inner for loop condition will false then outer for loop will increase then again inner for loop execute. When outer for loop condition will false then Loop will terminate.


...............................................................................
Syntax of nested for Loop:-



for(init;condition;iteration)
{
     for(init;condition;iteration)
    {


            Statement;

     }

     NewLineStatement



}


Example

for(int i=1;i<=3;i++)
{
       for(int j=1;j<=5;j++)
      {

           System.out.print(j);

     }

    System.out.println();
 

}


The output of this program:-

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5






Print this pattern

1 2 3 4 5
   2 3 4 5
      3 4 5
         4 5

Solution by Shiva Sir:-

class Pattern
{
   
    public static void main(String args[])
    {
            for(int i=1;i<=5;i++)
             {
                 for(int j=1;j<i;j++)
                 {
                    System.out.print(" ");

                 }
                 for(int k=i;k<=5;k++)
                 {
                     System.out.print(k);
                 }
              
                 System.out.println();
              }  



     }




}
.....................................
M A N I S H
     A N I S H
         N I S H
             I S H
                S H
                   H


Solution by Shiva Sir:-

class Pattern
{
   
    public static void main(String args[])
    {
            String s = "MANISH";
             int i,j,k;
            for(i=1;i<=6;i++)
             {
                 for(k=1;k<i;k++)
                 {
                  System.out.print(" ");
                 }
                 for(j=i-1;j<6;j++)
                 {
                     System.out.print(s.charAt(j));
                 }
              
                 System.out.println();
              }  



     }




}
............................................................

1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
......................................................
A a B b C
A a B b
A a B
A a
A

i     j
1    5
2    4
3    3
4    2
5   1

when j=1,3 and 5 then capital char
when j=2,4  then small

The solution of this program:-

class Pattern
{
   
    public static void main(String args[])
    {
            int i,j;
            for(i=1;i<=5;i++)
             {
                 int ch=65; 
                 
                 for(j=1;j<=6-i;j++)
                 {
                    if(j%2!=0)
                    System.out.print((char)ch);
                    else
                    {
                     System.out.print((char)(ch+32));
                     ch=ch+1;
                    }
                 }
              
                 System.out.println();
              }  



     }




}
....................................................................
A B C D E
A B C D
A B C
A B
A
........................................................

2
3 5
7 11 13
17 19 23 29

Solution by Shiva Sir:-

public class Pattern {

public static void main(String[] args) {
int j;
int c=0;
for(int i=2;i<=29;i++)
{

for(j=2; j<i; j++)
{
    if(i%j==0)
     break;
   
}
if(i==j)
{
System.out.print(i +" ");
c++;
if(c==1 || c==3 || c==6)
System.out.println();
}

}
}
}


Solve this problem:-

A B C D E
    B C D E
        C D E
            D E
                E

Solution by Shiva Sir:-

class NestedBlock
{
    public static void main(String args[])
    {
         for(int i=1;i<=5;i++)
         {
                char ch=65; 
               for(int k=1;k<i;k++)
               {
                System.out.print("  ");
                ch++; 
               }
            
             for(int j=i;j<=5;j++)
             {
                System.out.print((char)ch+" ");
                ch++;

              }
              
              System.out.println();

          }


    } 




}
    


  Challenging program of Star:-

          *
     *   *   *
*  *    *    *   *
    *     *    *
            *      


The solution to this program:-

class Patterndemo
{
    public static void main(String args[])
    {
       int o=2;
        for(int i=1;i<=5;i++)
        {
          if(i<=3)
          {
          for(int k=2;k>=i;k--)
          {
             System.out.print(" ");
          }
          for(int j=1;j<=2*i-1;j++)
          {
              System.out.print("*");
          }
          }
         else
         {
             for(int k=1;k<=i-3;k++)
             {
             System.out.print(" ");
             }
          for(int j=1;j<=2*o-1;j++)
          {
              System.out.print("*");
          }
          o--;
         }    
          System.out.println();
        }

    }


}



*
* *
* * *
* * * *
* * * * *




        

* * * * * 
  * * * * 
   * * * 
    * * 
     * 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,.........................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 


* * * * * 
 * * * * 
  * * * 
   * * 
    * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 


    *
   * *
  *   *
 *     *
*********
Solution:-
class Patterndemo2
{
    public static void main(String args[])
    {
        
        for(int i=1;i<=5;i++)
        {
         
          for(int k=1;k<=5-i;k++)
           {
                 System.out.print(" ");
           }
          for(int j=1;j<=i;j++)
          {
              if(i==3 && j==2 || i==4 && j==2 || i==4 && j==3)
              {
              System.out.print(" ");
              System.out.print(" ");
              }
              else if(i==5)
              {
                   
                  System.out.print("**");
                  if(j==4)
                  {
                   System.out.print("*");
                    break;
                  }
              }
              else
              System.out.print("* ");
          }
          
         
          System.out.println();
        }

    }


}


Post a Comment

6Comments

POST Answer of Questions and ASK to Doubt

  1. 1st one

    package lang;

    public class New {

    public static void main(String[] args) {

    for(int i=1;i<=5;i++)
    {
    for(int j=1; j<=5; j++)
    { //System.out.print(" ");
    if(j>=i)
    System.out.print(+j);
    else
    System.out.print(" ");
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  2. import java.util.Scanner;
    class Rev
    {
    public static void main(String args[])
    {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter Mobile Number = ");
    double num=s.nextDouble();
    double a=num/1000000000;
    num=num%1000000000;
    double b=num/100000000;
    num=num%100000000;
    double c=num/10000000;
    num=num%10000000;
    double d=num/1000000;
    num=num%1000000;
    double e=num/100000;
    num=num%100000;
    double f=num/10000;
    num=num%10000;
    double g=num/1000;
    num=num%1000;
    double h=num/100;
    num=num%100;
    double i=num/10;
    double j=num%10;

    double rev=j*1000000000+i*100000000+h*10000000+g*1000000+f*100000+e*10000+d*1000+c*100+b*10+a*1;
    System.out.println("Reverse Number = "+rev);
    }
    }

    ReplyDelete
  3. Actual reverse program of mobile number is this use long
    class ReverseDemo
    {
    public static void main(String args[])
    {
    long num= 9578912386L;
    long r=0;
    while(num!=0)
    {
    long a = num%10; //9
    System.out.println(a);
    num=num/10;
    r=r*10+a;

    }

    System.out.println(r);


    }


    }

    ReplyDelete
  4. class Pattern
    {
    public static void main(String args[])
    {
    int i,j;
    for(i=1;i<=10;i++)
    {
    if(i<=5)
    {
    for(j=1;j<=i;j++)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    else
    {
    for(i=6;i<=10;i++)
    {
    for(j=i;j<=9;j++)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    }
    }
    }
    }

    ReplyDelete
  5. class Pattern1
    {
    public static void main(String args[])
    {
    int i,j,k;
    for(i=1;i<=5;i++)
    {
    for(k=1;k<=5-i;k++)
    {
    System.out.print(" ");
    }
    for(j=1;j<=i;j++)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  6. class PatternDemo
    {
    public static void main(String args[])
    {
    int i,k,j;
    for(i=1;i<=10;i++)
    {
    if(i<=5)
    {
    for(k=2;k<=i;k++)
    {
    System.out.print(" ");
    }
    for(j=5;j>=i;j--)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    else
    {
    for(i=6;i<=10;i++)
    {
    for(k=1;k<=10-i;k++)
    {
    System.out.print(" ");
    }
    for(j=1;j<=i-5;j++)
    {
    System.out.print("* ");
    }
    System.out.println();
    }
    }
    }
    }
    }


    ReplyDelete
Post a Comment