Basic Structure of Code

At first glance, programming code may look intimidating; however, it is just a bunch of instructions that are given to a computer. Writing code is similar to composing music or designing a house in many ways. Once you know the fundamentals you can create something basic or something that is amazing, either way, the creation process could be lots of fun and the end result will be gratifying, just like any art. In addition, you can make money while doing it, which is always a bonus. We will cover the programming basics; once you know the basics, you will be able to create your own programs.

CodeCompletion

 

Code Blocks

One of the first things you will notice when you look at code is that it there are many curly brackets, the brackets represent code blocks. Basically a block is any set of instructions that are grouped together. They are surrounded by curly brackets {}.

Ex.
code blocks
In our example, we can see that there are four blocks, each block contains instructions, and a block could contain another block. Block1 contains all the other blocks, and Block2 contains Block3.

Each time we add a block (set of instructions), we indent the instructions within that block, this will make our code look much cleaner and makes it much easier to read. Additionally you must make sure that all open brackets have a matching closing bracket.

Determine which block structure is wrong or right

musicPlayer {    
    pause{
        save the time position;
        stop playing;
    }
    play{
        go to last stopped time position;
        start playing;
    }
}

musicPlayer { 
    pause
        stop playing;
        save the time position; 
    }
    play{
        go to last stopped time position;
        start playing;
    }
}


Statements

Notes make music, building materials make a house, and statements make programs.

A statement is a command/instruction that is given to the computer. A semi-colon must follow all statements; this is how the program determines where each command ends.

statements_code

Keep in mind that not every line of code is a statement, only the lines that involve an action. If you forget to add a semi-colon to the end of a statement, your program will run into issues.


Comments

In programming, we can add notes or descriptions to our code. We call them comments. Comments allow us to add regular readable text to our code to explain what the code is supposed to do. This is very useful because it helps others understand your code. Code that does not contain comments can be difficult to understand. When a computer is running your code, it will skip all the comments.

There are two ways to create a comment.

  1. We can comment by simply placing two forward slashes ‘//’in front of the text we would like to comment.
  2. If the comment is more than 1 line long we can put it in between ‘/*’ & ’*/’

Ex.
comment_code

Comments have no effect on your program; they only make your code more readable.

How many comments are in this program

/*This music player can play/pause songs
Created by John Doe*/
musicPlayer { 
    //when pause is clicked
    pause{
        save the time position;
        stop playing;
    }
    //When play is clicked
    play{
        get the song;  //if paused, get the same song (not next)
        go to last stopped time position;
        start playing;
    }
}

 

First printing program

The programs we will be writing are Java programs. It is possible to write a java program using just a simple text editor. However, it is very easy to make mistakes using a normal text editor. So we will use an IDE to help us write our code. This will give us a better coding experience. An IDE (Integrated Development Engine) is an application that provides several basic tools that developer can use to write and test software quickly. There are several different types of IDEs available to you. I would recommend NetBeans, Eclipse or Blue J. NetBeans and Eclipse are more commonly used because they are packed with many features. However Blue J has a simple easy interface even though it lacks many features. If you don’t want to download the IDE and you prefer using an online IDE, I suggest Codiva. The choice is yours.

Netbeans :          https://netbeans.org/downloads (select Java SE)

Eclipse :                https://eclipse.org/downloads/

Blue J:                   http://www.bluej.org/versions.html

Codiva:                 https://codiva.io/

Once you have decided on an IDE we can test our first program. Create a new Java project and name the file “Test”. Make sure your java file is blank, and then paste the following code in your java file.

public class Test {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

Make sure that the bolded text matches your file name. If not change it to match your file name.
Once everything looks right, click Run (for some IDEs you will have to click compile/build first).
The objective of this program is to print Hello World! To get a different output change the text in the quotes then compile/build & run again.

Back to Course Menu
What do you think about this topic? Share your suggestions / comments