CSC 4405 Survey of Programming Languages Lecture 5-1
CSC 4405 Survey of Programming Languages Lecture 5-1
CSC 4405 Survey of Programming Languages Lecture 5-1
FACULTY OF SCIENCE
DEPARTEMNT OF COMPUTER SCIENCE
PART FIVE
CSC4405: EVOLUTION OF SELECTED PROGRAMMING LANGUAGES
SURVEY OF PLs
The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively
recent addition to C++.
The next line '// main() is where program execution begins.' is a single-line comment available in C++.
Single-line comments begin with // and stop at the end of the line.
The line int main() is the main function where program execution begins.
The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the screen.
The next line return 0; terminates main( )function and causes it to return the value 0 to the calling
process.
Semicolons and Blocks in C++: In C++, the semicolon is a statement terminator. That is, each
individual statement must be ended with a semicolon. It indicates the end of one logical entity. For
example, following are three different statements:
A block is a set of logically connected statements that are surrounded by opening and closing braces.
For example:
C++ does not recognize the end of the line as a terminator. For this reason, it does not matter where
you put a statement in a line. For example −
x = y;
2
y = y + 1; add(x, y);
C++ Identifiers: A valid identifier is a sequence of one or more letters, digits or underscore characters
(_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits
and single underscore characters are valid. In addition, variable identifiers always have to begin with a
letter. They can also begin with an underline character (_ ), but in some cases these may be reserved
for compiler specific keywords or external identifiers, as well as identifiers containing two successive
underscore characters anywhere. In no case they can begin with a digit. Another rule that you have to
consider when inventing your own identifiers is that they cannot match any keyword of the C++
language nor your compiler's specific ones, which are reserved keywords. The standard reserved
keywords are:
asm, auto, bool, break, case, catch, char, class, const, const_cast,
continue, default, delete, do, double, dynamic_cast, else, enum, explicit,
export, extern, false, float, for, friend, goto, if, inline, int, long,
mutable, namespace, new, operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static, static_cast,
struct, switch, template, this, throw, true, try, typedef, typeid, typename,
union, unsigned, using, virtual, void, volatile, wchar_t,
Very important: The C++ language is a "case sensitive" language. That means that an identifier written
in capital letters is not equivalent to another one with the same name but written in small letters. Thus,
for example, the RESULT variable is not the same as the result variable or the Result variable. These
are three different variable identifiers.
Fundamental data types: When programming, we store the variables in our computer's memory, but
the computer has to know what kind of data we want to store in them, since it is not going to occupy
the same amount of memory to store a simple number than to store a single letter or a large number,
and they are not going to be interpreted the same way. A summary of the basic fundamental data types
in C++, as well as the range of values that can be represented with each one:
3
5.1.2 Features of C/C++
Simplicity: It is a simple programming language in the sense of a structured approach that is the
program can be broken down into parts and can be resolved modularly. This helps the programmer to
recognize and understand the problem easily.
High-Level Language: C++ is a high level language, making it easier to understand as it is closely
associated with the human-comprehensible language that is English. It also supports the feature of
low-level language that is used to develop system applications such as kernel, driver, etc. That is why
it is also known as mid-level language as C++ has the ability to do both low-level & high-level
programming.
Multi-paradigm programming language: C++ is a language that supports object- oriented, procedural
and generic programming. It supports at least 7 different styles of programming. This makes it very
versatile.
Case Sensitive: Similar to C, C++ also treats the lowercase and uppercase characters in a different
manner. For example, the keyword “cout” (for printing) changes if we write it as “Cout” or “COUT”. But
this problem does not occur in other languages such as HTML and MySQL.
Compiler-based: C++ is a compiler-based programming language, unlike Java or Python which are
interpreter-based. This feature makes C++ comparatively faster than Java or Python language.
4
Existence of Libraries: The C++ programming language offers a library full of in-built functions that
make things easy for the programmer. These functions can be accessed by including suitable header
files.
Dynamic memory allocation: Since C++ supports the use of pointers, it allows us to allocate memory
dynamically. Constructors and destructors can even be used while working with classes and objects in
C++.
Syntax based language: C++ is a strongly tight syntax based programming language. If any language,
follow rules and regulation very strictly known as strongly tight syntax based language. Example C, C++,
Java, .net etc. If any language not follow rules and regulation very strictly known as loosely tight syntax
based language. Example HTML.
Java has features of inheritance, strong type checking, modularity, exceptional handling,
polymorphism, dynamic loading of libraries, arrays, strings handling, concurrency etc. Fundamental
component of Java is class, Java is quite verbose and one need to understand OOP to make the most
effective use of Java. Java seems restructure since it does not use pointers and does not provide the
raw power of C++.
Java Identifiers: All Java components require names. Names used for classes, variables, and methods
are called identifiers. In Java, there are several points to remember about identifiers. *All identifiers
should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
After the first character, identifiers can have any combination of characters.
A key word cannot be used as an identifier.
5
Most importantly, identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, _1_value.
Examples of illegal identifiers: 123abc, -salary.
Java Modifiers: Like other languages, it is possible to modify classes, methods, etc., by using
modifiers. There are two categories of modifiers –
Java Arrays: Arrays are objects that store multiple variables of the same type. However, an array
itself is an object on the heap.
Literals: These are the identifiers which have a particular value in itself. These can be assigned to
variables. Literals can also be thought of as constants. These are of different types such as numeric,
characters, strings etc.
b. Floating point Literals: We can specify the numeric values only with the use of a decimal point(.).
These numbers represent fractional numbers which cannot be expressed as whole integers. For
Example: 10.876
c. Character Literals: These are the literals which deal with characters i.e inputs which are not
numeric in type.
Single quoted character: This encloses all the uni-length characters enclosed within single quotes.
Example- ‗’a‘, ‘j‘.
Escape Sequences: These are the characters preceded by a backslash which perform a specific
function when printed on screen such as a tab, creating a new line, etc. Example- ‘\n‘
Unicode Representation: It can be represented by specifying the concerned unicode value of the
character after ‗\u‘. Example- ―\u0054‘
Comments in Java: Comments are needed whenever the developer needs to add documentation
about a function which is defined within the program. This is to enhance the code-readability and
6
understandability. Comments are not executed by the compiler and simply ignored during execution.
The comments are of the following types:
a. Single Line Comments in Java: These comments, as the name suggests, consist of a single line of
comment generally written after a code line to explain its meaning. They are marked with two forward
slashes (//) and are automatically terminated when there is a new line inserted in the editor. For
Example:
int i = 6;
String s = “DataFlair”; // The value of i is set to 6 initially. The
string has value “DataFlair”
b. Multi Line Comments in Java: These comments span for multiple lines throughout the codebase.
They are generally written at the beginning of the program to elaborate about the algorithm. These are
also used by developers to comment out blocks of code during debugging. They comprise of a starting
tag(/*) and an ending tag(*/). For Example:
class Comment {
public static void main(String[] args) throws IOException {
/* all this is under a multiline comment These wont be executed by
compiler
Thank you*/
}
}
c. Documentation Comment: The javadoc tool processes these comments while generating
documentation.
Java Keywords: The following list shows the reserved words in Java. These reserved words may not
be used as constant or variable or any other identifier names. Keywords are the identifiers which have
special meaning to the compiler. These cannot be used to name variables, classes, functions etc. These
are reserved words.
7
each other‘s protected and private members. They are generally imported by using the import keyword
i.e, import java.util.* where we are importing java‘s util package.
b. Main Method: The main method marks the entry point of the compiler in the program. The main
method must always be static. For Example:
public class DataFlair { void teachJava() {
System.out.println(“Teaching Java by DataFlair”);
}
public static void main(String[] args) throws IOException {
System.out.println(“In main”);
DataFlair ob = new DatFlair(); ob.teachJava();
}
}
Output is:
In main
Teaching Java by DataFlair
Control statements: The syntax of control statements in Java are pretty straightforward. Let‘s take a
deeper look into the control statements in Java.
a. Conditional Statements: These statements are purely based on condition flow of the program. Its
divided into the following 3 parts:
if statement: The statement suggests that if a particular statement yields to true then the block
enclosed within the if statement gets executed. Exsmple:
if (condition) {
action to be performed
}
if else statement: This statement is of the format that if a condition enclosed is true then the if
block gets executed. If not the else block gets executed. Example:
if (condition) {
action1;
}
else {
action2
}
Else if statement: This statement encloses an if statement in an else block. Example
if (condition) {
action 1
}
else if (condition2) {
action 2
}
8
Switch case: The switch case is used for multiple condition checking. It‘s based on the value of
the variable. The value of the variable mentioned marks the flow of the control to either of the case
blocks mentioned. Example:
switch (var_name) case value1:
action1; break;
case value2:
action2; break;
default:
action3; break;
b. Iteration Statements: These are the statements which are primarily known as loops in
programming which run a particular set of programs a fixed number of times, Some of the types of
iterative statements are:
For loop: The for loop is responsible for running the snippet of code inside it for a predetermined
number of times. Example:
for (i = 0; i < 5; i++) { System.out.println(“Hi”);
}
While loop: This type of loop runs indefinitely until the condition is false. Example:
while (i < 6) {
System.out.println(“Hi”);
i++;
}
This prints Hi on the screen five times until the value of i becomes 6
Do-while loop: This is the same as the while loop. The only difference lies in the fact that the
execution occurs once even if the condition is false. Example:
do {
}
System.out.println(“Hi”);
while ( i > 6 );
Break statement: This breaks the nearest loop inside which is mentioned. The execution continues
from the next line just when the current scope ends.
Continue Statement: This continues the execution from the next iteration of the loop and skips the
current execution. Example:
while (i < 10) {
if (i == 3) continue; i++;
}
9
Return statement: The return statements are generally useful in methods when returning a value when
the function is done executing. After the return statement executes, the remaining function does not
execute.
Exception Handling in Java: Exception handling is important to custom output the errors during the
unfortunate case of an error occurrence. Syntax of the exception handling is fairly simple and structured. It
goes as below:
try {
// Code block within which error can occur
}
catch(Exception e) {
//Code block to perform when error thrown
}
Finally {
//Code to be executed after the end of try block. This is executed even
if there is no error.
}
There is a special keyword called throws, it is useful to throw custom exceptions. For Example- throw new
ArithmeticException();
Features of Java
Object Oriented − In Java, everything is an Object. Java can be easily extended since it is based on the
Object model.
Platform Independent − Unlike many other programming languages including C and C++, when Java is
compiled, it is not compiled into platform specific machine, rather into platform independent byte code.
This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever
platform it is being run on.
Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be
easy to master.
Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication
techniques are based on public-key encryption.
Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes
the compiled code executable on many processors, with the presence of Java runtime system.
Portable − Being architecture-neutral and having no implementation dependent aspects of the specification
makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a
POSIX subset.
Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time
error checking and runtime checking.
Multithreaded − With Java's multithreaded feature it is possible to write programs that can perform many
tasks simultaneously. This design feature allows the developers to construct interactive applications that
can run smoothly.
10
Interpreted − Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an incremental and
light-weight process.
High Performance − With the use of Just-In-Time compilers, Java enables high performance.
Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry extensive amount of run-time information that can be used
to verify and resolve accesses to objects on run-time.
ASSIGNMENT 4
1. Write short notes on Common Data Types
2. Define operator precedence and operator associativity
3. What is overloaded operator?
4. Briefly explain 4 Categories of Expressions
5. Why do we need Control structures in our program? State types of Control structures you know
11
ASSIGNMENT 5
GROUP ASSIGNMENT
Instruction
1. Form a group of at least 15 and not more than 20 members;
2. Write-up would not exceed 15 pages including the title page;
3. Each group would present their assignment;
4. Writing format;
a. Font and font size: Times new Romans 14,
b. 1.5 line spacing
5. Due date is 2 weeks from the date the assignment was given
QUESTION
Write briefly on the Evolution of the following Programming Languages
1 Python
Python Program Structure
Features of Python
2 LISP
LISP Program Structure
What made LISP Different
Important Points in LISP
Features of LISP
Advantages of LISP
3 PERL
PERL Program Structure
Features of PER
12