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 and run your first program
  • 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

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

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

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

Programming languages

In this unit, you will learn how to create a program completely using programming code. To do this, you have a choice of THREE (3) programming languages. Take a look at each one:

Swift is a free and open source language that is mainly used for Apple development such as for writing Mac, iPhone, and iPad apps.

Swift is the recommended choice at Onslow College because it’s designed to prevent you from making a range of very common mistakes that other languages won’t warn you about. This can make writing more complex code, particularly at Level 2 and Level 3, easier to debug. This will help you prevent crashes.

On the other hand, it can’t (currently) be installed on the Windows computers located in Room 8 and Room 10. However, you can install it on your Windows laptop, Linux laptop, MacBook (using Xcode), or iPad (using Swift Playgrounds).

TypeScript is a programming language based on JavaScript. Although JavaScript is mostly used for developing websites, TypeScript can be used for websites, desktop applications, and others.

It has some of the benefits of Swift, such as making errors easier to detect, whilst also making it possible to tap into the world of web development. TypeScript code can be run as a script, like JavaScript is in browsers, or compiled into a computer program that runs even faster.

Python is a popular programming language for all sorts of use cases: scripts, games, data crunching, and is commonly taught in schools around Aotearoa.

Many schools teach Python and it’s a fairly common language amongst beginners, seasoned professionals, and data scientists. At Onslow College, we used to teach it but found that it doesn’t protect you from a lot of common errors, meaning you have to write a lot more code to ensure that your programs didn’t crash.

At the start of the unit, you will learn how to use all three languages. Later in the course, you will decide which one you prefer and stick to that.

But I already know a different language…

Don’t panic!

If you have already learnt another language, don’t panic. In the software development industry, nobody uses just one programming language. Learning more than one programming language isn’t hard; in fact, already knowing another one gives you a head start.

These languages should look familiar

Swift, TypeScript, and Python are called “C-like languages”, meaning that much of their syntax is similar to C. It’s more than likely that you already know a C-like language, such as C#, C++, Lua, Ruby, Rust, etc.

If you know any of those, you will find more similarities than differences!

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

What did we just do? (print())

  1. We used the print function, which is used to display information in the Terminal window
  2. The text that gets printed is called a String.
  3. This is how Swift can tell the difference between the word print used for the function and the words “Hello, world!” to be printed
  4. Strings are always wrapped with double quotation marks
print("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.

Let’s write our first Swift code (III)

  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("Next Step High School")

What did we just do?

  1. We wrote a comment to describe what our code is doing.
  2. Our code is very simple, so there wasn’t much to explain
  3. However, your later code will become much more complex, so keep a hold of this skill.
  4. Remember to use double slashes for comments.
// This is a comment.

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