• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I am preparing for OCA 8 exam

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I cam across a question. I dont really understand the output. Can you please help me with this.

public class Circles
{
public static void main(String[] args)
{
int[] ia = {1,3,5,7,9};
for(int x :ia)
{
for(int j=0;j<3;j++)
{
if(x>4 && x<8)
continue;
if(j==1)
break;
continue;
}
continue;
}
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s utk wrote:I dont really understand the output. Can you please help me with this.



There is nothing in the code that generates output. So, there should be no output.

Can you tell us what you are getting?

Henry
 
s utk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Sorry my mistake .

Below is the actual code snippet.
public class Circles
{
public static void main(String[] args)
{
int[] ia = {1,3,5,7,9};
for(int x :ia)
{
for(int j=0;j<3;j++)
{
if(x>4 && x<8)
continue;
System.out.print(" "+x); //Missed this line before
if(j==1)
break;
continue;
}
continue;
}
}
}

This is from Oca/Ocp programmer's guide by Kathy and Bert. I don't really understand how break and continue affect the output.

 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The continue statement (without using a label) will end the current iteration, and start again the evaluation of the expression boolean of the loop that the continue statement belongs to. On the other hand, the break statement (again, without using a label) will end the current iteration and break out the loop that it belongs to, transfering the flow of control to the outer statement, which is, in this case, the outer for loop.

So, with that being said, grab a paper and a pencil, and write down the x and j values for each iteration. If the x value matches the boolean expression of the first if statement, apply the continue statement as I said before. If the j value matches the second if statement, apply the break statement. At the end, you will have the output. Remember to analyze each value for each iteration.
Pay attention to the if statements, because they control the flow of the loops, so the System.out.print() inside the inner loop depends on them.

You might notice that the continue statements in the last line of each loop aren't necessary, since they will end the current iteration, but there is no code after them, so the iteration would end anyway.
 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know the exam won't format it this nicely but it helps to explain what is happening (and I added a few comments to denote line numbers to assist with the explanation):


The outer for loop (or for-each as some like to refer to it now) is fairly simple. It just iterates through the array elements. The inner for loop is of more interest. It is checking your knowledge on the short circuit operator as well as what break and continue do.
Since the continue and/or break statements do not refer to a label, they will only apply to their enclosing code block (or for loop in this case). Thus, line 11, 14, and 15 only apply to the inner for loop. The continue statement on line 17 applies to the outer for loop since that is its enclosing structure. Coincidentally, the continue statement on line 17 serves no purpose as the loop will iterate on its own without this statement. It isn't required at all to produce the same functionality.

Line 10 is what determines why the array values of 5 and 7 are not output. They are the only two values that meet the short circuit and operator condition which allows the continue statement on line 11 to execute. The prevents these values from being output. When the last value of the outer for loop (9) is interrogated, it does not meet the criteria and as such, outputs just like the values of 1 and 3 did earlier.

Things I take away from this example are:
- understand the difference between the break and continue statement
- understand the impact of using a label with code blocks and their impact to the break and continue statements
- watch for crazy formatting on the exam (until I took a deeper look, I thought line 15 would through a compiler warning of unreachable code but then realized it is not part of the previous if condition
 
s utk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Chris and Victor. It makes more sense to me now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic