what exactly are you looking to find out about loops exactly?
For starters, there are for loops: these iterate and alter a variable until the iterator meets a condition.
for (int i = 0; i < 10; i++)
{
System.out.println(i);
}
prints 0 through 9
while loops iterate while a bool conditional is met:
boolean stop = true;
while (stop)
{
System.out.println("stop after line");
stop = false;
}
it's also good to test if something is true before a break with if else structures
public class test{
public static void main(String args[]){
boolean stop = true;
char test='x';
//notice that test is initialized to 'x'... this will make stop = false, exiting //while loop
while (stop)
{
if (test == 'x'){
System.out.println("stop after this line");
stop = false;
}
else
{
stop = true;
System.out.println("infinite loop!");
}
}
System.out.println("ended");
}
}
if you don't have a way of evaluating a boolean value to the needed condition to exit the loop, you will end up in a VERY ANNOYING INFINITE LOOP...
public class test{
public static void main(String args[]){
boolean stop = true;
char test='a';//initialized as 'a' instead of 'x'
while (stop)
{
if (test == 'x'){//does not ever evaluate to 'a' so it loops forever!
System.out.println("stop after this line");
stop = false;
}
else
{
stop = true;
System.out.println("infinite loop!");
}
}
System.out.println("ended");
}
}
a variation on the while loop is the "do while" loop which executes a body of code until a boolean condition is met to exit the body of code.
public class test{
public static void main(String args[]){
char test='x';
do
{
System.out.println("This will never print");
}
while (test == 'a');
System.out.println("ended");
}
if by way of input or a conditional statement you evaluate test to 'a', you will get looping until test != 'a'
this should start you off... remember you can nest your looping structure with in one another as long as you can preserve scoping. functions and methods work well for that.