java loops

Java Loops-: loops allow us to execute a block of Java code over and over again as many times as we see fit.. Looping Provide a control statement any programming language. Like any other programming language java support Looping system. In other words we can say that the looping a sequence of statement are executed until some conditional for termination of the loop are satisfied. A programme loop there for consists of two segments. The control statement tests certain condition and then direct the repeated execution of the statements contained in the body of loop.

For example . If we want to calculate some number 1 to 10000  it is not possible in If else statement .

sum=sum + n*n;

10,000 times it is called the looping.

In the java language support 3 types looping.

  1. For loops
  2. while loops
  3. do while loops

For looping In java -: For loop in java support as like a c and c++  for loop take three piece of input these are ..

  1. Declare the variable -:First  declare the variable that we are going to use for  increment or decrements to count the number of times we have looped. In this condition we will use an integer i and give it a initial value of 1. We follow this initial statement with a semicolon. This is not a function call. It is a special syntax for the for loop.
  2. Initialization- The second part  of information the special syntax requires is the conditional statement that we need to evaluate each time we are going to restart the loop.
    If this conditional statement is ever not true then our for loop ends and we continue resuming our code after the for loop block. In this condition  our conditional statement will be the same as it is for the while loop. We want our last iteration of the for loop to be when i  is equal to 100, that is, when we print out 100. Once i is no longer less than or equal to 100, it’s time to exit our for loop.
  3. Just as the first piece of information we specially gave the for loop saved us from having to handle a variable outside the scope of our loop the last piece of information we will give the for loop takes the place of us having to manually increment or decrements our counter inside the scope of the loop. This is the special modification code and whatever we provide the for loop here will run at the end of every loop. In this case, we would  just like to increment the value of i at the end of each looping through.

Syntax of For loops.

for(int i=1; i<=100; i++)
   {
           System.out.println(i);
    }

 

Example of for loops in java 

public class forloopexample {  

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

Output of this programme

1
2
3
4
5
6
7
8
9
10

 

While loop in java– loops allow us to execute a block of Java code over and over again as many times . For example of while loops

simple formula of while condition as like

while (Test condition)

{

Body of loop;

}

The while loop is the entry loop controlled loop statement. the test condition is evaluated if the condition is true then the body of loop executed and it runs again and again check the condition if is again true then body will be executed again it repeat and repeat. If the test condition is false it will be exit  for example here

public class examplewhile { 
public static void main(String[] args) { 
int i=5; 

while (i>0) 
 { 
      System.out.println("Hello netnic"); 
 } 
} 
} 

Output of this programme

Hello netnic
Hello netnic
Hello netnic
Hello netnic
Hello netnic
Hello netnic
For stop the press we need Ctrl+c button of keyboard 

 

Another example of while loops

public class examplewhile { 
public static void main(String[] args) { 
int a=1; 

while (a<=5) 
        { 
            System.out.println("a"); 
            a++;
        } 
  } 
} 

Output of this programme

1
2
3
4
5

 

Do while loops in java-: The do…while loops operate just like while loops, but the first time when we  run, the compiler check to see whether a conditional is true. It simply run through and check their conditional statement at the end. We do need to put a semicolon at the end of a conditional statement on the back of the do…while loop.

In other words we can say that  some time the test condition is not true and necessary to test the body of the loop this condition we use the do while loop. The syntax for do while loop is

Do
 {
    body of loop
 }
While test condition

For a example of do whole loops

public class netnic {  
public static void main(String[] args) {  
    int i=1;  
    do{  
        System.out.println(i);  
         i++;  
       }
    while(i<=5);  
   }  
}


Output of this programme
1
2
3
4
5

Leave a Reply

Your email address will not be published. Required fields are marked *