Assertions, Pre- and Post-Conditions and Loop Invariants

Assertions, Pre-conditions, and Post-conditions

Before we can define and use loop invariants, we must first understand the concept of assertions, pre-conditions and post-conditions in a programming language.

Pre- and Post-Conditions

The following video uses Python to explain the role of pre- and post-conditions as an important aid in testing a program.

Assertions in Java

Note that (as is mentioned in the video, below), the version of IntelliJ you are running may require you to enable assertions before using them. How to do this is shown at the beginning of the video, below.

Loop Invariants

Loop invariants are formalized statements that are true before and after each loop iteration. Understanding loop invariants can sometimes lead to a bettter understanding of a loop's function (i.e. what it does, and what it does not do). One common misunderstanding students have about loop invariants is that they only need to be true before and after each iteration of the loop. Loop invariants do not have to be true during each loop iteration. This is a somewhat theortical topic which may come up during a software interview or during the debugging process of a large program.