advantage of using assert in program

i want to know that some programmer use assert in their program , is there any advantage of using assert while running the program on judge where all the constraints are defined and are guaranteed that variables follow these constrains ??

like this program PRETNUM tester1 this solution tester uses assert for all the constraints.

@ajay154 Assert statement can be used to debug your program running on online judge. Whenever assert statement returns false, online judge throws a Run Time Error.

Example : Assume that you are getting WA due to some variable say X is exceeding some limit say Y. Now if you put a assert statement such as assert(X <= Y); , you will receive a RTE instead of WA and you will know that variable X is exceeding limit Y.

Thanks & Regards



This link should be helpful.


Hello @ajay154,

I don’t really believe there’s any real “advantage” in using asserts during a contest, at least, using asserts will not make your solutions better from the algorithmic point of view…

Usually, testers always use asserts when writing their solutions (which are written during the testing phase of the contest, done on the “backstage”, if you want to put it that way) because they need to make sure that the input files are well formatted and respect the given constrains. This is done mostly for two reasons:

  1. It ensures that the test data is formatted and consistent with the constrains given on the problem statement;

  2. It gives the problem setter’s the possibility of fixing any mistakes, if there are any;

This way, it is 100% checked that the problem is well-written and that the test data is well formatted.

As this is done during the testing phase, competitors usually don’t need to use asserts when writing code :slight_smile:

Hope this was helpful.



1 Like

And that’s not fair, that one can use assert in C++, but not in Java :-/


It can help you to debug the code, instead of WA you get RE and you can try to find out why you assumption is incorrect…


@betlista you can throw a runtime error whenever the constraint is broken. Pretty much the same in java. Just some extra characters to code the assert function. :wink:


It is not the same, because you will get NZEC instead of SIGABRT…

Why not get FPE instead of SIGABART for JAVA USERS…!!

Ex: You can use, if(X > Y) { print(1/0); }

It will show FPE if X exceeds the limit Y…!!

So please show me, where it shows FPE, thanks…

Status is RE as I can see…