0% found this document useful (0 votes)
10 views

Part 3 - Syntax and Semantics of Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Part 3 - Syntax and Semantics of Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Syntax & Semantic of

Programming Languages
-Orquesta, R.
Historical Perspective

The C++ programming language was created by Bjarne Stroustrup and his
team at Bell Laboratories (AT&T, USA) to help implement simulation
projects in an object-oriented and efficient way. The earliest versions, which
were originally referred to as “C with classes,” date back to 1980. As the
name C++ implies, C++ was derived from the C programming language: ++ is
the increment operator in C.
Historical Perspective

As early as 1989 an ANSI Committee (American National Standards Institute)


was founded to standardize the C++ programming language. The aim was to
have as many compiler vendors and software developers as possible agree
on a unified description of the language in order to avoid the confusion
caused by a variety of dialects. In 1998 the ISO (International Organization
for Standardization) approved a standard for C++ (ISO/IEC 14882).
Characteristics of C++
C++ is not a purely object-oriented language but a hybrid that contains the
functionality of the C programming language. This means that you have all
the features that are avail- able in C:
■ universally usable modular programs
■ efficient, close to the machine programming
■ portable programs for various platforms.
Advantages of OOP
Object-oriented programming offers several major advantages to software
development:
■ reduced susceptibility to errors: an object controls access to its own data.
More specifically, an object can reject erroneous access attempts
■ easy re-use: objects maintain themselves and can therefore be used as
building blocks for other programs
■ low maintenance requirement: an object type can modify its own internal
data representation without requiring changes to the application.
DEVELOPING A C++ PROGRAM

The following three steps are required to create and translate a C++ program:
1. First, a text editor is used to save the C++ program in a text file. In other
words, the source code is saved to a source file. In larger projects the
programmer will normally use modular programming. This means that the
source code will be stored in several source files that are edited and
translated separately.
DEVELOPING A C++ PROGRAM

The following three steps are required to create and translate a C++ program:
2. The source file is put through a compiler for translation. If everything
works as planned, an object file made up of machine code is created. The
object file is also referred to as a module.
DEVELOPING A C++ PROGRAM

The following three steps are required to create and translate a C++ program:
3. Finally, the linker combines the object file with other modules to form an
executable file. These further modules contain functions from standard
libraries or parts of the program that have been compiled previously.
DEVELOPING A C++ PROGRAM
A beginner’s C++ Program
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
cout<<“Enjoy yourself with C++!” <<
endl;
getch();
}
DEVELOPING A C++ PROGRAM
A beginner’s C++ Program
Output of the previous code
Structure of Function main()
Headers or Libraries

Function Name
Type of Function Beginning of Function
Clear the Previous Display
Display whatever is inside the “ ” in you the monitor screen
Function Block
Freeze the display until Enter key is pressed or Body
Return data with value zero as the function requires an integer type
End of Function
Structure of Function main()
A C++ program is made up of objects with their accompanying member functions
and global functions, which do not belong to any single particular class. Each
function fulfills its own particular task and can also call other functions. You can
create functions yourself or use ready-made functions from the standard library.
You will always need to write the global function main ()yourself since it has a
special role to play; in fact it isthe main program.
The short programming example on the opposite page demonstrates two of the
most important elements of a C++ program. The program contains only the
function main() and displays a message.
Structure of Function main()
The first line begins with the number symbol, #, which indicates that
the line is intended for the preprocessor. The preprocessor is just
one step in the first translation phase and no object code is created
at this time.
You can type #include to have the preprocessor copy the quoted file
to this position in the source code. This allows the program access to
all the information contained in the header file. The header file
iostream comprises conventions for input and output streams. The
word stream indicates that the information involved will be treated
as a flow of data.
Structure of Function main()
Predefined names in C++ are to be found in the std (standard)
namespace. The using directive allows direct access to the
names of the std namespace.
Program execution begins with the first instruction in function
main(), and this is why each C++ program must have a main
function. The structure of the function is shown on the
opposite page. Apart from the fact that the name cannot be
changed, this function’s structure is not different from that of
any other C++ function.
Structure of Function main()
In our example the function main() contains two statements. The first
statement cout << “ENJOY!<< endl; outputs the text string Enjoy
yourself with C++ ! on the screen. The name cout (console output)
designates an object responsible for output. The two less than symbols
<< indicates the characters are being “pushed” to the output stream.
Finally endl(end of line) causes a line feed. The statement return 0;
terminates the function main() and also the program, returning a value
of 0 as an exit code to a calling program. It is standard practice to use
the exit code 0 to follow by a semicolon. By the way, the shortest
statement comprises only a semicolon and does nothing.
Another C++ Program Example
This example shows the structure of a C++ program
containing multiple functions. In C++, functions do not
need to be defined in any fixed order. For example,
you could define the function message() first,
followed by the function line(), and finally the main()
function.
However, it is more common to start with the main()
function as this function controls the program flow. In
other words, main() calls functions that have yet to be
defined. This is made possible by supplying the
compiler with a function prototype that includes all
the information the compiler needs
In the example above it introduces comments. Strings enclosed in /* . . . */ or starting with // are interpreted
as comments.
EXAMPLES:
/* I can cover several lines of comments */
// I can cover just one line of comments

