Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about assert

 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source: http://www.javacertifications.net/javacert/assertions.jsp


Is this explanation to the question is really true?
 
Jehaan Butt
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explanations : When if condition returns true, the assert statement also returns true.
Hence AssertionError not generated.


This statement in itself is true. If i<j, then naturally i!=j would not give an assertion error. It is a correct, but highly cut down version of the program explanation. The program works like this:

On the first iteration of the inner loop, i=j=2, therefore, the assert statement is not reached. Second iteration, i!=j (i=2, j=3). Here, the assert is reached, and its condition is satisfied, and so on.

What the essence of the program is, is that i can have any value with respect to j. If it is equal to or greater than j, then the assert is not reached. If i is less than j, the assert is reached, but is true. Hence, if the "if" is true, the assert is true, which is what the explanation says.
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I see it thanks for replay Anurag!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic