ProgramGuru

Hello World and console output

Hello World and console output

A first program should have a result you can recognize immediately. Swift’s print function writes values to the console. A string is text enclosed in double quotes, and the parentheses supply the value to the function. This existing lesson keeps its URL while expanding the explanation and practice.

For a simple script, statements can appear at the top level and run in order. print normally finishes with a newline. Swift is case-sensitive, so Print is not the same name. Ordinary statements on separate lines do not require semicolons. Comments such as // greeting explain code without being executed.

What you’ll learn

  • Trace two print statements in source order.
  • Explain quotes, newlines, comments and the optional Foundation import.

Hello World and console output in the example

  1. First statement

    A first program should have a result you can recognize immediately.

    First statement
  2. Second statement

    For a simple script, statements can appear at the top level and run in order.

    Second statement
  3. Two output lines

    The import makes Foundation APIs available, although print itself comes from the standard library and does not need Foundation here.

    Two output lines
The import makes Foundation APIs available, although print itself comes from the standard library and does not need Foundation here. The first print produces the original greeting. The second produces a separate line. Both execute once, in their written order.

Work through hello world and console output

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

import Foundation

print("Hello, World!")
print("Welcome to River School")

Expected output

Hello, World!
Welcome to River School

Follow the execution

The import makes Foundation APIs available, although print itself comes from the standard library and does not need Foundation here. The first print produces the original greeting. The second produces a separate line. Both execute once, in their written order.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Hello World and console output

Your task: How would you print only Welcome to River School?

Swift · complete program

import Foundation

print("Hello, World!")
print("Welcome to River School")

Worked answer

Remove the first print statement and keep the second. Removing import Foundation also works for this example because neither line needs its APIs.

Console output and source spelling

Console output is not an application screen. Do not include the shell prompt when copying a terminal command. The original output_1 bookmark is retained as a destination in the worked example.

Check your decision

How would you print only Welcome to River School?
Choose one answer. You can try again.

Answer and explanation

Remove the first print statement and keep the second.

Remove the first print statement and keep the second. Removing import Foundation also works for this example because neither line needs its APIs.

Hello World and console output in practice

Remove the first print statement and keep the second. Removing import Foundation also works for this example because neither line needs its APIs. Console output is not an application screen.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variables and constants →