Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,046 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,516 people online right now. Registration is fast and FREE... Join Now!




Looping

 
Reply to this topicStart new topic

> Looping, Basic concept, nice for beginners

Rating  4
Locke37
Group Icon



post 30 Jul, 2008 - 06:48 PM
Post #1


Ok, Looping...so it's basically doing something while a certain condition is true. That sentence is basically one of the loops forms.


The for Loop

Ok, so the for loop. This is a useful looping structure for traversing through arrays and such. Let's take a look at the syntax shall we?

java
for (int x = 0; x < 100; x++)
{
// statements to execute 100 times
}


The for line, let's take a closer look at it...It starts with the for keyword of course, to signify that a loop follows. In the parentheses we have the loop control variable, x, which starts at zero (the first part, before the semicolon). The second part is what to stop at, in this case, 99, since x cannot be greater than or equal to 100. The third part is the value to increment by every time the end of the loop iteration is reached, in this case our shortcut is used for add 1 every time.


The while Loop

The while loop. A loop to continue going around...and around, and around until a certain condition is reached. Let's take a look at the syntax for this one.

java
int x = 5;

while (x < 10)
{
System.out.println(x);
x++;
}


Well, not much to explain here, just put in the parentheses after while whatever you want the condition to stop, to be.


The do-while Loop

The do-while. This is much the same as the while loop, with only 1 difference. It MUST execute at least once, as it doesn't know what the condition to stop is, until after it executes the code once.

java
int number = 10;

do
{
System.out.println(number);
number += 10;
}
while (number <= 100);


Well, there's one major difference between the while line in the regular while loop, and this one. Notice the semicolon, as this must be there for a do-while loop, as it signifies the end of the statement of the do-while loop. In other words, it just signifies the end.

_____________________________________________________________________

Modification Section:

The for each Loop

NOTE: To use this loop you must have JDK 1.5 or later, as that was when this loop was created.

Well, this is useful for traversing arrays in a different manner. It just goes for the entire length of the array, or ArrayList.

java
// our 'int' array, NUMBERS, is pre-defined

for (int number : NUMBERS)
System.out.println(number);


The basic syntax for this is for (variable_to_use_inside_the_loop : array_to_look_in). It's the same as doing a for (int x = 0; x < NUMBERS.length; x++).
_____________________________________________________________________

This has been my basic introduction to java looping. It's not a very in-depth tutorial, but it's not supposed to be. Then again, looping is a fairly routine and easy thing to accomplish.

Hope this helped!
smile.gif

This post has been edited by Locke37: 1 Aug, 2008 - 09:40 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

abgorn
Group Icon



post 2 Aug, 2008 - 02:43 PM
Post #2
Nice, thanks for the good tutorial. icon_up.gif

I constantly forget stuff about loops
Go to the top of the page
+Quote Post

Locke37
Group Icon



post 3 Aug, 2008 - 02:09 PM
Post #3
Thanks for the feedback! smile.gif
Go to the top of the page
+Quote Post

Dexy
*



post 7 Aug, 2008 - 03:16 PM
Post #4
great thanks !! but can u show us nested loops
like 2 for loops !!??
thanks !!
Go to the top of the page
+Quote Post

Locke37
Group Icon



post 7 Aug, 2008 - 03:20 PM
Post #5
Oh! GOOD CALL

I'll edit it right now, while I'm thinking about it.

EDIT: Crap, it's too old to edit, but I've got a plan B. smile.gif

This post has been edited by Locke37: 7 Aug, 2008 - 03:22 PM
Go to the top of the page
+Quote Post

chili5
****



post 28 Sep, 2008 - 10:56 AM
Post #6
Great tutorial. smile.gif

This is new to me:

java

for (int number : NUMBERS)


I didn't know you can do that. I'll show that to my teacher tomorrow. Bookmarks page. smile.gif
Go to the top of the page
+Quote Post

Locke37
Group Icon



post 1 Oct, 2008 - 07:38 AM
Post #7
QUOTE(chili5 @ 28 Sep, 2008 - 11:56 AM) *

Great tutorial. smile.gif

This is new to me:

java

for (int number : NUMBERS)


I didn't know you can do that. I'll show that to my teacher tomorrow. Bookmarks page. smile.gif


Yup, you can do that as of Java 5 (JDK 1.5).

Just make sure your school has that or 6! biggrin.gif
Go to the top of the page
+Quote Post

amber.bosch
*



post 8 Oct, 2008 - 12:23 PM
Post #8
QUOTE(Locke37 @ 30 Jul, 2008 - 07:48 PM) *

Ok, Looping...so it's basically doing something while a certain condition is true. That sentence is basically one of the loops forms.


The for Loop

Ok, so the for loop. This is a useful looping structure for traversing through arrays and such. Let's take a look at the syntax shall we?

java
for (int x = 0; x < 100; x++)
{
// statements to execute 100 times
}


The for line, let's take a closer look at it...It starts with the for keyword of course, to signify that a loop follows. In the parentheses we have the loop control variable, x, which starts at zero (the first part, before the semicolon). The second part is what to stop at, in this case, 99, since x cannot be greater than or equal to 100. The third part is the value to increment by every time the end of the loop iteration is reached, in this case our shortcut is used for add 1 every time.


The while Loop

The while loop. A loop to continue going around...and around, and around until a certain condition is reached. Let's take a look at the syntax for this one.

java
int x = 5;

while (x < 10)
{
System.out.println(x);
x++;
}


Well, not much to explain here, just put in the parentheses after while whatever you want the condition to stop, to be.


The do-while Loop

The do-while. This is much the same as the while loop, with only 1 difference. It MUST execute at least once, as it doesn't know what the condition to stop is, until after it executes the code once.

java
int number = 10;

do
{
System.out.println(number);
number += 10;
}
while (number <= 100);


Well, there's one major difference between the while line in the regular while loop, and this one. Notice the semicolon, as this must be there for a do-while loop, as it signifies the end of the statement of the do-while loop. In other words, it just signifies the end.

_____________________________________________________________________

Modification Section:

The for each Loop

NOTE: To use this loop you must have JDK 1.5 or later, as that was when this loop was created.

Well, this is useful for traversing arrays in a different manner. It just goes for the entire length of the array, or ArrayList.

java
// our 'int' array, NUMBERS, is pre-defined

for (int number : NUMBERS)
System.out.println(number);


The basic syntax for this is for (variable_to_use_inside_the_loop : array_to_look_in). It's the same as doing a for (int x = 0; x < NUMBERS.length; x++).
_____________________________________________________________________

This has been my basic introduction to java looping. It's not a very in-depth tutorial, but it's not supposed to be. Then again, looping is a fairly routine and easy thing to accomplish.

Hope this helped!
smile.gif


I really like this but I noticed you are missing a few things. Could you redo a more detailed version, including if then else and nested loops? You would be the BEST!!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/1/08 05:02PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month