कंप्यूटर प्रोग्रामिंग/कंडीशन/स्विफ्ट

विकिविश्वविद्यालय से

conditions.swift[सम्पादन | स्रोत सम्पादित करें]

// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given
// temperature and displays the result.

import Foundation

func toCelsius()
{
    var f: Double
    var c: Double
    
    print("Enter Fahrenheit temperature:")
    f = Double(readLine(strippingNewline: true)!)!
    c = (f - 32) * 5 / 9
    print(String(f) + "° Fahrenheit is " + String(c) + "° Celsius")
}

func toFahrenheit()
{
    var c: Double
    var f: Double
    
    print("Enter Celsius temperature:")
    c = Double(readLine(strippingNewline: true)!)!
    f = c * 9 / 5 + 32
    print(String(c) + "° Celsius is " + String(f) + "° Fahrenheit")
}

func ifElse()
{
    var choice: String
    
    print("Enter F to convert to Fahrenheit or C to convert to Celsius:")
    choice = readLine(strippingNewline: true)!
    if choice == "C" || choice == "c" 
    {
        toCelsius()
    } 
    else if choice == "F" || choice == "f" 
    {
        toFahrenheit()
    } 
    else 
    {
        print("You must enter C to convert to Celsius or F to convert to Fahrenheit!")
    }
}

func switchCase()
{
    var choice: String
    
    print("Enter F to convert to Fahrenheit or C to convert to Celsius:")
    choice = readLine(strippingNewline: true)!
    switch choice
    {
        case "C", "c":
            toCelsius()
        case "F", "f":
            toFahrenheit()
        default:
            print("You must enter C to convert to Celsius or F to convert to Fahrenheit!")
    }
}

func main()
{
    ifElse()
    switchCase()
}

main()

कोशिश करो[सम्पादन | स्रोत सम्पादित करें]

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें[सम्पादन | स्रोत सम्पादित करें]