00:00
00:00
Newgrounds Background Image Theme

MatthieuxDancingDead just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Java: Loops

2,372 Views | 2 Replies
New Topic Respond to this Topic

Java: Loops 2006-06-15 09:52:03


Java: Main.

This is a very basic, yet very important tutorial in understanding Java and all of the programming languages. The loops are a very vital tool whenever it comes to programming. So basically, what a loop does is to repeatedly execute a command or a set of commands as long as a given condition is true. It gets out of the loop when the condition is false.
There are three loops in Java:

1) while(condition)//the most basic loop.
2) for(opening statement;condition;command)//it is very comfortable and used a lot.
3) do...while(condition)//something like the while loop, just a bit different.

So let's start.

1. The while loop-while(condition)

The while loop, as I mentioned before, is the most basic loop and all of the other loops are taken from it in one way or another. The while loop simply checks whether a certain condition is true, if it is, it executes every command inside the body of the loop, if it isn't, it doesn't. The body of the loop is every command between these two parentheses ''{ }''. For example, in this code:

Code 1:

int i=3, j=5, product=0; //creates 3 new int variables and puts 3, 5 and 0 into them.
while(i>0) //the body of the while loop will continue to be executed as long as i>0.
{ //the beginning of the body of the loop.
product+=j; //replaces the current value of product with product+j.
i--; //replaces the variable i with i-1.
} //the end of the body of the loop.
System.out.print(product); //prints the variable 'product'.

Try to think what this code does before reading the answer at the end of the post.
Hopefully, now you know what a while loop does, so we can move on to a bit more complicated loop, but it is much more comfortable.

2. The for loop-for(opening statement;condition;command)

As I mentioned, the for loop doesn't only get a condition, but also an opening statement and a command. The way the for loop operates is similar to the way the while loop operates, but there is room inside the round parentheses "( )" for two more things. The first thing the computer does in a for loop is to execute the opening statement, which is the first one in the parentheses (I hope this is how you call these...). After doing this, it checks whether to condition is true, if it is true, it executes the body of the loop, else it gets out of it. After finishing executing the body of the loop, it executes the command at the end and checks again if the condition is true, if it is, it continues executing the body of the loop. This is, of course, until the condition is not true (or that it got a command in the loop to break it). For example, this code:

Code 2:

for(int i=0;i<=4;i++)
// the line above firstly creates an int variable and puts 0 into it. After that, it checks if the int
// is smaller or equal to 4, if it is, it executs the body of the loop. Aftet it finishes executing
// it replaces the int i with i+1.
{ //the beginning of the body of the loop.
System.out.print("*"); //prints *.
} //end of the body of the loop.

Try to think what this code does before reading the answers at the end of my post.
Also, assuming you don't want an opening statement/command, you don't have to use it, instead you can write:
for(;i<n;i++)
or:
for(int i=0;i<n;)
However, there must be a condition in the middle and 2 of these ";".
Now that you know how to use the for loop, we will move on to another loop.

3.do...while loop-do{...}while(condition)

This loop can be easily replaced by a while loop, but sometimes it makes programming much easier. The way this loop it written is this:
do
{
Blabla;
}while(condition)

The difference between this loop and a while loop is the the do…while loop executes its body at least once. For example, this code:

Code 3:

int i=0;
do
{
System.out.print(i);
i++;
}while(i<5);

This time, it is without my interpretations, try to figure it out by yourself. Also, think what it does before you see the answer at the end of my post.

4. Global things you need to know
You need to remember these signs for conditions:
'>' bigger than.
'<' smaller than.
'<=' equal or smaller.
'>=' equal or bigger.
'==' equal (not one, but two!!!).
'!=' not equal.
'%' modul or modullu or mod or whatever you call it.
Also, if you want to have more than one condition in your loop, you can by these two signs:
'&&' only if all of the conditions are true than the phrase inside the parentheses is true.
'||' if at least one of the conditions is true, than the whole phrase is true.
Also, '!' means 'not', if you put it beofre an expression, it will reverse it. For example:
while(!(i>n))//if i>n, than the not will turn the whole expression into false, if i<=n, the not will
// turn it into true.
There also is a command called 'break' which gets out of the loop. However, it is considered to be very not elegant to use it, so don't use it! >:-(

After finishing all the three loops, we can try and use this knowledge in order to program something a bit more complicated. I will write here the code of a program which finds the smallest divider (which is not 1 or the number itself) of a given num and prints it. If the number doesn't have any dividers, it prints "none".

import java.util.Scanner; //imports a scanner which allows you to get the user input.
Scanner scan=new Scanner(System.in); //creates the scanner.
int num=scan.nextInt(); //scans the next number from the user.
boolean check=false; //a nice way to surround break.
for(int i=2;(i<=num/2)&&(check==false);i++) //figure it out yourself ;-)
{
if(num%i==0) //if num divides by i.
{
System.out.println(i); //prints the devider-i.
check=true; //changes check into true.
}
}
if(check==false)
System.out.println("none");

The answers to the codes from before:

Code 1- It prints the product of 3 and 5, which means 15, to the screen.
Code 2- it prints 5 stars.
Code 3- it prints 01234.

Hopefully now you know how to do java loops, since it is a very important tool in java and programming as a whole.

If you want to ask anything, to comment about anything or to add something, post.


BBS Signature

Response to Java: Loops 2006-06-15 10:56:26


Good job :)

Response to Java: Loops 2006-06-15 11:09:35


At 6/15/06 10:56 AM, seel wrote: Good job :)

Thank you, I am glad to see you liked it :-)


BBS Signature