Matua Doc

Matua Doc

Introduction to programming

Learning intentions

You will learn:

  • What programming means
  • How learning programming relates to software engineering
  • How to write your first code using the Swift programming language
  • How to use the print function to make text appear on-screen
  • How to use comments to explain what your code is doing
  • You can explain what programming is in your own words (a sentence)

Your program:

  • Shows text on the screen using the print function
  • Contains comments that explain what each line is doing

Introduction to programming

Programming is a fundamental part of software engineering, which is a field of engineering relating to designing, writing, and testing software

As part of this course, you will take your first steps to becoming a software engineer!

Before we start learning about programming, let’s get familiar with the standard against which you will be assessed

AS92004: Create a computer program

Students are able to create a computer program

Paetae/A: Create a computer program

  • using a suitable programming language to construct a program that performs a specified task
  • testing and debugging the programming to ensure it works on expected cases
  • documenting the program with comments

AS92004: Create a computer program

Kaiaka/M: Create a well-structured computer program

  • using succinct and descriptive variable names
  • documenting the program with comments that clarify the purpose of code sections
  • testing and debugging the program to ensure it works on expected and boundary cases

AS92004: Create a computer program

Kairangi/E: Create a flexible and robust computer program

  • using conditions and control structures effectively
  • using constants, variables, or derived values in place of literals to make the program flexible
  • testing and debugging the program to ensure it works on expected, boundary, and invalid cases

What is programming?

Programming is the process of giving instructions to a computer so that it can perform tasks

When we perform programming, we are writing down the steps we want the computer to take

Therefore, we need to start from the most basic steps, such as putting text on the screen, before we can create a fully-fledged program

About the Swift programming language

In this unit, you will learn how to create a program completely using programming code. To do this, we will learn the Swift programming language

Swift is a powerful yet beginner-friendly programming language that can be used in a wide range of applications

Swift is the main programming language used to develop modern Mac, iPhone, iPad, Apple TV, and Apple Watch apps. However, it is also available for Windows and Linux

Why do we learn Swift?

Unlike Python, which we used to use, Swift prevents some common programming pitfalls that can lead to a Not Achieved grade

It can suggest when problems need fixing and can even refuse to run your code if there is a serious issue, meaning you can detect issues and test your program more easily

If you take Digital Media and Design, you can use Swift on a Mac or iPad to develop Mac, iPad, and iPhone applications

Learning Swift will also help you learn other, similar languages such as Java, Kotlin, C#, and Rust

But I already know another language…

If you have already learnt another language, such as Python or JavaScript, don’t panic. In the software development industry, nobody uses just one programming language. In fact, in a single project, you may use around two to five different languages!

Swift is a C-like language, meaning that much of its syntax is similar to C. Python, JavaScript, Java, etc. are also C-like languages. If you know any of those, you will find more similarities than differences!

If you would like a quick run-down of the Swift language syntax to help you feel more comfortable, take a look at Codecademy’s Swift documentation which breaks the syntax down into sections

Otherwise, let’s get started!

Let’s write our first Swift code! (I)

Get ready to run your first Swift code

Let’s get ready to write your first program in Swift

  1. Go back to Google Classroom and open the Swift Playground assessment
  2. If you haven’t signed up for a school GitHub account yet, you will need to do so before step 3
  3. Accept the GitHub Classroom assignment
  4. Click on the blue URL
  5. Save a bookmark to this page, your GitHub Repository
  6. Click on the green Code button
  7. Click on the Codespaces tab
  8. Click on “Create Codespace on main”, then wait until the GitHub Codespace has finished loading
  9. When you see this on the rightbelow, you are ready

GitHub Codespace ready

Let’s write our first Swift code (II)

print("Hello, world!")

Let’s write our first program. The first code we will learn is how to show text on the screen. This is important because if the program cannot show text, we won’t know if it is doing anything

  1. In the left-side panel, click on the Sources folder then SwiftPlayground.swift
  2. To show text on the screen, call the print() function
  3. Between the ( brackets ), you write the message you want to show between double quotation marks
  4. At the top-right of the screen, click on the Run button

The print function

So, what did we just do?

  • We used the print function, which is used to display information in the Terminal window
  • The text that gets printed is called a String.
  • This is how Swift can tell the difference between the word print used for the function and the words “Hello, world!” to be printed
  • Strings are always wrapped with double quotation marks
print("Your message here.")
Your message here.

Comments

Our code can rapidly become confusing, even after writing just a few lines. To help with this, it’s often beneficial to include notes or reminders for ourselves or other developers.

(When they start marking your work, your kaiako especially will appreciate this!)

Comments clarify the purpose of the code However, they are not run as part of the program.

Practice writing comments

  1. In SwiftPlayground.swift, add a new line above your print() call
  2. On line 1, write a comment explaining what you think the print() code does

Comments consist of

- two slashes, 
- a space, 
- and your comment, written as a proper sentence.
// Comment goes here
print("Onslow College")

So, what did we just do?

We wrote a comment to describe what our code is doing.

  • Our code is very simple, so there wasn’t much to explain — however, your later code will become much more complex, so keep a hold of this skill.

Committing and pushing code

Before moving on, save your progress to GitHub so your work is backed up and your kaiako can see what you have completed.

  • A commit is a saved checkpoint of your work at one specific moment.
  • To push a commit sends your saved work from your Codespace to the GitHub repository so your latest version is online.
  1. Open Source Control in the left sidebar (the branch icon).
  2. In the message box, write a short commit message, for example Add print and comments.
  3. Click Commit or Commit & Push.
  4. If needed, click Sync Changes to push your commit to GitHub.

Creating a new branch

A branch is a separate line of work where you can build new code without affecting code you’ve already finished writing.

  • Use branches to keep your work organised for each lesson, task, or feature.
  • This also makes it easier to review your progress and merge work later.
  1. First, you must commit and push any code before you create a new branch.
  2. Open the branch menu in the bottom-left corner of Codespaces.
  3. Choose Create new branch.
  4. Give the new branch a meaningful name. It should be written in lowercase with dashes - instead of spaces..
  5. Press Enter and confirm you are now on 11dit-intro.

Task

In this task, you will write a program that displays the initials of your name in block letters ::: {.task} 1. If you haven’t done it already, create a new branch called 11dit-intro 2. Take a look at the complete alphabet and find your initials 3. For example: Whaea Tatiana’s initials are T and V (shown right) 4. Next, write a comment with a fun fact about yourself

Hints:

  • You will need to print SEVEN (7) characters across for each line of text to output your first initial as a block letter. Use spaces for the empty areas
  • You can print an empty line by writing print() without a string in it
  • Repeat the previous step for the next letter(s)
  • Add comments before printing the first letter and before printing the second, explaining what your code will do :::
T T T T T
    T
    T
    T
    T

V       V
V       V
V       V
  V   V
    V

Reflection

Congratulations! You have written your first Swift program! 🙌

In this lesson, you have learned:

  • the print() function is used to show text in the Terminal
  • single line comments are created using //

You can explain what programming is in your own words (a sentence)

Your program:

  • Shows text on the screen using the print function
  • Contains comments that explain what each line is doing