TL ; DR:
The console is a panel that displays important messages, like errors, for developers.
In Swift, we use the
print()function to print something to the console. For example, this program:print(13)
prints
13to the console.
Full lesson:
The console is a panel that displays important messages, like errors, for developers.
Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print to our console directly.
print():
In Swift, we use the print() method to print something to the console. For example, this program:
print(13)
logs 13 to the console.
When you run this program, this is what you'll see in your console:
13
We refer to the data printed to the console as the output of the program.
Newline character:
You might have noticed the extra empty line after 13 in our program's output.
It's important to know that the print() function also prints an invisible newline character after the value we provide.
This newline character inserts a new line in the console. It's like pressing Enter in your text editor to move the cursor to the next line.
This means that if we use print() multiple times, the values will be printed on separate lines in the console:
print(1)
print(2)
print(3)
The ouput of this program would be:
1
2
3
Assignment
Follow the Coding Tutorial and let's print some values to the console!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.