Skip to main content

Control Structure or Loop in Java

It is used to enumerate range based data, repeated data and large data manipulation using single repeatable code.

we can easily control large statement hence it is called Control Statement.

Control Statement Will Work using three different substatements.
                               
                                    Forward                    Backward

Initialization             i=min                         i= max 


Condition                  i<=max                      i>=min


Iteration                    i++                               i--



Loop:-  Process of Control Structure is called loop because it will execute until condition false


Type of Control Structure in java:-

1.1 Entry Control:-  First Check Condition then execute statement

1.1.1   For Loop  

1.1.2   While Loop

1.1.3    For-each Loop
1.2 Exit Control:-  First execute statement then check the condition

1.2.1  Do--while:-

Syntax:

init;
do
{
   statement;
   increment|decrement;

}while(condition);


WAP to Print Sum of two-digit positive number in a range?

class Dowhile
{
   public static void main(String args[])
   {
       int i;
       i=99;
       int sum=0;
       do
       {
         sum+=i;   //sum=0+10 //sum= 10+11
         i--;
       }while(i>=10); 

       System.out.println(sum);

   }



}





Q1 WAP to print 1 to 5 and 5 to1 using single do--while loop?
1
2
3
4
5
5
4
3
2
1


Q2 WAP to calculate factorial with complete expression?

5! = 5*4*3*2*1=120


Q3 WAP to reverse mobile number and find max digit in the mobile number and count repeated digit and show number is VIP or not according to number combination?


While Structure:-

init;
while(condition)
{
     statement;
     iteration;

}

First Check Condition then execute the statement.


while loop mostly used for the in-finite loop-based program.


WAP to print 1 to 10 using the finite loop and infinite loop both?

i=1;
while(i<10)
{
    System.out.println(i);
    i++;

}
...............................................................
boolean a=true
int i=1;
while(a)
{

System.out.println(i++);
if(i>10)
flag=false;

}


Limitation of While Loop:-

1 Complex structure hence for nested sequence it can not be used easily.



For Loop:-


It provides a simple syntax structure and defines all sub statement using a single statement.

       1     2    5            4  7
for(init;condition;iteration)
{
     3  6
     statement
}


we can create finite program using for loop.




ASSIGNMENT:-

1) WAP to display series of a prime number?

2) WAP to print sum of one digit and 2 digits negative number using a single while loop and for loop?

0 to 9
-99 to -10

class Sumwhile
{
   public static void main(String args[])
   {
      int i,j;
      i=-99;
      j=1;
      int sum1=0; 
      int sum2=0;
      int sum=0;
      while(i<=-10)
      {
         if(j<10)
         sum1=sum1+j;
         j++;
         sum2=sum2+i;   
         i++;

      }
     sum=sum1+sum2;
     System.out.println(sum);

    }

}





Q)WAP to print only Fibonacci series in Java ?


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

    } 


}




............................................................................................
Q)
Q)WAP to print prime number only in  Fibonacci series in Java ?

Logic1)

class Faboprime
{
    public static void main(String args[])
    {
          int a=-1,b=1;
          int c;
          int i=1;
         
          while(i<=10)
          {
                 c=a+b;   
                 int j=2;
                 while(j<c)
                 {
                     if(c%j==0)
                       break;
                     j++;

                 }
                 if(c==j)
                 System.out.println(c);
                 a=b;   
                 b=c;   
                 i++;
           }

    } 


}

..............................................................................................
Logic2)
class Faboprime
{
    public static void main(String args[])
    {
          int a=-1,b=1;
          int c;
          int i=1;
         
          while(i<=10)
          {
                 c=a+b;   
                 int j=1;
                 int count=0;
                 while(j<=c)
                 {
                     if(c%j==0)
                       count++;
                     j++;

                 }
                 if(count==2)
                 System.out.println(c);
                 a=b;   
                 b=c;   
                 i++;
           }

    } 


}

Logic3)

class Faboprime
{
    public static void main(String args[])
    {
          int a=-1,b=1;
          int c;
          int i=1;
         
          while(i<=10)
          {
                 c=a+b;   
                 int j=2;
                 boolean f=true;
                 while(j<c)
                 {
                     if(c%j==0)
                     {
                       f=false;
                       break;
                     }  
                     j++;

                 }
                 if(f && c>=2)
                 System.out.println(c);
                 a=b;   
                 b=c;   
                 i++;
           }

    } 



}





Design4)
class Faboprime
{
    public static void main(String args[])
    {
          int a=-1,b=1;
          int c;
         
         
        for( int i=1;i<=10;i++)
          {
                 c=a+b;   
                 boolean f=true;
                for(int j=2;j<c;  j++)
                 {
                     if(c%j==0)
                     {
                       f=false;
                       break;
                     }  
                 }
                 if(f && c>=2)
                 System.out.println(c);
                 a=b;   
                 b=c;   
               
           }

    } 


}

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

