[TEX]. . . i need some tips on programming java with looping structure, tnx . . .[/TEX]

Member Avatar for coil

Those code blocks that you used are reserved for LaTeX typesetting - it's why your text turned out in italic and without spaces. You don't need to use them if you're not using LaTeX.

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.

commented: Good stuff. Well done. +1

I thought this--> http://leepoint.net/notes-java/flow/loops/loops.html was a helpful example of the various Java loop structures and how and when they evaluate conditions.

Please also note that the 'goto' branch statement is heavily abused and misused- almost NEVER an acceptable programming style as it creates 'spaghetti' code that is hard to follow and not portable in the way an OOP language such as Java was meant for. Because of modular programming styles with functions and methods with loops you never have to use it. Any program written today that uses goto can be rewritten without it. Just FYI

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.