top of page
Search
  • Writer's pictureShakil

Java Loops


A looping statement is used to loop through a certain statement at a certain time.


Suppose you need to print the numbers between 1–100 . So how would you do that? If you use normal basis statement and print it then it will take a large amount of time and makes the program lengthy.But with the help of loops you can print it within 2 or 3 lines.

There are basically three looping statement.They are for , while and do while.


1.For loop : For loop has three parts it takes the initial or start point then the ending point and finally the increment.Lets see an example that how could we print number 1–100 by for loop.

Syntax of for loop:

for(declaration : expression) {

// Statements

}

Printing out the numbers between 1 to 100:

for(int i=1;i<=100;i++){

System.out.println(i);

}

This three lines of code prints the numbers between 1–100.Which is very easy.


2.While loop : While loop more similar to do while loop.While loop contains just one condition into single parenthesis.And it has iteration into the body.The loop will be continued untill the condition gets false.

Syntax of while loop :

while(Boolean_expression) {

// Statements

increment;

} Printing numbers between 1 to 100 by while loop:

int i=1;

while(1<=100){

System.out.println(i);

i++;

}

Here first we take an initial point to start which is i.Then we set a condition into while that prints the number between 1–100 and each time it enters into the loop the value of i increments.


3. Do while: Do while loop mostly like while loop.But the the condition gets checked after the program executed at least once.

Syntax of do while loop:

do {

// Statements

increment;

}while(Boolean_expression); Lets print out the numbers between 1-100.

int i=1;

do{

System.out.println(i);

i++;

}while(i<=100);


Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.

So that is looping statement in java.Thanks for reading my blog.

9 views0 comments

Recent Posts

See All

Foundations of Artificial Intelligence-Philosophy

Philosophy (from the Greek phílosophía, meaning ‘the love of wisdom’) is the study of knowledge, or "thinking about thinking", although the breadth of what it covers is perhaps best illustrated by a

© 2023 by NOMAD ON THE ROAD. Proudly created with Wix.com

  • b-facebook
  • Twitter Round
  • Instagram Black Round
bottom of page