Introduction
Input and output are terms used to describe the interaction between a computer program and its user. The user provides input to the program, whereas the programme provides output to the user.
Let's understand with the help of an example. Let's say we're stuck in the lawyer's office waiting room and get hungry. Oh, there's a vending machine! We've been rescued! We may not be as enthusiastic, but let's get something to eat. We'll put $2 into the vending machine and get a bag of chips. This is a very straightforward and simple form of input and output. Put something into a machine, and it produces something else.
In a similar manner, in this blog, we will go through how to read the user's standard input in swift programming language and display the output to the screen in various ways.
Swift Output
To begin, open XCode and create a new command-line application project.
To send output to standard output, simply use:
print( :separator:terminator:) function (screen)
Three parameters are recognised by the function print( :separator:terminator:).
• items: Things to print over a console. It can recognise many items.
• separator: a line that will be printed between each item. A single space (" ") is the default.
• terminator: After all items have been printed, print this string. A newline ("n") is the default.
Because the last two parameters (separator and terminator) have default values, they aren't required to be used when invoking the print function.
Example 1: Using the print() function to print to the screen
print("Hello, World!")
print(“I love Swift.”)
Output:
Hello, World!
I love Swift.
Print ("Hello, World!") in the above program prints the string literal Hello, World! To the terminal.
Note: Because the print strategy's parameter terminator has a default value of n, it also alters the line (adds a line break) when printing "I love Swift" (newline).
The sentence print("I love Swift.") is similar in that it publishes the message in a new line.
Example 2: Printing constants, variables and literals
var helloMsg = "Hello, World!"
print(helloMsg)
print(123.45)
Output:
Hello, World!
123.45
You can print the value of a variable or constant by simply typing the name of the variable or constant in the print function. print(helloMsg) returns the value Hello, World! From the variable helloMsg in the preceding program.
In the print statement, you can also use it literally. The print(123.45) sentence produces a floating-point literal 123.45 without the twofold quotation.
Example 3: Printing without a link break using the terminator parameter
If you need to print without a line break, use the terminator parameter of the print function to pass an empty string, such as
print("Hello, World!", terminator: "")
print("I love Swift.")
print("I also love Computer Programming.")
Output:
Hello, World! I love Swift.
I also love Computer Programming.
The terminator in the above program is the string printed after all of the elements have been printed. The terminator has been set to a blank string (default is a newline \n). The first sentence prints without a new line and the second statement print("I love Swift.") displays the message in a similar line.
The sentence print("I also love Computer Programming") outputs in a new line since the print("I adore Swift.") function adds a line break.
Example 4: Printing multiple items using a single print function
You may also print multiple elements in a single print statement and use a separator like this:
print("Hello, World!", 2021, "See you soon", separator: ". ")
Output:
Hello, World!. 2021. See you soon.
In the print statement of the above program, we separated different data types using a comma. We've included the separator parameter with the value ".". The separator (dot.) is inserted between each item. As you can see, the output is separated by a space and a character.
Example 5: Printing multiple lines
If you need to print numerous lines with a single print statement, you can utilise the carriage return escape sequence in the print statement as follows:
print("Hello, \rWorld!")
Output:
Hello,
World!
Example 6: Printing variables using string interpolation
You can also use string interpolation to add the value of a variable or be consistent with the string literal, such as wrapping a variable in a set of parentheses and prefixing it with a backslash (\).
Var helloMsg = "Hello, World!"
print("I have a message \(helloMsg)")
Output:
I have a message Hello, World!
The statement print("I have a message (helloMsg)") inserts the value of variable helloMsg into a string literal wrapped as (helloMsg). As a result, the statement prints out Hello, World! On the screen.