while loop in java

Working of while loop: with while loop first of all condition is check if condition is true then flow go to inside while loop and lines of code that write with in while loop  will execute line by line  that process happen again and again till condition is true.When condition return false then flow comes out of loop and jumps to the next statement after while loop.

Remember: We need to write increment or decrements statement with in while loop so that the loop variable value gets switch on each iteration, and some point of time condition returns false.So in that time execution of while loop is end otherwise loop would execute infinite time.

Syntax of while loop in java:

while(condition)

{

code to be executed

Increment/decrement

}

or

 

while(condition)

{

Increment/decrement

code to be executed

}

 

Workflow diagram for while loop:

 

 

Example of while loop:

class ExampleOfWhileLoop{

public static void main(String ar [])

{

int i=0;

System.out.println(“Before while loop”);

while(i<5)

{

i++;

System.out.println(“value of i is::”+i);

}

System.out.println(“After while loop”);

}

}

.

Output:  Before while loop

value of i is::1

value of i is::2

value of i is::3

value of i is::4

value of i is::5

After while loop

 

Example of while loop:

class ExampleOfWhileLoop1

{

public static void main(String ar [])

{

int i=0;

System.out.println(“Before while loop”);

while(i<5)

{

System.out.println(“value of i is::”+i);

i++;

}

System.out.println(“After while loop”);

}

}

Output:  Before while loop

value of i is::0

value of i is::1

value of i is::2

value of i is::3

value of i is::4

After while loop

 

Now we will solve above problem with help of while loop;

int i=1;

while(i<=100)

{

System.out.println(i);

i++;

}

Output:

1

2

3

.

10

..

99

100

So now we can see with help of loop that programme complete in some statements

So this the power of looping in programming languages.

 

infinite while loop:    if our condition never be false for loop then that loop will go

in   infinite state.

Example :  int i=1;

while(i<5)

{

System.out.println(i);

 

}

Here we can see for this loop condition never be false so this programme will go in

Infinite state.

Here we can see a another example of infinite while loop:

int i=1;

while (true)

{

System.out.println(i);

}

 

Above all example of while loop hope so you notice we can’t exit from loop till specify

Condition is false but in some cases we don’t need execute loop till condition is false.

Here an example: if we can say WAP for find a number from an array

While loop with One-D array :

import java.util.Scanner;

class ExampleOfWhileLoopWithArray

{

public static void main(String ar [])

{

Scanner sc=new Scanner(System.in);

int [] num={ 10,20,30,40,15 };

int i;   int i1=0;

System.out.println(“enter number for search”);

i=sc.nextInt();

int count=0;

while(i1< num.length)

{

if(num[i1]==i)

{

System.out.println(“number is find on index ::”+i1 + “and number is::”+i);       count++;      break;

}

i1++;

}

if(count==0)

{

System.out.println(“number is not find in array”);

}

}

}

so in this programme we can see whenever number will find then with help of break

keyword we will immediately out of loop. So we don’t need execute loop till condition is false.

Hope so you like this article Thanks….

 

 

 

56 thoughts on “while loop in java

Leave a Reply

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

  1. I simply want to mention I am newbie to weblog and honestly liked this website. Most likely I’m planning to bookmark your site . You surely have amazing posts. Regards for sharing with us your webpage.

  2. Hello, Neat post. There’s a problem along with your web site in internet explorer, could test this… IE nonetheless is the market leader and a large part of other people will miss your magnificent writing due to this problem.

  3. Hey there I am so thrilled I found your site, I really found you by mistake, while I was looking on Aol for something else, Anyways I am here now and would just like to say thanks a lot for a incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.

  4. Good day! I simply want to give an enormous thumbs up for the nice information you will have right here on this post. I will be coming again to your weblog for extra soon.

  5. Excellent blog right here! Also your website quite a bit up very fast! What host are you using? Can I get your affiliate hyperlink in your host? I wish my site loaded up as quickly as yours lol

  6. Thank you for some other fantastic article. The place else could anybody get that type of info in such a perfect approach of writing? I’ve a presentation next week, and I’m on the search for such information.

  7. I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here frequently. I am quite sure I’ll learn many new stuff right here! Best of luck for the next!

  8. I am not sure where you are getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for great info I was looking for this information for my mission.

  9. Hey there, Well put together post aplikasi berita saham . There is a challenge together with your web-site throughout world-wide-web internet explorer, may test this particular? For instance even so is definitely the current market boss as well as a huge portion of other folks may miss out on your own good publishing just for this trouble.

  10. This design is incredible! You most certainly know
    how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Excellent job. I really loved what you had to say,
    and more than that, how you presented it. Too cool!

  11. I’m usually to running a blog and i really respect your content. The article has really peaks my interest. I am going to bookmark your website and preserve checking for new information.

  12. Thank you almost every other magnificent publish. Where by otherwise may well any individual obtain that kind of information and facts in this a healthy way associated with producing? I’ve a speech future few days, and i am on the look for such information aplikasi rekomendasi saham terkini.

  13. obviously much like your web-site nevertheless, you must check the punctuation for numerous of your respective blogposts koleksi vlogger terbaru. A number of them usually are rife along with punctuation difficulties and I believe that it is extremely troublesome in all honesty on the other hand I’m going to unquestionably occur again again.

  14. Your blog on while loop in java : with while loop first of all condition is check if condition is true then flow go to inside while loop and lines of code that write with in while loop?? will execute line by line?? that process happen again and again till condition is true. | Online Tutorial is very good. We hope u can continue delivering many more blog . Be prosperous onlinetutorial.co.in

  15. Hello would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a fair price? Cheers, I appreciate it!

  16. Your blog on while loop in java : with while loop first of all condition is check if condition is true then flow go to inside while loop and lines of code that write with in while loop?? will execute line by line?? that process happen again and again till condition is true. | Online Tutorial is very good. We hope you can continue writing many lot article soon. Viva onlinetutorial.co.in

  17. La erección masculina normal puede ocurrir cuando la estimulación psicológica o física desencadena impulsos del cerebro que se desplazan a los nervios del pene.
    comprar viagra genérico
    Además del tratamiento de la disfunción eréctil, para la cual se usan dosis en el rango de 50mg a 100mg (Viagra®), el citrato de sildenafilo se prescribe también para el tratamiento de la hipertensión arterial de origen pulmonar (Revatio® comprimidos de 20mg).

  18. La erección masculina normal puede ocurrir cuando la estimulación psicológica o física desencadena impulsos del cerebro que se desplazan a los nervios del pene.
    viagra genérico caverta
    Además del tratamiento de la disfunción eréctil, para la cual se usan dosis en el rango de 50mg a 100mg (Viagra®), el citrato de sildenafilo se prescribe también para el tratamiento de la hipertensión arterial de origen pulmonar (Revatio® comprimidos de 20mg).

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.