• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


with assertion enabled.the above programs gives 012.That part i got it.But with assertion disabled how it displays infinite times.When it comes to execute
while 3<3 condn it returns false.I thought with assertion disabled also it will come out of loop.Please clear my doubt.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
...with assertion disabled how it displays infinite times. When it comes to execute while 3<3 condn it returns false...

With assertions disabled, the expression following "assert" is not evaluated. So in this code, i1 is never incremented.

It's not that i1<3 returns false when i1 is 3. It's that i1 remains at zero.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this with assertions enabled, and with assertions disabled...
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir! Excellent coding explanation.Thank you very much.
Without enabled assertions,it displayed all x value 0.(but i don't know why that increment doesn't happen)
does that mean we used inappropriate use of assertions.
I mean "Do not use assert expression that can cause side effects".
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.(but i don't know why that increment doesn't happen)
does that mean we used inappropriate use of assertions.
I mean "Do not use assert expression that can cause side effects".


Exactly. Use assertions only for evaluating conditions that you know should never arise. In other words, the assertion code should not modify the data and behaviour of your program in any way. The compiler will not complain, but it is a bad programming practice and misuse of assertions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic