1.02 Coding basics

Aim: To view some code and discuss a few basic components.

Topics covered:

  • What does code look like?
  • What is a statement?
  • What is an expression?
  • What are variables and how can they be used?

Code Examples

Code comes in a variety of different forms as you will see in the excerpts below:

Java

package uk.co.philkeene.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Example1 {
    public static void main(String... args) throws IOException {
		System.out.println("Please enter your name: ");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String name = reader.readLine();
		System.out.println("Hello " + name + ", how do you do?");
	}
}

PHP

<html>
    <head>
        <title>Demo1</title>
    </head>
    <body>
        <p>Please enter your name:</p>
        <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
            <input name="name" />
            <input type="submit" />
        </form>

<?php
        if ($_REQUEST['name']) {
?>
        <p>Hello <?=$_REQUEST['name'];?>, how do you do?</p>
<?php
        }
?>
    </body>
</html>

At this point in the course it is not expected for you to understand the code examples, they are given for illustration purposes only.

Fundamental coding elements

Code is generally made up of statements that tell the machine to do something. Statements can be categorised into assignments, sub-routines, functions and flow-related code (such as conditions and loops).

An expression is another key element. An expression is anything that has a result. It could be a variable, function, calculation or a combination of these. Expressions are either assigned to a variable (in a statement called an assignment), or used with functions.

A variable has a name, a type and a value (or a reference to a value). A variable is used to store the result of an expression for later use.

The type of the variable tells us what we can expect to be contained in the variable. In some languages (e.g. Java) the type is strict and the value cannot deviate from it. In other languages (e.g. conventional PHP) the type is not strict and could contain almost any value. We would call the first group strongly-typed languages.

The name of a variable is of vital importance. Well-named variables make reading code much easier and make the code more maintainable. Conversely, badly-named variables often lead to unexpected results, difficult to maintain code and ultimately lots of bugs.

Confusing mathematicians

The way that variables are used in code, they sometimes give the appearance of algebra. There are important differences, the following is valid in Java (excusing the semi-colon), but would appear to be a contradiction in algebra…

a = a + 1

In Java this means: –

  • retrieve the value of variable ‘a’
  • add 1 to the value
  • then place the result into the variable ‘a’

Undefined terms

In this article, we have discussed a number of features of coding that have not been defined. We will discuss sub-routines, functions, conditions and loops in upcoming articles.

 

Feedback on 1.02 Coding basics

If you have any questions or suggestions relating to this article or any other then please fill in our feedback form and let us know what you think.