You are on page 1of 7

Introduction to C++

● C++ is object-oriented programming Language (OOP), created by Bjarne


Stroustrup in 1983 at Bell Labs
● C++ is a superset of the C language.

Data types

● A data type, denotes the type of a value .


● Data types in C++ is mainly divided into two types:
● Primitive Data Types is a built-in or predefined data types and can be used
directly by the user to declare variables.
● example: int, char , float, bool etc. Primitive data types available in C++ are:
● Abstract or user defined data type: These data types are defined by user itself.

Variable: A variable is a temporary container to store information, in programs

 A variable can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits)
and underscore( _ ) symbol.
 Variable names must be unique
 The first character must be an alphabet or underscore.
 You cannot use a keyword as identifiers.
 Only first thirty-one (31) characters are significant.
 Must not contain white spaces.
 Variables are case-sensitive.

Syntax for Variable name

 type variable_name;

or
 type variable_name, variable_name, variable_name;
Constants: A constant value is the one which does not change during the execution of
a program.

● constants never change in value


● must initialize a constant when it is created
● it is also called as a Literals
● In C++ language constants are of two types:
o 1. Numeric Constant.
o 2. Non-Numeric Constant (Character Constant)

Structure of C++

 Documentations (Documentation Section)


 Preprocessor Statements (Link Section)
 The main() function
o Local Declarations
o Program Statements & Expressions

. Documentation section: The documentation section consists of a set of comment lines


giving the name of the program, the author and other details.

2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #include directive

3. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable par
t
1. Declaration part: The declaration part declares all the variables
executable part.
2. Executable part: There is at least one statement in the executable part. These two
parts must appear between the opening and closing braces. The program
execution brace of the main function is the logical end of the program. All statements in
the declaration and executable part end with a semicolon.

Flow of Control - It is the order in which instructions, statements and function calls being executed
or evaluated when a program is running.
Conditional statements, also known as selection statements, are used to make decisions based on
a given condition. If the condition evaluates to True, a set of statements is executed, otherwise
another set of statements is executed.
o if statement
o if else statement
o if…else..If statement
o switch statement
if Statement - The if statement selects and executes the statement(s) based on a given condition.
If the condition is true then a given set of statement(s) is executed. However, if the condition is
false, then the given set of statements is skipped and the program control passes to the statement
following the if statement.
Syntax:
if (condition)
{
statement(s);
}
if ... else statement - The if - else statement causes one of the two possible statement( s) to
execute, depending upon the outcome of the condition.
Syntax
Example:
if (condition)
if(a>b)
{
{
one or more statement(s);
cout<<” A is greater”;
}
}
else
{ else

one or more statement(s); {

} cout<<” B is greater”;
}
Switch Statement - The switch statement selects a set of statements from the available sets of
statements. The switch statement tests the value of an expression in a sequence and compares it
with the list of integers or character constants. When a match is found, all the statements
associated with that constant are executed.
Syntax
switch(expression)
{
case value:
statement(s);
break;
case value:
statement(s);
break;
default:
statement(s);
}
The following rules apply to a switch statement −
● It can have any number of case statements within a switch.
● Statements of switch is end with a semicolon (;)
● Each case value is end with a colon. (:)
● When a break statement is reached, the switch terminates, and the flow of control jumps
outside of the switch statement.
Example: Write a program in C++ to display the performance based on the grade using switch
case.
#include <iostream.h>
#include<conio.h>
void main ()
{
char grade;
cout<<”Enter your grade”;
cin>>grade;
switch(grade)
{
case 'A' :
cout << "Excellent!”;
break;
case 'B' :
case 'C' :
cout << "Well done”;
break;
case 'D' :
cout << "You passed”;
break;
case 'F' :
cout << "Better try again";
break;
default :
cout << "Invalid grade”;
}
cout << "Your grade is " << grade;
getch();
}
Looping Statement - A program executes the sequence of statements many times until the stated
condition becomes false is called looping.

Types of Loops

Depending upon the position of a control statement in a program, a loop is classified into two
types:
1. Entry controlled loop - A condition is checked before executing the body of a loop. It is also
called as a pre-checking loop.
2. Exit controlled loop - A condition is checked after executing the body of a loop. It is also called
as a post-checking loop.
Note: The control conditions must be well defined and specified otherwise the loop will execute
an infinite number of times. The loop that does not stop executing and processes the statements
number of times is called an infinite loop. An infinite loop is also called as an "Endless loop."
The characteristics of an infinite loop are:
1. No termination condition is specified.
2. The specified conditions never meet. (The specified condition determines whether to execute
the loop body or not).
Types of Loop
1. The while loop - It is an entry-controlled loop. In while loop, the condition is evaluated before
processing a body of the loop. If a condition is true then and only then the body of a loop is
executed.
Syntax:
while (expression)
{
statement;
}

Eg:
i=1;
while(i<=5)
{
cout<<i;
i++;
}
2. The do-while loop - A do-while loop is similar to the while loop except that the condition is
always executed after the body of a loop. It is also called an exit-controlled loop.
Syntax:
do
{
statement;
}while (expression);
Eg:
i=2;
do
{
cout<<i;
i=i+2;
}while(i<10);
3. The for loop - It is an entry-controlled loop, used for executing a block of statements
repeatedly until a particular condition is satisfied. In This loop all the basic operations like
initialization, condition checking and incrementing or decrementing all these are performed in
only one line.
Syntax:
for (initialCondition; testExpression; iterativeStatement)
{
statement;
}
Eg:
for(i=10;i>=5;i--)
{
cout<<i;
}

You might also like