Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java: While Loops

So far in this section I've seen 3 people struggling with their while loops. So what I'm going to do is break it down for you all to make it as easy as possible to understand.

Function of a while loop; A While loop allows you to execute a block of code a number of times without having to type it over and over.

Starting your While Loop

Before we can make the loop, we need something called a counter. What the counter will do is take your number you input for the counter "5" and print the line of text "5" times.

Code
class Project1 {
    public static void main(String args[]) {
            int counter = 0;

Now that we have our counter variable we can start giving it a value.

Code
while(counter < 5){
                System.out.println("While loop tutorial");
                counter++;

Now let me explain what each line of code does.

int counter =0; is saying the counter value = 0.

Next, you wanted to give your counter a number to go off of.

while(counter < 5){ is stating that as long as your variable counter is <5 it will print out "While loop tutorial" 5 times.

System.out.prinln("While loop tutorial"); is just printing out that line of code on to your screen.

And finally, counter++; This is stating that the counter will go up by 1 until it reaches 5.

Here is the full, working code.

Code
class Project1 {
    public static void main(String args[]) {
            int counter = 0;

            while(counter < 5){
                System.out.println("While loop tutorial");
                counter++;
            }
    }
}

I hope this helps people that are struggling with while loops. If you guys want more tutorials post on the thread below. Also, if you have any questions regarding your code feel free to pm me.
This is quite useful. I was able to understand that quite well, thanks! :)
(Jul 26, 2015 05:03 AM)Susta1nz Wrote:  This is quite useful. I was able to understand that quite well, thanks! :)

Thanks. I'm happy I could help.
Wtf

It should be

while(condition is true) execute code in tags.

Also would be nice to explain do while

And people should learn the language before trying anything.
(Jul 26, 2015 03:37 PM)MrOrang Wrote:  Wtf

It should be

while(condition is true) execute code in tags.

Also would be nice to explain do while

And people should learn the language before trying anything.

It could also be while(condition is <= or >=) so It doesnt really matter.

I'll get on explaining that

Agreed 100%
Uhm >= or <= whatever return true or false so kinda the same.
(Jul 26, 2015 03:55 PM)MrOrang Wrote:  Uhm >= or <= whatever return true or false so kinda the same.

Yeah. Either way the loop works fine.
No reason to simply write a tutorial on just a while loop. Loops in general? Maybe, if it is really written well enough. You could have gone into a deeper explanation of loops and added layers of abstraction and made it at least decent. For example, instead of explaining a simple scenario where there is a counter four, you could have explained that the while loop will execute until the given condition becomes false.
(Jul 31, 2015 12:43 AM)Apathy Wrote:  No reason to simply write a tutorial on just a while loop. Loops in general? Maybe, if it is really written well enough. You could have gone into a deeper explanation of loops and added layers of abstraction and made it at least decent. For example, instead of explaining a simple scenario where there is a counter four, you could have explained that the while loop will execute until the given condition becomes false.

^ This. A While loop doesn't have to run with counters. A While loop runs until a specified expression evaluates to false. (Boolean expression)
[Image: D12KBT6.png]
A for-loop would be much cleaner for this application.
As others have said, you should edit the thread to add other types of loops and the differences and when to use them (e.g. for-loop, while, do...while).
(Jul 31, 2015 02:36 AM)Snoweyes Wrote:  ^ This. A While loop doesn't have to run with counters. A While loop runs until a specified expression evaluates to false. (Boolean expression)

Haha, exactly what I was thinking as well. I was wondering why we had a counter anyways... ✌
(Aug 5, 2015 09:17 AM)Warren Buffett Wrote:  Haha, exactly what I was thinking as well. I was wondering why we had a counter anyways... ✌

I dunno actually. I thought I would just add it to add it. I'll make a more detailed tutorial in the future.
(Aug 5, 2015 10:56 AM)Balding Wrote:  I dunno actually. I thought I would just add it to add it. I'll make a more detailed tutorial in the future.

If you are making a more detailed one, do more then just While loops.
(Aug 5, 2015 01:34 PM)La Manifesto Wrote:  If you are making a more detailed one, do more then just While loops.

Yeah, I'm going to explain each type of loop in depth.
do you mind explaining the feature of a "do" function in a while loop?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by n00b.caApr 4, 2020 12:39 PM 3 200 Apr 4, 2020 12:39 PM
Last Post: n00b.ca
[Java] by YukinoNov 12, 2019 05:50 PM 6 425 Nov 12, 2019 05:50 PM
Last Post: Accord
[C] by GoddessTaliyahMay 3, 2018 02:22 PM 4 393 May 3, 2018 02:22 PM
Last Post: 0xDEAD10CC