1. Syntax, semantics, and pragmatics
Every programming language has syntax, semantics, and pragmatics. We have seen that natural languages also
have syntax and semantics, but pragmatics is unique to programming languages.
• A programming language’s syntax is concerned with the form of programs: how expressions, commands,
declarations, and other constructs must be arranged to make a well-formed program.
• A programming language’s semantics is concerned with the meaning of programs: how a well-formed
program may be expected to behave when executed on a computer.
• A programming language’s pragmatics is concerned with the way in
which the language is intended to be used in practice. (C++/ Java……)
2. Language processors
High-level languages are implemented by compiling programs into machine language, by
interpreting them directly, or by some combination of compilation and interpretation.
Any system for processing programs – executing programs, or preparing them for execution –
is called a language processor .
Ex: Language processors include compilers, interpreters, and auxiliary tools like source-code editors
and debuggers.
Programming Paradigms
Programming is a creative process carried out by programmers to instruct a computer on how to do a task. A
program is a set of instructions that tells a computer what to do in order to come up with a solution to a
particular problem. There are a number of alternative approaches to the programming process, referred to
as programming paradigms.
3. Procedural language is one of the most common programming languages in use with notable languages such as
C/C++, Java, ColdFusion and PASCAL.
What does Procedural Language mean?
Procedural language is a type of computer programming language that specifies a series of well-structured
steps and procedures within its programming context to compose a program. It contains a systematic
order of statements, functions and commands to complete a computational task or program.
Procedural Programming
Procedural programming uses a list of instructions to tell the computer what to do step-by-step.
Procedural programming relies on - you guessed it - procedures, also known as routines or subroutines. A
procedure contains a series of computational steps to be carried out. Procedural programming is also
referred to as imperative programming. Procedural programming languages are also known as top-down
languages.
4. Object-Oriented Programming
Object-oriented programming, or OOP, is an approach to problem-solving where all computations are carried out using
objects.
An object is a component of a program that knows how to perform certain actions and how to interact with other elements
of the program.
Objects are the basic units of object-oriented programming. A simple example of an object would be a person. Logically, you
would expect a person to have a name. This would be considered a property of the person. You would also expect a person
to be able to do something, such as walking. This would be considered a method of the person.
A method in object-oriented programming is like a procedure in procedural programming. The key difference here is that the
method is part of an object. In object-oriented programming, you organize your code by creating objects, and then you can
give those objects properties and you can make them do certain things.
A key aspect of object-oriented programming is the use of classes. A class is a blueprint of an object. You can think of a class
as a concept, and the object as the embodiment of that concept. So let's say you want to use a person in your program. You
want to be able to describe the person and have the person do something.
A class called 'person' would provide a blueprint for what a person looks like and what a person can do.
Examples of object-oriented languages include C#, Java, Perl and Python.
13. Objects
JAVA was designed by drastically simplifying C++, removing nearly all its shortcomings. Although primarily a simple
object-oriented language, JAVA can also be used for distributed and concurrent programming.
JAVA is well suited for writing applets (small portable application programs embedded in Web pages), as
a consequence of a highly portable implementation (the Java Virtual Machine) that has been incorporated into all the
major Web browsers. Thus JAVA has enjoyed a symbiotic relationship with the Web, and both have experienced
enormous growth in popularity. C# is very similar to JAVA, apart from some relatively minor design improvements, but
its more efficient implementation makes it more suitable for ordinary application programming.
14. Functional programming is based on functions over types such as lists and trees. The ancestral
functional language was LISP , which demonstrated at a remarkably early date that significant programs can be
written without resorting to variables and assignment.
ML and HASKELL are modern functional languages. They treat functions as ordinary values, which can be
passed as parameters and returned as results from other functions. Moreover, they incorporate advanced type
systems, allowing us to write polymorphic functions (functions that operate on data of a variety of types).
15. Logic programming is based on a subset of predicate logic. Logic programs infer relationships between
values, as opposed to computing output values from input values. P ROLOG was the ancestral logic language, and is still
the most popular.
Programming languages are intended for writing application programs and systems programs. However, there are other
niches in the ecology of computing.
Programming languages script that will later be called whenever required. An office system (such as a word processor or
spreadsheet system) might enable the user to store a script (‘‘macro’’) embodying a common sequence of commands,
typically written in VISUAL BASIC.
The Internet has created a variety of new niches for scripting. For example, the results of a database query might be
converted to a dynamic Web page by a script, typically written in P ERL. All these applications are examples of scripting .
Scripts (‘‘programs’’ written in scripting languages) typically are short and high-level, are developed very quickly, and
are used to glue together subsystems written in other languages.
So scripting languages, while having much in common with imperative programming languages, have different design
constraints. The most modern and best-designed of these scripting languages is PYTHON .
16. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you
reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the
reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters
in these variables.
There are two data types available in Java:
Primitive Data Types
Reference/Object Data Types
Primitive Data Types:
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by
a keyword. Let us now look into detail about the eight primitive data types.
17. byte:
Byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large
arrays, mainly in place of integers, since a byte
is four times smaller than an int.
Example: byte a = 100 , byte b = -50
18. Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as
byte data type. A short is 2 times smaller than an int
Default value is 0.
Example: short s = 10000, short r = -20000
int:
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Int is generally used as the default data type for integral values unless there is a concern about memory.
The default value is 0.
Example: int a = 100000, int b = -200000
short:
Short data type is a 16-bit signed two's complement integer.
19. float:
Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point numbers.
Default value is 0.0f.
Float data type is never used for precise values such as currency.
Example: float f1 = 234.5f
double:
double data type is a double-precision 64-bit IEEE 754 floating point.
This data type is generally used as the default data type for decimal values, generally
the default choice.
Double data type should never be used for precise values such as currency.
Default value is 0.0d.
Example: double d1 = 123.4
20. boolean:
boolean data type represents one bit of information.
There are only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false. Example: boolean one = true
char:
char data type is a single 16-bit Unicode character.
Minimum value is 'u0000' (or 0).
Maximum value is 'uffff' (or 65,535 inclusive).
Char data type is used to store any character. Example: char letterA ='A'
Reference Data Types:
Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are
declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
Class objects, and various type of array variables come under reference data type.
Default value of any reference variable is null.
Example: Animal animal = new Animal("giraffe");
21. Java Literals:
A literal is a source code representation of a fixed value. They are represented directly in the code without any
computation.
Literals can be assigned to any primitive type variable. For example:
byte a = 68;
char a = 'A'
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number
systems as well.
Prefix 0 is used to indicate octal and prefix 0x indicates hexadecimal when using these number systems for literals.
For example:
22. int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String literals in Java are specified like they are in most other languages by enclosing a sequence of characters
between a pair of double quotes. Examples of string literals are:
"Hello World"
"twonlines"
""This is in quotes""
String and char types of literals can contain any Unicode characters. For example:
char a = 'u0001';
String a = "u0001";
Java language supports few special escape sequences for String and char literals as well. They are:
23. Notation Character represented
n Newline (0x0a)
r Carriage return (0x0d)
f Formfeed (0x0c)
b Backspace (0x08)
s Space (0x20)
t tab
" Double quote
' Single quote
backslash
ddd Octal character (ddd)
uxxxx Hexadecimal UNICODE character (xxxx)
24. What is the meaning of the following
control characters:
1.Carriage return
2.Line feed
3.Form feed
Carriage return means to return to the beginning of the current line without advancing
downward. The name comes from a printer's carriage, as monitors were rare when the name was
coined. This is commonly escaped as "r", abbreviated CR, and has ASCII value 13 or 0xD.
25. Linefeed means to advance downward to the next line;
however, it has been repurposed and renamed. Used as "newline", it terminates lines (commonly
confused with separating lines). This is commonly escaped as "n", abbreviated LF or NL, and has ASCII
value 10 or 0xA. CRLF (but not CRNL) is used for the pair "rn".
Form feed means advance downward to the next "page".
It was commonly used as page separators, but now is also used as section separators. (It's uncommonly used in
source code to divide logically independent functions or groups of functions.) Text editors can use this character
when you "insert a page break".
This is commonly escaped as "f", abbreviated FF, and has ASCII value 12 or 0xC.
26. Here data type is one of Java's datatypes and variable is the name of the variable.
To declare more than one variable of the specified type, you can use a comma-
separated list.
Following are valid examples of variable declaration and initialization in Java:
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a is initialized with value 'a'
This chapter will explain various variable types available in Java Language. There
are three kinds of variables in Java:
•Local variables
•Instance variables
•Class/static variables
27. Variable Scope
A scope is a region of the program and broadly speaking there are three places, where variables can be
declared:
•Inside a function or a block which is called local variables,
•In the definition of function parameters which is called formal parameters.
Local Variables:
Variables that are declared inside a function or block are local variables.
They can be used only by statements that are inside that function or block of code.
Local variables are not known to functions outside their own. Following is the example using local variables:
29. public void pupAge()
{
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.pupAge(); }}
This would produce the following result:
Puppy age is: 7
30. Following example uses age without initializing it, so it would give an error at the time of compilation.
public class Test{
public void pupAge(){
int age;
age = age + 7;
System.out.println("Puppy age is : " + age);
} public static void main(String args[])
{ Test test = new Test();
test.pupAge(); }}
This would produce the following error while compiling it:
Test.java:4:variable number might not have been initializedage = age + 7; ^1 error
31. Global Variables:
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold
their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire
program after its declaration. Following is the example using global and local variables:
#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main ()
{
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
} O/P 30
32. #include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main ()
{
// Local variable declaration:
int g = 10;
cout << g;
return 0;
}
When the above code is compiled and executed, it produces the following result:
10
33. Defining Constants:
There are two simple ways in C++ to define constants:
•Using #define preprocessor.
•Using const keyword.
The #define Preprocessor:
Following is the form to use #define preprocessor to define a constant:
#define identifier value
Following example explains it in detail:
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE 'n'
int main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0; }
When the above code is compiled and executed, it produces the following result: 50
34. The const Keyword:
You can use const prefix to declare constants with a specific type as follows:
const type variable = value;
Following example explains it in detail:
#include <iostream>
using namespace std;
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = 'n';
int area; area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0; }
When the above code is compiled and executed, it produces the following result: 50
Note that it is a good programming practice to define constants in CAPITALS.
38. Sample Session:
•***Static Variables***
•Please enter 5 numbers to be summed
•Enter a number: 12
•The current total is 12
•Enter a number: 23
•The current total is 35
•Enter a number: 34
•The current total is 69
•Enter a number: 45
•The current total is 114
•Enter a number: 56
•The current total is 170
•Program completed