Assertion is used to terminate the program while break is used to terminate loop, am i right? Or Both are interchangeable? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Assertion is used to terminate the program while break is used to terminate loop, am i right? Or Both are interchangeable?

20th Jan 2017, 5:21 PM
Gaurav
Gaurav - avatar
4 Réponses
+ 4
Assertion is used to be sure of a condition in your programme. And the break statement are used to break out of a loop. They are NOT interchangeable.
20th Jan 2017, 7:16 PM
Amaras A
Amaras A - avatar
+ 4
More to the point, assertions are used to verify invariants, i.e., conditions that must hold true for the program to proceed. You should not use assertions to exit your program normally nor to check for "normal" error conditions such as invalid user input. Those should be anticipated by the program flow or handled with specific exceptions. Use assertions to terminate the program in case something impossible happens that can never be recovered from, such as the length of a list being negative or conditions of a similar caliber.
31st Jan 2017, 2:05 PM
Phipps
+ 2
yes you are right
20th Jan 2017, 5:23 PM
Sanchit Pandey
Sanchit Pandey - avatar
0
Thanks Phipps for the details :)
31st Jan 2017, 2:11 PM
Gaurav
Gaurav - avatar