Loop Control

There are 2 use cases for loops in any language. 1. If a piece of code has to be executed multiple times, we use loops. 2. To iterate over range of values, we use loops. A loop is basically that section of code which needs to be executed several times. There are 3 types of loops in Java: 1. while Loop 2. do...while Loop 3. for Loop All these three types execute the section of code inside their scope multiple times based upon the conditions. Let us see one by one:

While Loop:

Syntax:

while(Boolean_expression)
{
  //Statements
}
As we can see, the code will be executed if the boolean_expression is true.

Examples on while loop:

Example 1:

public class WhileDemo1
{
  public static void main(String[] args)
  {
     int i=10;
     while(i>=0)
     {
     System.out.println(i);
     i--;
     }
   }
 }

Example 2:

Below program will print "Virus alert!" for infinite number of times, using while loop.
public class WhileDemo2 {
  public static void main(String[] args)
  {
	 while(1!=0)
         {
	     System.out.println("Virus alert!");
	  }
    }
}

Example 3:

Below program uses while loop to reverse a given number entered through keyboard.
public class Reverse {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter a number");
		int num = sc.nextInt();
		int newnum=0;
		while(num!=0){
			newnum = (newnum *10)+ num%10;
			num = num/10;
		}
		System.out.println(newnum);
	 }
}

Example 4:

Below program uses while loop to reverse a given number entered through keyboard, and finds if it is a polyndrome or not.
public class Polyndrome {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter a number");
		int num = sc.nextInt();
		int temp = num;
		int rev = 0;
		
		while(num!=0){
			int last = num%10;//gets the last digit from num
			rev = (rev*10)+last;
			num = num/10; //removes last digit from num
		}
		if(temp == rev){
                          	System.out.println("its a polyndrome");
		}
		
		System.out.println("after reversing.."+rev);
	}
}

Do...While:

Syntax:

do
{
//Statements
}while(Boolean_expression);
Here also the code will be executed unless boolean_expression is false but the difference is since the expression value is checked at the end of the code, the code executes once before the boolean goes false. That is if the value is false at the first time itself, then also the code segment will be executed once.

Examples on Do while :

Example 1:

Below program will print hello for only once using do-while loop.
public class DoWhileDemo1 {
	public static void main(String[] args) {
		do{
			System.out.println("hello");
		}while(1==0);
        }
}

Example 2:

Below program will print 1 to 10 by using do-while loop.
public class DoWhileDemo2 {
	public static void main(String[] args) {
		int i=0;
		do{
			i++;
			System.out.println(i);
		}while(i<10);
        }
}

For loop:

Syntax:

for(initialization; Boolean_expression; update) { //Statements } This loop is different from the above two loops as it takes a variable to be initialized and updated and then the code segment executes until the updated variable value makes the boolean_expression false.

Examples on for loop:

Example 1:

Below program will print fibanacci series up to 10 numbers by using for loop. Fibonacci series : 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
public class Fibonacci {
	public static void main(String[] args) {
		int i=0;
		int j=1;
		int sum = 0;
		System.out.println(i);
		System.out.println(j);
		for(int z=0; z<10; z++){
			sum = i+j;
			i=j;
			j=sum;
			System.out.println(sum);
		 }
         }
}

Example 2:

Below program will find if a given number entered from keyboard is prime number or not.
public class PrimeNumberProgram {
           public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("enter a number to find prime 
                             or not");
		int num = sc.nextInt();
		boolean prime = true;
		
		for(int i=2; i<num; i++){
			if(num%i==0){
				prime = false;
				break;
  			}
		}
		
		if(prime == false){
			System.out.println("not a prime");
		}else{
			System.out.println("prime"
		   }
           }
}

Example 3:

Start pattern program - Write the logic to draw below pattern using for loops. * ** *** ****
public class Triangle1 {
	public static void main(String[] args) {
		int num=1;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<num;j++)
			{
				System.out.print("*");
			}
                  	                num++;
			System.out.println();
		  }
        }
}

Example 4:

Number pattern program - Write the logic to draw below pattern using for loop. 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
public class Rectangle2 {
	public static void main(String[] args) {
		int num=1;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<5;j++)
			{
				System.out.print(num);
				num++;
			}
			num=1;
			System.out.println();
		    }
        }
}

Example 5:

Number pattern program - Write the logic to draw below pattern using for loop. 1 2 3 4 5 6 7 8 9 10
public class Triangle2 {
	public static void main(String[] args) {
		int num1=1;
                                	int num2=1;
		                for(int i=0;i<4;i++)
		              {
			for(int j=0;j<num1;j++)
			{
				System.out.print(num2);
				num2++;
			}
			num1++;
			System.out.println();
	}
}

Example 6:

pattern program - Write the logic to draw below pattern * *** ***** *** *
public class Triangle3 {
	public static void main(String args[]) {
		int n=1;
		Boolean b=false;
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<n;j++)
			{
				System.out.print("*");
				if(n==5)
				{
					b=true;
				}
			}
			System.out.println();
			if(b==true)
			{
				n=n-2;
                        }
			else
			{
				n=n+2;
			}
	      }
      }
}

Example 7:

Number pattern program - Write the logic to draw below pattern 1 2 2 3 3 3 4 4 4 4
public class Triangle4 {
	public static void main(String[] args) {
		int num=1;
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<num;j++)
			{
				System.out.print(num);
			}
			num++;
			System.out.println();
		}
        }
}

Example 8:

Start pattern program - Write the logic to draw below pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
public class Triangle5 {
	public static void main(String[] args)
	{
		for (int i = 1; i <= 6; i++) {
			for (int j = 6; j > i; j--) {
				System.out.print("  ");
			}
			for (int j = i; j > 1; j--) {
				System.out.print("*" + " ");
			}
			for (int j = 1; j <= i; j++) {
				System.out.print("*" + " ");
			}
			System.out.println();
		}
        }
}