c++ file
c++ file
SUBMITTED BY :
HARSHIT KHANNA
Introduction to
Programming
An organized list of instructions that, when executed,
causes the computer to behave in a predetermined
manner. Without programs, computers are useless.
A program is like a recipe. It contains a list of ingredients
(called variables) and a list of directions (called
statements) that tell the computer what to do with the
variables. The variables can represent numeric data, text,
or graphical images.
There are many programming languages -- C, C++, Pascal,
BASIC, FORTRAN, COBOL, and LISP are just a few. These
are all high-level languages. One can also write programs
in low-level languages called assembly languages,
although this is more difficult. Low-level languages are
closer to the language used by a computer, while high-
level languages are closer to human languages.
Categories of Programming
Language
Programming languages fall into two fundamental
categories low and high-level languages. Low-level languages
are machine-dependent; that is, they are designed to be run
on a particular computer. In contrast, high-level languages
(for example, COBOL and BASIC) are machine-independent
and can be run on a variety of computers.
Areas of Application :
For Develop Graphical related application like a computer and mobile
games.
C++ COMPILER
A C++ Compiler is itself a computer program which’s the only
job is to convert the C++ program from our form to a form the
computer can read and execute. The original C++ program is
called the “source code”, and the resulting compiled code
produced by the compiler is usually called an “object file”.
Before compilation, the preprocessor performs preliminary
operations on C++ source files. A preprocessed form of the
source code is sent to the compiler.
After compilation stage object files are combined with
predefined libraries by a linker, sometimes called a binder, to
produce the final complete file that can be executed by the
computer. A library is a collection of pre-compiled “object code”
that provides operations that are done repeatedly by many
computer programs.
C++ CHARACTER SET
The character set is a set of valid characters that a language can
recognize
TOKENS
A token is a group of characters that logically belong together. The
programmer can write a program by using tokens. C++ uses the
following types of tokens. Keywords, Identifiers, Literals, Punctuators,
Operators.
C++ is case sensitive that is upper case and lower-case letters are
3. Literals :
Literals (often referred to as constants) are data items that never
change their value during the execution of the program. The
following types of literals are available in C++.
Integer-Constants
Character-constants
Floating-constants
Strings-constants
Integer Constants: Integer constants are the whole number
without any fractional part. C++ allows three types of integer
constants.
Decimal integer constants: It consists of a sequence of digits and
should not begin with 0 (zero). For example, 124, - 179, +108.
Octal integer constants: It consists of a sequence of digits starting
with 0 (zero). For example. 014, 012.
Hexadecimal integer constant: It consists of a sequence of digits
preceded by ox or OX.
BASIC SYNTAX
BASIC DATA TYPES
C++ supports many data types. The built-in or basic data types
supported by C++ are an integer, floating point, and character.
These are summarized in table along with description and
memory requirement
VARIABLES
It is a location in the computer memory which can store data
and is given a symbolic name for easy reference. The variables
can be used to hold different values at different times during
the execution of a program.
To understand more clearly, we should study the following
statements: Total = 20.00; In this statement, a value 20.00 has
been stored in a memory location Total.
Declaration of a variable
Before a variable is used in a program, we must declare it. This
activity enables the compiler to make available the appropriate
type of location in the memory.
float Total;
You can declare more than one variable of the same type in a
single statement int x,y;
Initialization of variable
When we declare a variable it's default value is undetermined.
We can declare a variable with some initial value.
int a = 20;
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well
as user-defined data types. Following table lists down seven
basic C++ data types:
Flow of control
Statements
Statements are the instructions given to the computer to
perform any kind of action. Action may be in the form of data
movement, decision making etc. Statements form the smallest
executable unit within a C++ program. Statements are always
terminated by a semicolon.
Compound Statement
A compound statement is a grouping of statements in which
each individual statement ends with a semicolon. The group of
statements is called block. Compound statements are enclosed
between the pair of braces ({}.). The opening brace ({) signifies
the beginning and closing brace (}) signifies the end of the
block.
Null Statement
Writing only a semicolon indicates a null statement. Thus ';' is a
null or empty statement. This is quite useful when the syntax of
the language needs to specify a statement but the logic of the
program does not need any statement. This statement is
generally used in for and while looping statements.
Conditional Statements
Sometimes the program needs to be executed depending upon
a condition. C++ provides the following statements for
implementing the selection control structure.
•if statement
•if else statement
• nested if statement
•switch statement
if statement
The syntax of the if statement
if (condition)
{
statement(s);
}
From the flowchart, it is clear that if the if the condition is true,
the statement is executed; otherwise it is skipped. The
statement may either be a single or compound statement.
if else statement
syntax of the if - else statement
if (condition)
statement1;
else
statement2;
From the above flowchart, the given condition is evaluated first.
If the condition is true, statement1 is executed. If the condition
is false, statement2 is executed. It should be kept in mind that
statement and statement2 can be a single or compound
statement.
Nested if statement
The if block may be nested in another if or else block. This is
called nesting of if or else block.
Switch statement
The if and if-else statements permit two-way branching
whereas switch statement permits multiple branching.
Looping statement
It is also called a repetitive control structure. Sometimes we
require a set of statements to be executed many times by
changing the value of one or more variables each time to obtain
a different result.
ARRAY
An array is a collection of data elements of same data type. It is
described by a single name and each element of an array is
referenced by using array name and its subscript no.
Declaration of Array
Type arrayname[numberofelements];
For example, int Age[5] ;
float cost[30];
Arrays as Parameters
At some moment, we may need to pass an array to a function
as a parameter. In C++, it is not possible to pass a complete
block of memory by value as a parameter to a function, but we
can pass its address. For example, the following function: void
print(int A[]) accepts a parameter of type "array of int" called A.
Functions in C++
A function is a group of statements that together perform a
specific task. Every C++ program has at least one function,
which is main (). The function is used for dividing a large code
into a module, due to this we can easily debug and maintain the
code. For example, if we write calculator programs at that time
we can write every logic in a separate function (For addition
sum (), for subtraction sub()). Any function can be called many
times.
Advantage of Function
Code Re-usability
Develop an application in module format.
Easily to debug the program.
Code optimization: No need to write a lot of code.
Type of Function
There are two types of function in C++ Language. They are;
Library function or pre-define function.
User defined function.
Calling a function.
When we call any function, control goes to function body and
execute entire code. For a call, any function just writes the
name of the function and if any parameter is required then pass
a parameter.
Syntax
function_name();
or variable=function_name(argument);
Note: At the time of function calling function must be
terminated with ';'.
Example 3
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<”the value of a is “;
cin>>a;
cout<<”the value of b is “;
cin>>b;
c = a+b;
cout<<”the sum of a and b is “<<c;
return 0;
}