- A precondition loop is used when the number of iterations is not known beforehand and is dependent on a condition being true
- It is also known as an indefinite loop
- The loop will continue to execute while the condition is true and will stop once the condition becomes false
Example in Pseudocode:
INPUT temperature
WHILE temperature > 37 DO
OUTPUT "Patient has a fever"
INPUT temperature
END WHILE
Example in Python:
temperature = float(input("Enter temperature: "))
while temperature > 37:
print("Patient has a fever")
temperature = float(input("Enter temperature: "))
Example in Java:
Scanner input = new Scanner(System.in);
float temperature = input.nextFloat();
while (temperature > 37) {
System.out.println("Patient has a fever");
temperature = input.nextFloat();
}
Example in Visual Basic:
temperature = InputBox("Enter temperature")
Do While temperature > 37
Console.WriteLine( "Patient has a fever")
Console.WriteLine("Enter temperature")
temperature=Console.ReadLine()
Loop