10-08-2009, 01:17 AM
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
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:
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
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
-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 )
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
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
1.1 While (True or False condition)
Another kind of while loops is wondering if a boolean bar is true or false.
And you can test the variable to false if you put while (!variable) would be if variable == false...
2 - For
3 - Do While
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
Bye bye
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
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
-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 )
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
Bye bye