Programming Concepts: A Brief Tutorial for New Programmers

Richard Holowczak


Topics


This tutorial introduces basic programming concepts such as program structure, variable declaration, conditional and looping constructs, and the code/compile/run style of programming. This tutorial is intended for use as an introduction to these concepts for students who have no prior programming experience.

Introduction

Computer programs are collections of instructions that tell a computer how to interact with the user, interact with the computer hardware and process data. The first programmable computers required the programmers to write explicit instructions to directly manipulate the hardware of the computer. This “machine language” was very tedious to write by hand since even simple tasks such as printing some output on the screen require 10 or 20 machine language commands. Machine language is often referred to as a “low level language” since the code directly manipulates the hardware of the computer.

By contrast, higher level languages such as “C”, C++, Pascal, Cobol, Fortran, ADA and Java are called “compiled languages”. In a compiled language, the programmer writes more general instructions and a compiler (a special piece of software) automatically translates these high level instructions into machine language. The machine language is then executed by the computer. A large portion of software in use today is programmed in this fashion.

We can contrast compiled programming languages with interpreted programming languages. In an interpreted programming language, the statements that the programmer writes are interpreted as the program is running. This means they are translated into machine language on the fly and then execute as the program is running. Some popular interpreted languages include Basic, Visual Basic, Perl, Python, and shell scripting languages such as those found in the UNIX, Linux and MacOS X environment.

We can make another comparison between two different models of programming. In structured programming, blocks of programming statements (code) are executed one after another. Control statements (described later on) change which blocks of code are executed next.

In object oriented programming, data are contained in objects and are accessed using special methods (blocks of code) specific to the type of object. There is no single “flow” of the program as objects can freely interact with one another by passing messages.

In this tutorial, we focus only on structured programming.

Program Structure

Virtually all structured programs share a similar overall pattern:

  • Statements to establish the start of the program
  • Variable declaration
  • Program statements (blocks of code)

The following is a simple example of a program written in several different programming languages. We call this the “Hello World” example since all the program does is print “Hello World” on the computer screen.

Language Example program
“C”
#include <stdio.h>
void main() {
    printf("Hello World");
}
C++
#include <iostream>
int main() {
    cout << "Hello World";
    return 0;
}
Pascal
program helloworld (output);
begin
    writeln('Hello World');
end. 
Oracle PL/SQL
CREATE OR REPLACE PROCEDURE helloworld AS
BEGIN
    DBMS_OUTPUT.PUT_LINE('Hello World');
END;
Java
class helloworld {
    public static void main (String args []) {
       System.out.println ("Hello World");
    }
}
Perl
#!/usr/local/bin/perl -w print "Hello World";
Basic
print "Hello World"

Note that the Perl and Basic languages are technically not compiled languages. These language statements are “interpreted” as the program is running.

As a side note, if you would like to try out programming in Java, follow this link

Variable Declaration

Variables are place holders for data a program might use or manipulate. Variables are given names so that we can assign values to them and refer to them later to read the values. Variables typically store values of a given type. Types generally include:

  • Integer – to store integer or “whole” numbers
  • Real – to store real or fractional numbers (also called float to indicate a floating point number)
  • Character – A single character such as a letter of the alphabet or punctuation.
  • String – A collection of characters

In order to use a variable within a program, the compiler needs to know in advance the type of data that will be stored in it. For this reason, we declare the variables at the start of the program. Variable declaration consists of giving a new name and a data type for the variable. This is normally done at the very start of the program.

In the following example programs, variables of different types are declared and used in the programs.

Language Example program
“C”
#include <stdio.h>
void main() {
   int age;
   float salary;
   char middle_initial;
   age = 21;
   salary = 29521.12;
   middle_initial = "K";
   printf("I am %d years old ", age);
   printf("I make %8.2f per year " salary);
   printf("My middle initial is %c ", middle_initial);
}
Pascal
program myexample (output);
   var age: integer;
   salary: real;
   middle_initial: char;
begin 
   age := 21;
   salary := 29521.12;
   middle_initial := 'K';
   writeln('I am ', age, ' years old');
   writeln('I make ', salary, ' per year');
   writeln('My middle initial is ', middle_initial);
end.
Oracle PL/SQL
CREATE OR REPLACE PROCEDURE myexample AS
    age NUMBER;
    salary NUMBER;
    middle_initial CHAR(1);
BEGIN
   age := 21;
   salary := 29521.12;
   middle_initial := 'K';
   DBMS_OUTPUT.PUT_LINE('I am ' || age || ' years old');
   DBMS_OUTPUT.PUT_LINE('I make ' || TO_CHAR(salary, '99999.99') || ' per year');
   DBMS_OUTPUT.PUT_LINE('My middle initial is ' || middle_initial);
END;
Java
class myexample {
   public static void main (String args []) {
      int age; 
      double salary;
      String middle_initial;
      age = 21;
      salary = 29521.12;
      middle_initial = "K";
      System.out.println ("I am " + age + " years old ");
      System.out.println ("I make " + salary + " per year");
      System.out.println ("My middle initial is " + middle_initial);
   }
}
Perl
#!/usr/local/bin/perl $
     age = 21; 
     $salary = 29521.12; 
     $middle_initial = "K"; 
     print "I am " . $age . " years old "; 
     print "I make " . $salary . " per year "; 
     print "My middle initial is " . $middle_initial;
Basic
Dim age AS Integer 
Dim salary AS Decimal 
Dim middle_initial As String 
age = 21 
salary = 29521.12 
middle_initial = "K" 
print "I am " & age & " years old " 
print "I make " & $salary & " per year " 
print "My middle initial is " & $middle_initial

In the above examples it is clear that different programming languages have slightly different syntax and data types. However for the most part, variable declaration is straight forward.

Real differences begin to appear when more complex data structures such as arrays and pointers are declared. Such discussion is best left for programming courses.

Tags: , , , ,

Comments are closed.