We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,965 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java looping structure

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

4
Contributors
4
Replies
19 Hours
Discussion Span
2 Years Ago
Last Updated
6
Views
nix_xin
Newbie Poster
17 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

http://leepoint.net/notes-java/flow/loops/loops.html
Use spaces when you type in english.

Akill10
Posting Pro
586 posts since Sep 2010
Reputation Points: 115
Solved Threads: 81
Skill Endorsements: 0

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.

coil
Deleted Member

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.

brandonrunyon
Junior Poster
154 posts since Sep 2007
Reputation Points: 32
Solved Threads: 14
Skill Endorsements: 0

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

brandonrunyon
Junior Poster
154 posts since Sep 2007
Reputation Points: 32
Solved Threads: 14
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0675 seconds using 2.67MB