1) WAP to print 1 to 5 , 5 to 1 and 5 to 1 and 1 to 5 using single while loop and for loop?



2) WAP to print pattern


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



3) WAP  to print this pattern?

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

4 ) WAP to print this pattern?

A a B b C
A a B b
A a B
A a
A

5 ) WAP to print this pattern?

1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
..............................................................................................................













Comments

  1. Solution 2
    class Fact
    {
    public static void main(String args[])
    {
    int num=5,fact=1;
    System.out.print(num+"! = ");
    do
    {
    System.out.print(num);
    fact=num*fact;
    num--;
    if(num>0)
    System.out.print("*");
    }while(num>0);
    System.out.print(" ="+fact);
    }
    }

    ReplyDelete
  2. Solution 1
    class Print
    {
    public static void main(String args[])
    {
    int i=1,j=5;
    do
    {
    if(i<=5)
    {
    System.out.println(i);

    }
    else
    {
    System.out.println(j);
    j--;
    }
    i++;


    }while(i<=10);
    }
    }

    ReplyDelete
  3. Solution 3
    import java.util.Scanner;
    class Check
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    int j=0;
    System.out.println("Enter mobile no.");
    int num=sc.nextInt();
    int max=num%10;
    System.out.print("Reverse of no. = ");
    do
    {
    int i=num%10;
    System.out.print(i);
    num=num/10;
    j=num%10;
    if(j>max)
    max=j;

    }while(num>0);

    System.out.println("\nMaximum digit ="+max);
    }


    }

    ReplyDelete
  4. public class Forprime
    {
    public static void main(String args[])
    {
    int i=1;
    int j=2;
    int conter=0;
    for (i=1;i<=20; i++)
    {
    conter=0;
    for(j=2; j<=i/2; j++)
    {
    if (i%j==0)
    {
    conter++;
    break;
    }
    }

    if(conter==0)
    {
    System.out.println(i);
    }
    }
    }
    }

    ReplyDelete
  5. Solution to print pattern
    class Pattern1
    {
    public static void main(String args[])
    {
    int i=1,j=0;
    while(i<=6)
    {
    j=1;
    while(j<i)
    {
    System.out.print(j);
    j++;
    }
    i++;
    System.out.print("\n");
    }
    }
    }

    ReplyDelete
  6. Solution to print pattern 1
    class P1
    {
    public static void main(String args[])
    {
    int i=1,j=5,k=5,m=1;
    while(i<20)
    {
    if(i<=5)
    {
    System.out.println(i);
    i++;
    }
    else if(j>=1)
    {
    System.out.println(j);
    j--;
    }
    else if(k>=1)
    {
    System.out.println(k);
    k--;
    }
    else if(m<=5)
    {
    System.out.println(m);
    m++;
    }

    }
    }
    }

    ReplyDelete
  7. import java.util.Scanner;
    class Mobileno
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int mb,i,rev=0,temp,temp1,temp2,rem,rep,rep1,max=1,count=0;
    System.out.println("Enter your mobile number");
    mb=sc.nextInt();
    System.out.println("enter the number to check repeatation");
    rep=sc.nextInt();
    temp=mb;
    temp2=mb;
    do
    {
    temp1=temp%10;
    temp=temp/10;
    if(temp1>max)
    max=temp1;
    }while(temp>0);
    System.out.println("The maximum digit in mobile number is :"+max);
    do
    {
    rem=mb%10;
    rev=rev*10+rem;
    mb=mb/10;


    }while(mb!=0);
    System.out.println("the reversed number :"+rev);
    for(i=1;i<=10;i++)
    {
    if(temp2%10==rep)
    {
    ++count;
    }
    temp2=temp2/10;
    }
    System.out.println(rep+" is repeated "+count+" times");
    if(count>=4)
    System.out.println("the Entered no. is VIP");
    }
    }

    ReplyDelete
  8. pattern 10010

    class Pattern2
    {
    public static void main(String args[])
    {
    int i=1,j=0,count=0;
    while(i<=5)
    {
    j=1;count=0;
    while(j<=i)
    {
    if(i%j==0)
    count++;

    if(count==2)
    System.out.print("1");
    else
    System.out.print("0");
    j++;
    }
    i++;
    System.out.print("\n");
    }
    }
    }

    ReplyDelete
  9. pattern ABCDE

    class Pattern
    {
    public static void main(String args[])
    {
    char a='A';
    int i=5;
    for(;i>=1;)
    {
    for(int j=1;j<=i;j++)
    {
    System.out.print(a);
    a++;
    }
    System.out.print("\n");
    a='A';
    i--;
    }
    }
    }

    ReplyDelete
  10. pattern AaBbC

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

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...