Condition Examples

Dave Braunschweig

Temperature

Pseudocode

Function Main
    Declare String choice
    Declare Real temperature
    Declare Real result
    
    Assign choice = GetChoice()
    If Choice = "C" Or Choice = "c"
        Assign temperature = GetTemperature("Celsius")
        Assign result = CalculateCelsius(temperature)
        Call DisplayResult(temperature, "Fahrenheit", result, "Celsius")
    False:
        If Choice = "F" Or Choice = "f"
            Assign temperature = GetTemperature("Fahrenheit")
            Assign result = CalculateFahrenheit(temperature)
            Call DisplayResult(temperature, "Celsius", result, "Fahrenheit")
        False:
            Output "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
        End
    End
End

Function CalculateCelsius (Real fahrenheit)
    Declare Real celsius
    
    Assign celsius = (fahrenheit - 32) * 5 / 9
Return Real celsius

Function CalculateFahrenheit (Real celsius)
    Declare Real fahrenheit
    
    Assign fahrenheit = celsius * 9 / 5 + 32
Return Real fahrenheit

Function DisplayResult (Real temperature, String fromLabel, Real result, String toLabel)
    Output temperature & "° " & fromLabel & " is " & result & "° " & toLabel
End

Function GetChoice
    Declare String choice
    
    Output "Enter F to convert to Fahrenheit or C to convert to Celsius:"
    Input Choice
Return String choice

Function GetTemperature (String label)
    Declare Real temperature
    
    Output "Enter " & label & " temperature:"
    Input temperature
Return Real temperature

Output

Enter C to convert to Celsius or F to convert to Fahrenheit:
 c
Enter Fahrenheit temperature:
 100
100° Fahrenheit is 37.7777777777778° Celsius

Enter C to convert to Celsius or F to convert to Fahrenheit:
 f
Enter Celsius temperature:
 100
100° Celsius is 212° Fahrenheit

Enter C to convert to Celsius or F to convert to Fahrenheit:
 x
You must enter C to convert to Celsius or F to convert to Fahrenheit.

Flowchart

File:Flowgorithm Conditions ProcessCelsius.svg File:Flowgorithm Conditions ProcessFahrenheit.svg

File:Flowgorithm Conditions GetChoice.svg File:Flowgorithm Conditions GetTemperature.svg

File:Flowgorithm Conditions CalculateCelsius.svg File:Flowgorithm Conditions CalculateFahrenheit.svg

File:Flowgorithm Conditions DisplayResult.svg

References

License

Icon for the Creative Commons Attribution-ShareAlike 4.0 International License

Programming Fundamentals by Dave Braunschweig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book