Comments are an important notes added


into the script for better understanding on the
purpose of the codes.
In single-line comments the compiler ignores any characters
following the // signs up to the end of the line. Comments that
cover several lines are useful when troubleshooting, as you can
use them to mask complete sections of your program. Both
comment types can be used to comment out the other type.
As to the layout of source files, the compiler parses each source file
sequentially, breaking the contents down into tokens, such as function
names and operators. Tokens can be separated by any number of
whitespace characters, that is, by spaces, tabs, or new line characters.
The order of the source code is important but it is not important to
adhere to a specific layout, such as organizing your code in rows and
columns. For example:
void message ( ) {
cout <<"In function message()." << endl;
}
might be difficult to read, but it is a correct definition of the function
message(). Preprocessor directives are one exception to the layout rule
since they always occupy a single line. The number sign, #, at the
beginning of a line can be preceded only by a space or a tab character.
To improve the legibility of your C++ programs you should adopt a
consistent style, using indentation and blank lines to reflect the
structure of your program. In addition, make generous use of
comments.
Syntax
The previous slides shows the structure and meaning of its structures in developing a computer program
using C++.

The way how the codes are arrange is what we called SYNTAX. Syntax in computer programming is
equivalent to GRAMMAR in English language.
For example this line to display a text
cout<<“Display this text”<<endl;
Cant be written as
Cout>>”Display this text”>>endl
Changing the << into >> and removing the ; means it is grammatically incorrect and would produce a
SYNTAX ERROR that could not be understood by the computer.

Just like for example this sentence “The quick brown fox jumps over the lazy dog.” cant written as “the
lazy dog jumps quick The brown fox over”
Semantic
While syntax represents the grammar of the programming language,
semantic represents the MEANING of the code.
For example by looking at the previous example we will know that this
void main(){
}
means we are about to create a function, the main function of our program
or that this line cout<<“Hello”<<endl; means it will display a Hello
word in our computer monitor when we run the program.
Syntax and Semantic
Syntax may differ across programming languages for example:
C++ :
void main() {
cout<<“Display this text”<<endl;
}
Java :
public static void main() {
System.out.println(“Display this text”);
}
Both codes has the same meaning, that is to have a void main function that will display the text
“Display this text” but has different structure.
That is to say that SYNTAX may differ but SEMANTIC remains the same. That also means that
you have to understand at least one computer programming language and it will be then easy to
transfer to another computer programming language since it has the same meaning and uses
only the syntax may slightly differ from each other.
IN HUMAN LANGUAGE

TAGALOG: Ako ay Pilipino


ENGLISH: I am a Filipino

Both has the same meaning but there at least a slight difference in the
structure.

You might also like