Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] Java Loops
#1
Hi Guys.

I have just registered in this web, and as you have been able to read, my english is not perfect... The purpose of this post is to show all of you that are learning java how to make loops.

Loops is one of the most important parts in all programming languages, and now, let me start Big Grin

Java Loops.

There is three kinds of loops in java:
- While
- For
- Do While

1 - While.

While is the first loop I'm gonna explain, it's one of the most used hehe.

Let me to put the syntax:

Code:
while (CONDITION) {
    ACTIONS;
}

And now explain it:

- While is the reserved word to start the loop, is what says to the system that you are going to make a while loop.

- CONDITION is the comparation you want to do, you can compare equals to... there is some examples
(1<2) <- Will be always true ;)
Operators:
== <- Equals to
!= <- NOT Equals to
< <- Minor than
> <- Bigger than
<= <- Minor or equals to
>= <- Bigger or equals to

- ACTIONS: are the actions you want to repeat X times, while the condition is true.


2 - For

Code:
for (int i=0;i<10;i++) {
    ACTIONS;
}

This is the FOR syntax, it's only one way to simply the WHILE loop, because many of while loops will be done X times where X is constant, so, you can make the declaration of the var, the condition and the increase of the variable in only one line. Maybe is hard to understand when you see it first time but will be useful in the future Big Grin

-FOR: As while, is the reserved word to tell the system you're going to do a for loop, it's always the same.

- int i=0 <- This is the variable declaration
i<10 <- This is where you see the condition
i++ <- This is the part where you increase the variable

All that parts are separed by ; as always in java or C... etc...

- ACTIONS: are the actions you want to repeat X times, while the condition is true. (As I put in the while loop Big Grin)

Third and last part of this mini tutorial.

3 - Do While.

This loop is only useful in one "kind" of while loop, this loop will always be executed ONE or MORE times, at least ONE time because the condition will be compared at the end of the loops, so, first make actions and then compare the condition, if the condition is true, make another time the actions etc...

Syntax

Code:
do {
ACTIONS;
} while (CONDITION)

Maybe is hard to see first time but it's as easy as the other loops hehe

-DO is the reserved word (as the other loops) to tell the system what you are gonna do.

and the rest... is the same as the other loops, so I'm not gonna write it xD

Now 3 examples, one of each, to show you how the loops works:

1 - While

Code:
int a = 0;
while (a < 10) {
    System.out.println(a);
    a++;
}

1.1 While (True or False condition)
Another kind of while loops is wondering if a boolean bar is true or false.

Code:
boolean variable = true; int a = 0;
while (variable) {
   if (a == 10) variable = false;
   else { System.out.println(a); a++; }
}

And you can test the variable to false if you put while (!variable) would be if variable == false...

2 - For

Code:
for (int a=0;a<10;a++)
    System.out.println(a);

3 - Do While

Code:
int a=0;
do {
    System.out.println(a);
    a++;
} while (a<11)



OK! This is the end of the tutorial hehe, I have just wrote it and as I have said before... sorry for my english I know it's not perfect but I'm learning from my mistakes.

Good forum Big Grin

Bye bye
Reply
#2
Very nice quick read, java loops are fairly easy, but you explained the "for" loop very well
Good job
Reply
#3
Pretty good. But here are some things you can add/improve on;

- Explain the use of the new for loops. For example,
Code:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
          for (int item : numbers) {
            System.out.println("Count is: " + item);
          }
Hopefully you can figure that one out.

- Explain more on the do-while statement. Many people find it difficult to understand for their first time. Smile

- Also, something about the while statement. It can be infinite if its set to,
Code:
while (true) {
// execution
}

- Maybe teach them how to make infinite loops using the for statement?

Good job nonetheless. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Java Tutorial United States of America 0 691 01-23-2012, 03:39 PM
Last Post: United States of America
  why java haphazard 8 1,786 12-12-2011, 03:23 AM
Last Post: RDCA
  Need Java help from java expert. Blazin 2 2,101 09-07-2011, 02:43 PM
Last Post: AceInfinity
  Java help php 1 1,047 04-06-2010, 06:41 AM
Last Post: php
  Java Problem for real java programmer Testgamma1 10 5,175 03-14-2010, 09:08 AM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 2 Guest(s)