Page 1 of 1

Looping Basic concept, nice for beginners Rate Topic: ****- 1 Votes

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 30 July 2008 - 07:48 PM

*
POPULAR

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?

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.

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.

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.

// 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!
:)

This post has been edited by Locke37: 01 August 2008 - 10:40 PM


Is This A Good Question/Topic? 5
  • +

Replies To: Looping

#2 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Posted 02 August 2008 - 03:43 PM

Nice, thanks for the good tutorial. :^:

I constantly forget stuff about loops
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 03 August 2008 - 03:09 PM

Thanks for the feedback! :)
Was This Post Helpful? 0
  • +
  • -

#4 Dexy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-August 08

Posted 07 August 2008 - 04:16 PM

great thanks !! but can u show us nested loops
like 2 for loops !!??
thanks !!
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 07 August 2008 - 04:20 PM

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. :)

This post has been edited by Locke37: 07 August 2008 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Posted 28 September 2008 - 11:56 AM

Great tutorial. :)

This is new to me:

for (int number : NUMBERS)  



I didn't know you can do that. I'll show that to my teacher tomorrow. Bookmarks page. :)
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 01 October 2008 - 08:38 AM

View Postchili5, on 28 Sep, 2008 - 11:56 AM, said:

Great tutorial. :)

This is new to me:

for (int number : NUMBERS)  



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


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

Just make sure your school has that or 6! :D
Was This Post Helpful? 0
  • +
  • -

#8 amber.bosch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-October 08

Posted 08 October 2008 - 01:23 PM

View PostLocke37, on 30 Jul, 2008 - 07:48 PM, said:

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?

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.

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.

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.

// 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!
:)


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!!
Was This Post Helpful? 0
  • +
  • -

#9 jv44heinzbar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-February 09

Posted 23 March 2009 - 10:21 AM

Bump for nested loops!

HB
Was This Post Helpful? 0
  • +
  • -

#10 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Posted 23 March 2009 - 01:09 PM

Why?

A nested loop is just a loop in a loop.

for(int i=0;i<5;i++) {
	  for (int x=0;x<5;x++) {

	  }
}



Basically the inner loop executes 5 times for each execution of the outer loop. Not much to write another tutorial about. So the inner loop executes 25 times.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1