Results 1 to 11 of 11
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Thumbs up help me in this program please (break and continue statement s)

    hi guys ,

    i could not understand the logic of this program given below , please tell me the logic :(

    Java Code:
    public class BreakAndContinue
    {
      public static void main(String[] args)
      {
     
       for(int i=0; i<100; i++)
         {
      if(i==75) break;//out of for loop
    if(i%9!=0) continue; //next iteration 
    System.out.println(i);
         }
    int i=0;
    //An "infinite loop":
    while(true)
    
           {
    i++;
    int j=i*27;
    if(j==1269) break;// out of loop
    if(i % 10!=0) continue; //top of loop 
    System.out.println(i);
           }
       }
    }
    Java Code:
    OUTPUT :
    
    0
    9
    18
    27
    36
    45
    54
    63
    72
    10
    20
    30
    40
    please guys help me in giving me the reason for this out put :(
    Last edited by funkygarzon; 01-07-2011 at 11:33 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    break exits a loop.
    continue causes it to jump to the next iteration (if there is one), missing out any remaining code in the loop.
    Java Code:
    while (true) {
        break;  // This will stop this loop and go onto the next line after the close bracket
    }
    while (true) {
        continue;  // This will go back round the loop ignoring the following lines.
        doSomething();
    }
    Now you should be able to step through that code and figure out why you get that result.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Here is a link with the comparison : Break-Continue

    Have a look in case if you are interested.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    break exits a loop.
    continue causes it to jump to the next iteration (if there is one), missing out any remaining code in the loop.
    Java Code:
    while (true) {
        break;  // This will stop this loop and go onto the next line after the close bracket
    }
    while (true) {
        continue;  // This will go back round the loop ignoring the following lines.
        doSomething();
    }
    Now you should be able to step through that code and figure out why you get that result.

    thanks for the reply buddy , Actually my doubt is

    what is
    Java Code:
    [b]while(true)[/b]
    . what does it do actually ? :(

    Quote Originally Posted by goldest View Post
    Here is a link with the comparison : Break-Continue

    Have a look in case if you are interested.

    Goldest
    thanks for the link about "break and continue " keyword buddy , with the help of below program i understand that "break " keyword will entirely stops the execution of the loop
    ,but i could not clearly understand the second "for" loop i.e., with the "continue" keyword
    Java Code:
    public class BreakContinue{
    public static void main(String argv[]){
    	System.out.println("Break");
    	for(int i=1; i < 4; i++){
    		System.out.println(i);
    			if(i > 1) break;
    		}
    	System.out.println("Continue");
    	for(int i=1; i < 4; i++){
    		System.out.println(i);	
    			if(i > 1) continue;
    		}
    	}
    }
    
    This code will output Break 1 2 followed by  Continue 123.  This is because the break continue entirely stops the execution of the for loop whereas the continue statement only stops the current iteration.
    my doubt is , the below statement
    Java Code:
    [b]"continue statement only stops the current iteration."[/b]
    how does the "continue " keyword is stopping the current iteration in the above BreakContinue program ?
    Last edited by funkygarzon; 01-06-2011 at 08:04 PM.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Ok then, here is the explanation.

    Java Code:
    public class BreakContinue {
    	public static void main(String argv[]) {
    		System.out.println("Break");
    		for (int i = 1; i < 4; i++) {
    			System.out.println(i);
    			if (i > 1)
    				break; //line 7
    			System.out.println("B");
    		} //line 9
    		System.out.println("Continue");
    		for (int i = 1; i < 4; i++) { //line 11
    			System.out.println(i);
    			if (i > 1)
    				continue; //line 14
    			System.out.println("C"); //line 15
    		} //line 16
    	}
    }
    In the above code I have added two additional println statements after the break and continue statements.

    Now in break for loop, when your i becomes greater than 1 and you reach at line 7, the break statement makes the control to jump to line 9 directly, which is the end of the entire for loop. And hence there won't be any further iterations.

    Now in your continue for loop, when your i becomes greater than 1 and you reach at line 14, the continue statement stops the current iteration, means it makes the control jump to line 11 [not to line 16 like break statement] and it skips the content at line 15 which is the part of current iteration. Now the next iteration i.e. i++ happens and next flow gets on the way.

    If you look at the output,

    Java Code:
    Break
    1
    B
    2
    Continue
    1
    C
    2
    3
    Notice, when the break happens nothing gets iterated further and the for loop itself ends instantly.

    But when the continue happens, the content after the continue gets skipped but not the for loop. The control simply moves to next iteration. But C won't be printed whenever i is greater than 1.

    I hope thats clear enough,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Ok then, here is the explanation.

    Java Code:
    public class BreakContinue {
    	public static void main(String argv[]) {
    		System.out.println("Break");
    		for (int i = 1; i < 4; i++) {
    			System.out.println(i);
    			if (i > 1)
    				break; //line 7
    			System.out.println("B");
    		} //line 9
    		System.out.println("Continue");
    		for (int i = 1; i < 4; i++) { //line 11
    			System.out.println(i);
    			if (i > 1)
    				continue; //line 14
    			System.out.println("C"); //line 15
    		} //line 16
    	}
    }
    In the above code I have added two additional println statements after the break and continue statements.

    Now in break for loop, when your i becomes greater than 1 and you reach at line 7, the break statement makes the control to jump to line 9 directly, which is the end of the entire for loop. And hence there won't be any further iterations.

    Now in your continue for loop, when your i becomes greater than 1 and you reach at line 14, the continue statement stops the current iteration, means it makes the control jump to line 11 [not to line 16 like break statement] and it skips the content at line 15 which is the part of current iteration. Now the next iteration i.e. i++ happens and next flow gets on the way.

    If you look at the output,

    Java Code:
    Break
    1
    B
    2
    Continue
    1
    C
    2
    3
    Notice, when the break happens nothing gets iterated further and the for loop itself ends instantly.

    But when the continue happens, the content after the continue gets skipped but not the for loop. The control simply moves to next iteration. But C won't be printed whenever i is greater than 1.

    I hope thats clear enough,

    Goldest

    wooooooooooooooooooooooooooooooow incredible explanation .. thanks a lot i clearly understood the way you explained with some additional line in the program .... :) thank you buddy . so now i can clearly understand the logic of the both the program s .

    now my doubt is, why we are using the below line ?
    Java Code:
    [b]while(true)[/b]
    what is the use of this line ?
    Last edited by funkygarzon; 01-06-2011 at 08:44 PM.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by funkygarzon View Post
    so now i need to match the exact logic with my old BreakAndContinue
    program :(
    Exactly!

    And best of luck for that :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Exactly!

    And best of luck for that :)

    Goldest
    buddy , now i clearly understood the concept of both the programs . please tell me the use of
    Java Code:
    while(true)
    . :confused:

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    The while statements are used to execute some piece of code until the while condition is met.

    Here is the syntax for that,

    Java Code:
    while (expression) {
         statement(s)
    }

    The expression above is anything that can be evaluated to a boolean i.e. true or false.

    If we want to make sure that the while statement should be entered at any cost, we can put the hard coded boolean value as true. That makes sure that it gets executed for sure. Thats often referred to as infinite loop.

    Here is the link for while and do-while statements : While-doWhile

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Goldest summarized it well, the code
    Java Code:
    while(true)
    creates an infinite loop, no matter what it goes into the next step of the loop. The break; clause is added to have it end at some point. The idea of that is really just to show you another way to produce loops.

    it could have easily also been
    Java Code:
    for(int i = 0; i < 41; i++)
    {
       if(i % 10 == 0)
       {
         System.out.println(i);
        }
    }
    This would have looped through, everytime i is divisible by 10, it would print i.
    so, 10, 20, 30, 40. once i was 41 or larger it would end the loop

    to use break and continue it could have been
    Java Code:
    for(int i = 0; i < 100; i++)
    {
       if(i % 10 != 0)
       {
         continue;
       }
       if(i == 41)
       {
          break;
        }
        else
       {
         System.out.println(i);
       }
    }
    For this code, everytime i is not divisible by 10 it would call continue, which would increment i and re enter the loop, the only way it would get to the else clause is if i < 41 and i % 10 == 0.

    Also, that example looks familiar, are you reading thinking in java?

  11. #11
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Goldest summarized it well, the code
    Java Code:
    while(true)
    creates an infinite loop, no matter what it goes into the next step of the loop. The break; clause is added to have it end at some point. The idea of that is really just to show you another way to produce loops.

    it could have easily also been
    Java Code:
    for(int i = 0; i < 41; i++)
    {
       if(i % 10 == 0)
       {
         System.out.println(i);
        }
    }
    This would have looped through, everytime i is divisible by 10, it would print i.
    so, 10, 20, 30, 40. once i was 41 or larger it would end the loop

    to use break and continue it could have been
    Java Code:
    for(int i = 0; i < 100; i++)
    {
       if(i % 10 != 0)
       {
         continue;
       }
       if(i == 41)
       {
          break;
        }
        else
       {
         System.out.println(i);
       }
    }
    For this code, everytime i is not divisible by 10 it would call continue, which would increment i and re enter the loop, the only way it would get to the else clause is if i < 41 and i % 10 == 0.

    Also, that example looks familiar, are you reading thinking in java?

    woooooooow really good explanation buddy , thanks a lot for your effort to make to understand the concept . :)

    Java Code:
    are you reading thinking in java?
    no buddy , i saw this example in one book prepared by IGate company for java trainee :)


    Quote Originally Posted by goldest View Post
    The while statements are used to execute some piece of code until the while condition is met.

    Here is the syntax for that,

    Java Code:
    while (expression) {
         statement(s)
    }

    The expression above is anything that can be evaluated to a boolean i.e. true or false.

    If we want to make sure that the while statement should be entered at any cost, we can put the hard coded boolean value as true. That makes sure that it gets executed for sure. Thats often referred to as infinite loop.

    Here is the link for while and do-while statements : While-doWhile

    Hope that helps,

    Goldest
    thanks a lot buddy , now i understood "while(true)" will blankly allow the execution of remaining code assuming that the boolean condition is true :) .
    And i already know "while" and "do -while" in C language , but still your link helped me to learn a lot . thanks a lot Goldest :)

Similar Threads

  1. break statement
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 04:42 PM
  2. using from lable with break and continue
    By arefeh in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 01:52 PM
  3. Help getting my program to continue requesting info.
    By antdilla22 in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 11:44 PM
  4. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 11:28 PM
  5. Demonstration of break and continue keywords
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:47 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
  •