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

Introduction Week#1

Introduction for class

Uploaded by

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

Introduction Week#1

Introduction for class

Uploaded by

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

Programming Fundamental Week#1

Machine Languages, Assembly Languages and High-Level


Languages

 Programmers write instructions in various programming languages, some


directly understandable by computers and others requiring intermediate
translation steps.

A. Machine Languages:

 Any computer can directly understand only its own machine language
(also called machine code), defined by its hardware architecture. Machine
languages generally consist of numbers (ultimately reduced to 1s and 0s).
Such languages are difficult for humans.

B. Assembly Languages:

 Programming in machine language was simply too slow and tedious for
most programmers. Instead, they began using English-like abbreviations
to represent elementary operations. These abbreviations formed the basis
of assembly languages. Translator programs called assemblers were
developed to convert assembly- language programs to machine language.
Although assembly language code is clearer to humans, it’s
incomprehensible to computers until translated to machine language.

C. High-Level Languages:

 To speed up the programming process further, high-level languages were


developed in which single statements could be written to accomplish
substantial tasks. High-level languages, such as C, C++, Java, C#, Swift
and Visual Basic, allow you to write instructions that look more like every
day English and contain commonly used mathematical notations.
Translator programs called compilers convert high-level language
programs into machine language.

 The process of compiling a large high-level language program into machine


language can take a considerable amount of computer time. Interpreter
programs were developed to execute high-level language programs directly
(without the need for compilation), although more slowly than compiled
programs. Scripting languages such as the popular web languages
JavaScript and PHP are processed by interpreters.
Programming Fundamental Week#1

C and C++:

 C was implemented in 1972 by Dennis Ritchie at Bell Laboratories. It


initially became widely known as the UNIX operating system’s development
language. Today, most of the code for general-purpose operating systems
is written in C or C++.

 C++ evolved from C, which is available for most computers and is hardware
independent. With careful design, it’s possible to write C programs that
are portable to most computers.

 The widespread use of C with various kinds of computers (sometimes


called hardware platforms) unfortunately led to many variations. A
standard version of C was needed. The American National Standards
Institute (ANSI) cooperated with the International Organization for
Standardization (ISO) to standardize C worldwide; the joint standard
document was published in 1990.

 C11 is the latest ANSI standard for the C programming language. It was
developed to evolve the C language to keep pace with increasingly powerful
hardware and ever more demanding user requirements. C11 also makes
C more consistent with C++.

 C++, an extension of C, was developed by Bjarne Stroustrup in 1979 at Bell


Laboratories. Originally called “C with Classes,” it was renamed to C++ in
the early 1980s. C++ provides a number of features that “spruce up” the
C language, but more importantly, it provides capabilities for object-
oriented programming that were inspired by the Simula simulation
programming language.

C++ Standard Library:


 C++ programs consist of pieces called classes and functions. You can
program each piece yourself, but most C++ programmers take advantage
of the rich collections of classes and functions in the C++ Standard
Library. Thus, there are really two parts to learning the C++ “world.” The
first is learning the C++ language itself (often referred to as the “core
language”); the second is learning how to use the classes and functions
in the C++ Standard Library.
Programming Fundamental Week#1

Basic Program Construction:

Let’s look at a very simple C++ program. This program is called FIRST, so its
source file is FIRST.CPP. It simply prints a sentence on the screen. Here it is:

Example:

#include <iostream.h>

using namespace std;

int main()

cout << “Every age has a language of its own\n”;

return 0;

Despite its small size, this program demonstrates a great deal about the
construction of C++ programs. Let’s examine it in detail.

Functions:

Functions are one of the fundamental building blocks of C++. The FIRST program
consists almost entirely of a single function called main(). The only parts of this
program that are not part of the function are the first two lines—the ones that
start with #include and using. A function can be part of a class, in which case it
is called a member function. However, functions can also exist independently of
classes. Functions that are separate standalone entities, as main() is here.
Programming Fundamental Week#1

Function Name:

The parentheses following the word main are the distinguishing feature of a
function. Without the parentheses the compiler would think that main refers to
a variable or to some other program element. When we discuss functions in the
text, we’ll follow the same convention that C++ uses: We’ll put parentheses
following the function name. The parentheses aren’t always empty. They’re used
to hold function arguments: values passed from the calling program to the
function. The word int preceding the function name indicates that this particular
function has a return value of type int.

Braces and the Function Body:

The body of a function is surrounded by braces (sometimes called curly


brackets). These braces play the same role as the BEGIN and END keywords in
some other languages: They surround or delimit a block of program statements.
Every function must use this pair of braces around the function body. In this
example there are only two statements in the function body: the line starting
with cout, and the line starting with return. However, a function body can
consist of many statements.

Always Start with main():

When you run a C++ program, the first statement executed will be at the
beginning of a function called main(). (At least that’s true of the console mode
programs in this book.) The program may consist of many functions, classes,
and other program elements, but on startup, control always goes to main(). If
there is no function called main() in your program, an error will be reported when
you run the program. In most C++ programs, main() calls member functions in
various objects to carry out the program’s real work. The main() function may
also contain calls to other standalone functions.

Program Statements:
Programming Fundamental Week#1

The program statement is the fundamental unit of C++ programming. There are
two statements in the FIRST program: the line

cout << “Every age has a language of its own\n”;

and the return statement

return 0;

The first statement tells the computer to display the quoted phrase. Most
statements tell the computer to do something. In this respect, statements in C++
are similar to statements in other languages. In fact, as we’ve noted, the majority
of statements in C++ are identical to statements in C.

A semicolon signals the end of the statement. This is a crucial part of the syntax
but easy to forget. In some languages (like BASIC), the end of a statement is
signaled by the end of the line, but that’s not true in C++. If you leave out the
semicolon, the compiler will often (although not always) signal an error.

The last statement in the function body is return 0;. This tells main() to return
the value 0 to whoever called it, in this case the operating system or compiler. In
older versions of C++ you could give main() the return type of void and dispense
with the return statement, but this is not considered correct in Standard C++.

Whitespace:

We mentioned that the end of a line isn’t important to a C++ compiler. Actually,
the compiler ignores whitespace almost completely. Whitespace is defined as
spaces, carriage returns, linefeeds, tabs, vertical tabs, and form feeds. These
characters are invisible to the compiler. You can put several statements on one
line, separated by any number of spaces or tabs, or you can run a statement
over two or more lines. It’s all the same to the compiler. Thus the FIRST program
could be written this way:

#include <iostream>

using

namespace std;

int main ()

cout << “Every age has a language of its own\n” ;

return 0;

We don’t recommend this syntax—it’s nonstandard and hard to read—but it


does compile correctly.

There are several exceptions to the rule that whitespace is invisible to the
compiler. The first line of the program, starting with #include, is a preprocessor
directive, which must be written on one line. Also, string constants, such as
“Every age has a language of its own”, cannot be broken into separate lines. (If
you need a long string constant, you can insert a backslash (\) at the line break
or divide the string into two separate strings, each surrounded by quotes.)
Programming Fundamental Week#1

Output Using cout:

As you have seen, the statement

cout << “Every age has a language of its own\n”;

The identifier cout (pronounced “C out”) is actually an object. It is predefined in


C++ to correspond to the standard output stream. A stream is an abstraction
that refers to a flow of data. The standard output stream normally flows to the
screen display—although it can be redirected to other output devices.

The operator << is called the insertion or put to operator. It directs the contents
of the variable on its right to the object on its left. In FIRST it directs the string
constant “Every age has a language of its own\n” to cout, which sends it to the
display.

You might also like