Results 1 to 8 of 8
  1. #1
    thyks is offline Member
    Join Date
    Jun 2012
    Posts
    19
    Rep Power
    0

    Question How to jump between lines of code?

    For the example, i have a 20 lines long code. Java is now reading 8 line. The question is: how to jump between lines of code? (ex. 8 to 3). b

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  3. #3
    thyks is offline Member
    Join Date
    Jun 2012
    Posts
    19
    Rep Power
    0

    Default Re: How to jump between lines of code?

    The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. This section describes the decision-making statements (if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language.
    Whoever can give me example how to jump between lines of code?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to jump between lines of code?

    Quote Originally Posted by thyks View Post
    Whoever can give me example how to jump between lines of code?
    You can't do that without a bit of trickery; Java is a structured programming language and has no goto statement; it has do-while, while and for for its loop structures. Even more: Java isn't a line based programming language.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: How to jump between lines of code?

    Are you talking about a goto statement that would allow you to jump from one line to another?
    Java does not have a goto statement. You have to use other types of statements, like a loop.

    Post some sample code that shows what you are talking about.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    thyks is offline Member
    Join Date
    Jun 2012
    Posts
    19
    Rep Power
    0

    Default Re: How to jump between lines of code?

    Are you talking about a goto statement that would allow you to jump from one line to another?
    Yes
    Post some sample code that shows what you are talking about.
    Java Code:
    import java.util.Scanner;
    public class ProgramMichs{
    	public static void main(String [] args){
    	Scanner scan = new Scanner(System.in);
    	System.out.print("\n-------------------------------");
    	System.out.print("\nI  Witaj w programie MICHS!!  I");
    	System.out.print("\nI-----------------------------I");
    	System.out.print("\nI    Wersja Java 0.01         I");
    	System.out.print("\nI Dopiero uczę się Javy, więc I");
    	System.out.print("\nI program wymaga wielu popra- I");
    	System.out.print("\nI            wek.             I");
    	System.out.print("\nI-----------------------------I");
    	System.out.print("\n[1] Gra w pytania");
    	System.out.print("\n[2] Autor\n");
    	System.out.print("[3] Creeper\n");
    	String in;
    	in = scan.nextLine();
    	if(in.equals("1"))
    		{
    		System.out.print("\nIle to jest 8 / 2?");
    		System.out.print("\n4");
    		System.out.print("\n3");
    		System.out.print("\n2");
    		System.out.print("\n1");
    		System.out.print("\n0\n");
    		in = scan.nextLine();
    		if(in.equals("4"))
    		{
    		System.out.print("\nDobrze!");
    		}
    		else
    		{
    		System.out.print("\nZle! 8 / 2 = 4.");
    		}
    	}
    	if(in.equals("2"))
    		{
    		System.out.print("\nAutor: XicXXl O, pseudo MXXhs");
    		}
    		if(in.equals("3"))
    		{
    		System.out.print("\nXX  XX");
    		System.out.print("\nXX  XX");
    		System.out.print("\n  XX");
    		System.out.print("\n XXXX");
    		System.out.print("\n XXXX");
    		System.out.print("\n X  X");
    		System.out.print("\n SSSSSSS");
    		System.out.print("\nCo teraz?");
    		System.out.print("\n[1] Uciekac");
    		System.out.print("\n[2] Zostac na miejsu\n");
    		in = scan.nextLine();
    		if(in.equals("1"))
    		{
    		System.out.print("\nUdalo ci sie zwiac Creeperowi!!");
                    /*For example, i want to jump from this line of code to 3th line.*/
    		}
    		else
    		{
    		System.out.print("\nSsSsSs... wiecej juz nie uslyszales bo nie zdazyles.");
    		}
    	}
    }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: How to jump between lines of code?

    You would wrap the code in a loop and use the continue statement to change the flow of execution back to the beginning of the loop.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: How to jump between lines of code?

    Yes. You can use loop + break / continue to instead of GOTO .

    Java Code:
    
    
       public static void main(String [] args){
        Scanner scan = new Scanner(System.in);
    for(;;) { //<-----------------------
        System.out.print("\n-------------------------------");
        System.out.print("\nI  Witaj w programie MICHS!!  I");
        System.out.print("\nI-----------------------------I");
        System.out.print("\nI    Wersja Java 0.01         I");
        System.out.print("\nI Dopiero uczę się Javy, więc I");
        System.out.print("\nI program wymaga wielu popra- I");
        System.out.print("\nI            wek.             I");
        System.out.print("\nI-----------------------------I");
        System.out.print("\n[1] Gra w pytania");
        System.out.print("\n[2] Autor\n");
        System.out.print("[3] Creeper\n");
        String in;
        in = scan.nextLine();
        if(in.equals("1"))
            {
            System.out.print("\nIle to jest 8 / 2?");
            System.out.print("\n4");
            System.out.print("\n3");
            System.out.print("\n2");
            System.out.print("\n1");
            System.out.print("\n0\n");
            in = scan.nextLine();
            if(in.equals("4"))
            {
            System.out.print("\nDobrze!");
            }
            else
            {
            System.out.print("\nZle! 8 / 2 = 4.");
            }
            break;//<-------------------
        }
        if(in.equals("2"))
            {
            System.out.print("\nAutor: XicXXl O, pseudo MXXhs");
                    break;//<-------------------
            }
            if(in.equals("3"))
            {
            System.out.print("\nXX  XX");
            System.out.print("\nXX  XX");
            System.out.print("\n  XX");
            System.out.print("\n XXXX");
            System.out.print("\n XXXX");
            System.out.print("\n X  X");
            System.out.print("\n SSSSSSS");
            System.out.print("\nCo teraz?");
            System.out.print("\n[1] Uciekac");
            System.out.print("\n[2] Zostac na miejsu\n");
            in = scan.nextLine();
            if(in.equals("1"))
            {
            System.out.print("\nUdalo ci sie zwiac Creeperowi!!");
                    /*For example, i want to jump from this line of code to 3th line.*/
            }
            else
            {
            System.out.print("\nSsSsSs... wiecej juz nie uslyszales bo nie zdazyles.");
                    break;//<-------------------
            }
        }
    }
        }




    Ksharp
    Last edited by Ksharp; 06-25-2012 at 07:57 AM.

Similar Threads

  1. Optimising this simple piece of code (30 lines)
    By hvince95 in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 08:43 AM
  2. lines truncated in code formatting
    By zweibieren in forum Suggestions & Feedback
    Replies: 6
    Last Post: 12-19-2011, 06:13 PM
  3. Question about some lines of code arrays
    By silverglade in forum New To Java
    Replies: 9
    Last Post: 06-09-2011, 04:59 AM
  4. Please explain these 2 lines of code to me..
    By murphaph in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 02:11 PM
  5. Unify these 3 lines of code
    By ulykidjoe in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2007, 01:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •