SURVEY OF PRORAMMING LANGUAGES
Table of Contents
Unit 1: Introduction
Unit 2: Program Structure, Comment, Variables and Datatype
Unit 3: Programs with Lab Examples
UNIT 1
INTRODUCTION
1.1 INTRODUCTION:
The goal of this
tutorial under Survey of programming languages is to teach you the practical
general overview, and fundamental concept of programming languages and
comparing syntactic (syntax) and semantic concepts underlying programming
languages like: Pascal, Java and C++. We will be implementing programs using at
least three different programming languages. their syntax: bindings and scope,
data types and type checking, functional scheme, expression of assignments and
statements, control structure (selection statement and loop statement) etc. This
will harness and reinforce the knowledge of students on how to be familiar with
the programming language differences.
There are two
ways to write programs, console application (it focuses on texts only) and
Graphical User Interface application (it focuses on using controls like label,
textbox, button, separator, panel, frame etc.). However, this tutorial will
focus on the use of console application.
1.2 Identifier, Binding
and Scope
Identifier: Identifier is a name that allows programmer to refer to
variables, constants, functions, types, operations etc. in a program.
Binding: Binding association of an identifier with an object (i.e. assigning
datatype to an identifier). There is static and dynamic binding. Static binding uses
Type (datatype) information for binding while Dynamic binding uses Objects to
resolve binding. In addition, if linking between method call and method
implementation is resolved at compile time then we call it static binding or If
it is resolved at run time then it dynamic binding.
Scope: The lifetime of a binding of a name to an object. There
is local and global variable. A local variable is declared inside a function
and can only be accessed within the function in which it is declared while A
global variable is declared outside of a function and can be accessed anywhere
in the program.
UNIT 2
PROGRAM STRUCTURE,
COMMENT, VARIABLE AND DATATYPE
2.1 Program Structure
Program
structure gives the overview of a program for a particular programming language, and also with emphasis on the individual components of the program and the
interrelationships between these components.
Pascal
program filename; var //variable
declaration begin //statements
to be executed end. |
Java
public class filename
{ public static void main(String[] args) { //statements to be executed } } |
C++
#include
<iostream> using
namespace std; int main() { //statements to be executed return 0; } |
2.2 Comment
Comment is used to add additional information to a program. It helps to indicate what needs to be done either instantly or after. It helps to explain the function of a particular line or lines of code.
2.2.1 Single line
commenting
Single line commenting |
// Pascal
program to display your name |
//Java Program
to display your name |
// C++ Program
to display your name |
2.2.2 Multiple line
commenting (it allows blocks of comment in multiline)
{ Author: Dmac
Tutor Written Date:
Mar 30, 2023 Topic: Pascal
program to display your name } |
/* Author: Dmac
Tutor Written Date:
Mar 30, 2023 Topic: Java
program to display your name */ |
Author: Dmac
Tutor Written Date:
Mar 30, 2023 Topic: C++
program to display your name */ |
2.3 Variable
Variable is used
to store values in a program. When you create variable, it resides in the memory
of the computer. Variable is a value that can change.
2.3.1 Rules for naming
identifier
-
Give
meaning names (your identifier must tally with what your program is all about)
-
Start
with letter (a-z, or A-Z), number (0-9) or underscore
-
Identifier
cannot start with a number (0-9)
-
Keywords
or reserved words cannot be used to create identifier
2.4 Datatype
A data type
specifies which type of value a variable has, what type of value can be stored
in it considering the type of mathematical, relational or logical operations
that can be applied to the variable without causing an error. Two categories of
datatypes are primitive and non-primitive. We will focus on primitive.
Pascal
string –
stores combination of letters, numbers and special character char – stores single
character like letter, number and special character integer,
longint – stores whole number real – stores decimal
numbers boolean – stores
true or false value |
Java
String –
stores combination of letters, numbers and special character (TIP: Java String
starts with capital ‘S’) char – stores single
character like letter, number and special character int, long –
stores whole number float, double –
stores decimal numbers boolean – stores
true or false value |
C++
string –
stores combination of letters, numbers and special character char – stores single
character like letter, number and special character int – stores whole
number float, double –
stores decimal numbers bool – stores true
or false value |
2.5 Variable declaration
Variable declaration
is a statement used to specify the variable name and its data type.
Pascal
Syntax variableName :
datatype; //single
variable firstname :
string; age : integer //multiple
variables firstname,
surname, address : string; age, level :
integer |
Java
Syntax datatype variableName; //single
variable String firstname; int age; //multiple
variables String firstname,
surname, address; int age, level; |
C++
Syntax datatype variableName; //single
variable string firstname; int age; //multiple
variables String firstname,
surname, address; int age, level; |
2.6 Variable Initialization
Initializing a
variable means to specify an initial value to assign to it. A variable that is
not initialized is stored with a default value. int is stores 0, float stores
0.0, string stores null, char stores ‘\u0000’, boolean stores false. Two types of variable initialization are explicit and implicit. Explicit initialization is when you assign a value in a program in the source editor before compiling the program, while implicit initialization is when you supply value after the program compiles.
Pascal
Syntax The assignment
operator of Pascal is colon and equal to. It assigns value to the variable
for storage. variableName :=
value; //single
variable firstname := “dmactutor”; age := 20; |
Java
Syntax The assignment
operator of Java is equal to. variableName =
value; firstname = “dmactutor”; age = 20; |
C++
Syntax The assignment
operator of C++ is equal to. variableName =
value; firstname = “dmactutor”; age = 20; |
2.7 Variable Declaration
and Initialization
If you don’t want
to first declare a variable then initialize it, you can also make a 2-in-1
statement, by declaring and initializing a variable at the same time.
Pascal
Syntax The colon will
not be present, you use only equal to in Pascal to perform 2-in-1. variableName :
datatype = value; //single
variable firstname :
string = “dmactutor”; age : integer =
20; TIP: Pascal allows multiple variables to be declared, but I
have not gotten the way for multiple variables to be declared and initialized
at the same time. //multiple
variables firstname,
surname : string; |
Java
Syntax datatype variableName
= value; String firstname
= “dmactutor”; int age = 20; //multiple int age = 20,
level = 4; |
C++
Syntax datatype variableName
= value; String firstname
= “dmactutor”; int age = 20; //multiple int age = 20,
level = 4; |
2.8 Constant Variable
Declaration and Initialization
If you don’t want
a value to change, you can declare it as constant, or if you want to use a value
repeatedly in program, for example pi = 3.14, the value is constant.
Pascal
Syntax This works with
or without using the var keyword. Use the const keyword. const variableName
= value; const age =
20; |
Java
Syntax Use the const
keyword. const datatype
variableName = value; const int pi =
3.14; |
C++
Syntax Use the const keyword. datatype variableName = value; String firstname = “dmactutor”; int age = 20; |
UNIT 3
CODE
IMPLEMENTATION USING PJC (Pascal, Java, C++)
3.1 PROGRAMS WITH LAB EXAMPLES
TIP: At some
point, you might have issue running C++ program using internet enabled compiler,
I will recommend you run your code on Dev C++, to be on a safer side and to avoid
giving yourself headache to debug error.
3.1.1 Lab 1 – program to display name and age
Pascal
program lab1; begin writeln('dmac tutor'); writeln(18); end. |
TIP: In the program
above, surround the characters with single quotes, if your display string. If
you use double quotes, it will generate error. To display a number, you don’t
need put it in quote.
Java
class lab1 { public static void main(String[] args) { System.out.println("dmactutor \n"); System.out.println(18); } } |
TIP: In the
program above, we use escape sequence \n so that 18 will appear on a new line.
C++
#include
<iostream> using
namespace std; int main() { cout << "dmactutor \n"; cout << 18; return 0; } |
3.1.2 Lab 2 – program to declare and initialize
name and age
Pascal
program lab2; var //declare
variables fullname : string; age : integer; begin //initialize
variables fullname := 'dmactutor'; age := 18; //print
content of variables writeln(fullname); writeln(age); //to make it more descriptive writeln('Your name is:: ', fullname); end. |
TIP: In the
above program, don’t wrap variables in quote
Java
class lab2 { public static void main(String[] args) { String fullname; int age; fullname = "dmactutor"; age = 18; System.out.println(fullname); System.out.println(age); //to make it more descriptive System.out.println("Your name
is::" + fullname); } } |
C++
#include
<iostream> using
namespace std;
int main() { string fullname; int age; fullname = "dmactutor"; age = 18; cout << fullname <<endl; //endl creates a new line like: cout
<< fullname <<"\n"; cout << age <<endl; //to make it more descriptive cout << "Your name is:: "
<< fullname; return 0; } |
3.1.3 Lab 3 – program to declare and accept input
for variables name and age
Pascal
program lab3; var //declare
variables fullname : string; age : integer; begin //display
action for the user and accept input writeln('Please enter your full name'); readln(fullname); writeln('Please enter your age'); readln(age); //print
content of variables writeln(fullname); writeln(age); end. |
Java
import
java.util.*; class lab3 { public static void main(String[] args) { //create scanner object sc (you can
use any variable of your choice) Scanner sc = new Scanner(System. in);
//display action for the user and
accept input System. out. println("Please
enter your full name:: "); String fullname = sc.nextLine(); System. out. println("'Please
enter your age:: "); int age = sc.nextInt(); System.out.println(fullname); System.out.println(age); } } |
TIP: You have to
import the libarary that contains Scanner, using java.util.*; helps you to
access many libraries at the same time. Then we need to create a scanner object
‘sc’ which is associated with our variables: fullname and age, along with their
scanner method. String uses nextLine(), int uses nextInt()
C++
#include
<iostream> using
namespace std;
int main() { string fullname; int age; cout<<"Please enter your full
name:: "; cin >> fullname; cout<<"Please enter your age::
"; cin >> age; cout << fullname <<endl; cout << age <<endl; return 0; } |
3.1.4 Lab 4 – program to add two numbers (arithmetic
operation)
Pascal
program lab4; var //declare
variables fnum, snum, result : integer; begin //display
action for the user and accept input writeln('Enter first value'); readln(fnum); writeln('Enter second value'); readln(snum); //processing result := fnum + snum; //print
content of variables writeln('The answer is:: ', result); end. |
Java
import
java.util.*; public class lab4
{ public static void main(String[] args) { //create scanner object sc (you can
use any variable of your choice) Scanner sc = new Scanner(System. in); System. out. println("Enter
first value:: "); int fnum = sc.nextInt(); System. out. println("Enter
second value:: "); int snum = sc.nextInt(); int result = fnum + snum; System.out.println("The answer
is:: " + result); } } |
C++
#include
<iostream> using
namespace std;
int main() { int fnum, snum, result; cout<<"Enter first value::
"; cin >> fnum; cout<<"Enter second value::
"; cin
>> snum; result = fnum + snum; cout << "The answer is::
" <<result; return 0; } |
TIP: If you
encounter ths error: id returned 1 exit status. The possible causes of this
error are: A syntax error in the C++ program, A mismatch between the compiler
and the library, An incorrect link to a library, An incorrect or missing header
file. Solution, open another file, type your codes then compile and run again.
3.1.5 Lab 5 – program to compute mathematical formulas
getting decimals. Program for bmi (body max index).
The appropriate
datatype for pascal: real, Java: double, C++: float) and formatting it to two
decimal places. Test it with integer for pascal and int for Java and C++ to see
what will happen.
Pascal
program lab5; var //declare
variables weight, height, resultBmi : real; begin //display
action for the user and accept input writeln('Enter weight value in KG:: '); readln(weight); writeln('Enter height value in meters from
(0-2):: '); readln(height); //processing resultBmi := weight / (height * height); //before
formatting writeln('Your body mass index is:: ',
resultBmi); //after
formatting writeln('Your body mass index is:: ',
resultBmi:0:2); end. |
TIP: 0:2, the
zero indicate the number of digit spaces to be reserved for the whole number
part (replace the 0 with 1,2,3,4 etc to see the output), while two indicate
that the number should be approximated to two decimal places. Pascal don’t allow
** or ^ as power operator.
Java
import
java.util.*; public class lab5
{ public static void main(String[] args) { //create scanner object sc (you can
use any variable of your choice) Scanner sc = new Scanner(System. in); System. out. println("Enter
weight value in KG:: "); double weight = sc.nextDouble(); System. out. println("Enter
height value in meters from (0-2):: "); double height = sc.nextDouble(); double resultBmi = weight / (height *
height); //before formatting System.out.println("Your body
mass index is:: " + resultBmi); //after formatting System.out.println("Your body
mass index is:: " + String.format("%.2f", resultBmi)); } } |
TIP: To round up
the number to two decimal places, we use format() method in java. Java allows
** as power operator.
C++
#include <iostream> using
namespace std;
int main() { float weight, height, resultBmi; cout<<"Enter weight value::
"; cin >> weight; cout<<"Enter height value::
"; cin >> height; resultBmi = weight / (height * height); //before formatting cout << "Your body mass index
is:: " <<resultBmi <<endl;
//after formatting cout << "Your body mass index
is:: "; printf("%.2f",
resultBmi); return 0; } |
3.1.6 Lab 6 – program to compute mathematical formulas
using inbuilt function. Program for Pythagoras theorem to find the hypothenus.
We will use Sqrt()
built-in function. The Sqrt() function returns the square root of its argument.
Pascal
program lab6; var //declare
variables opp, adj, hyp : real; begin //display
action for the user and accept input writeln('Enter opposite value'); readln(opp); writeln('Enter adjacent value'); readln(adj); //processing hyp := Sqrt((opp*opp) + (adj*adj));
writeln('Hypothenuse result is:: ',
hyp:5:2); end. |
TIP: your Sqrt()
in pascal must start with capital “S”
Java
import
java.util.*; import
java.lang.*; public class lab6
{ public static void main(String[] args) { //create scanner object sc (you can use any
variable of your choice) Scanner sc = new Scanner(System. in); System. out. println("Enter
opposite value:: "); double opp = sc.nextDouble(); System. out. println("Enter
adjacent value:: "); double adj = sc.nextDouble(); double hyp = Math.sqrt((opp*opp) +
(adj*adj)); System.out.println("Hypothenuse
result is:: " + String.format("%.2f", hyp)); } } |
TIP: To use the Math
sqrt() function in Java, you must import java.lang.*; and you must associate
your variables with Double. Any other datatype with the math inbuilt function
will generate error.
C++
#include
<iostream> #include
<cmath> using
namespace std;
int main() { float opp, adj, hyp; cout<<"Enter opposite value::
"; cin >> opp; cout<<"Enter adjacent value::
"; cin >> adj; hyp = sqrt((opp*opp) + (adj*adj)); cout << "Hypothenuse result
is:: "; printf("%.2f", hyp); return 0; } |
TIP: To use the sqrt()
function in C++, you must call #include <cmath>
Comments
Post a Comment