0% found this document useful (0 votes)
32 views30 pages

Hsslive Xii Comp CH 1 Review of C++ Programming Upt

Uploaded by

Imad Zauq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views30 pages

Hsslive Xii Comp CH 1 Review of C++ Programming Upt

Uploaded by

Imad Zauq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

REVIEW OF C++

Review of
PROGRAMMING
C++
Programmin
g SHAHIJA P V
HSST COMPUTER APPLICATION
ANJARAKANDY HSS
BASICS OF C++

It is developed By Bjarne Stroustrup at AT


& T Bell Laboratories, USA.

It is powerful, Popular and Object Oriented


Programming (OOP) Language.
An Overview of C++
1. Character set 1.Fundamental unit of C++
language. Classified into letters
(a - z, A - Z), digits (0 - 9),
special characters (# , ; , : >, {
, [ , ) etc.

2. Tokens

2.Basic building blocks of C++


programs. Constituted by one
or more characters. Classified
into keywords, identifiers,
3.Keywords literals, punctuators and
operators.
3.Reserved words that give
special meaning to the
4.Identifiers
language compiler.
4.User-defined words to identify
memory locations, statements,
functions, data types, etc.
Overview of C++
5. . Tokens that do not change
5. Literals their value during the
program run. They are also
known as constants. Classified
into integer constants, floating
point constants, character
constants and string
6. Operators constants.

6. Symbols that tell the compiler


about some operations.
7. Punctuators operators are classified into
unary, binary and ternary
7. They give semantic and
syntactic meaning to the
8. Data types compiler. Eg: #,(,{,[,! Etc.
8.The type specifies to the
Overview of C++
9. Type modifiers
9.These are used to change the size and
range of datatype. signed, unsigned,
short and long are the type modifiers

10. Expressions are constituted by


operators and required operands to
perform an operation..Arithmetic
10. Expressions expressions, relational expressions
and logical expressions.

11. It is the process of converting the


current data type of a value into
another type.Implicit conversion(type
promotion),Explicit Conversion(Type
Casting).
11. Type conversion
Statements in C++ Programs
 Every C++ program begins with pre-processor directives
(#include ) statement. The pre-processor directive
statements with header file like <iostream> are followed
by using namespace statement. namespace std to specify
the scope of identifiers cin and cout. Then the main()
function appears. Every C++ program start and end within
main() function.
Commonly used C++ statements are

 Declaration statements

 Input Statements

 Output statements

 Assignment statements
Declaration Statements
Variables are identifiers of memory locations. The following
statements are examples for variable declaration:
int n, sum;
float rad, area;
signed int a,b,c;

Values are giving at the time of declaration known as


variable initialization.
int n=10;
The variable initialisation begins with the access modifier
const, ie const int n=10; means the value of n does not
change during execution. It treated as permanent
constant.
Input/Output Statements
• C++ provides the operator >>, called
extraction operator or get from operator(binary
operator).

Eg: cin>>a; cin used as input


statement.

• To perform output operation, C++ gives the


operator <<,called insertion or put to
operator(binary operator).

Eg: cout<<a; cout used as output


Assignment Statements
 A specific data is stored in memory locations (variables) using
assignment operator (=). The statement that contains ‘=‘ is
known as assignment statement. It is also a binary operator.
 We use Assignment statements for creating arithmetic
assignment expression, Prefix and Postfix expression etc.
 Arithmetic Assignment expression
n+=2; // It is equivalent to n=n+2;
a*=b; // It is equivalent to a=a*b;
sum-=n%10; // It is equivalent to sum=sum-n%10;

n++; // It is equivalent to n=n+1;


a--; // It is equivalent to a=a-1;
There are two versions for these operators: postfix form and prefix
form.
Prefix and postfix form of
increment/decrement operators
There are two forms for these operators:
postfix (a++ and b --) & prefix (++a and --b).
1) c = a++; means c=a; and a=a+1; ( post increment)
2) d=b--; means d=b; and b=b-1; (post decrement)
3) c = ++a; means c = a+1; and a=a+1;(pre
increment)
4) d= --b ; means d=b-1 ;and b=b-1;(pre
decrement)
If a= 5 and b=3
1) c = 5;and a = 6; 3. c=6; and
2) d= 3; and b=2;
a=6;
4.d=2;and b=2;
Structure of a C++ program
 To find the area and perimeter of
a rectangle
#include using namespace std;
int main()
{
float length, breadth, peri, area;
cout << "Enter the length and
breadth of rectangle: ";
cin >> length >> breadth;
peri = 2*(length + breadth);
area = length * breadth;
cout << "Perimeter = " << peri <<
endl; cout << "Area = " << area
<< endl; return 0;
}
Control statements
These are the statements that alter the normal
execution of a program.
(i) Decision /selection statements
(ii) Iteration /Looping statements.
The decision statements are based on a
condition. types of decision statements,
a) Simple if
b) if – else
c) else if ladder
d) switch statement
If statement
Syntax: Eg:
if(condition) if(mark>=18)
{ {
Statement; cout<<“The student
} fail”;
}
First ‘Condition’ is
evaluated,if condition
is true then
statement executed.
If - else
Syntax:
if (condition)
{
Statement1;
} if(mark>=18)
else {
{ cout<<“The student is
Statement2; passed”;
} }
else
if condition evaluates to {
True, the Statement1 will cout<<“The student is
be executed if it is false
failed”;
Statement2 will be
executed. }
Syntax:
else if ladder
if (condition 1)
{
Statement1 ; Eg:
}
else if (condition2)
if(n1>n2 && n1>n3)
{ {
Statement2; cout<<“n1 is large”;
}
else if }
{ else if(n2>n3)
………………
{
}
else cout<<“”n2 is large”;
{ }
Statements;
else
}
This statement is used for {
checking multiple conditions.
cout<<“n3 is large”;
}
Switch statement
Syntax:
switch (Expression)
{ Eg:
case Constant 1 : switch(n)
Statement 1; {
break; case 1:cout<<"Sunday";break;
case Constant 2 : case 2:cout<<"Monday";break;
Statement2;
case 3:cout<<"Tuesday";break;
break;
case
case Constant 3 : 4:cout<<"Wednesday";break;
Statement3;
case 5:cout<<"Thursday";break;
break;
case 6:cout<<"Friday";break;
case ConstantN:
StatemeN; case 7:cout<<"Saturday";break;
break; default:cout<<"invalid input";
default : Wrong }
Satement;
}
Conditional operator (?:)
It is a ternary operator of C++ and it requires
three operands. It can substitute if - else
statement.

Eg: Large = (a>b) ? a: b;


we can substitute the above conditional
operator statement with if - else statement.
if(a>b)
Large=a;
else
Large=b;
Looping/Iteration statements
Looping statements are the set of statements
for repeated execution. It is Classified into
two Entry Control loop (while,for) and Exit
Control loop(d0..while).
A looping statement has four components:
i. Initialisation of loop control
variable
ii. Test expression
iii. Update expression
iv. Body of the loop
Entry Control Loop Exit Control Loop
 Condition is checked  Condition is checked
before the execution of after the execution of
the body . the body .
 Body of the loop may  Body of the loop will
never be executed. surely be executed at
 Suitable when skipping least once.
of the body from being  Suitable when normal
executed is required execution of the body is
 Eg:while,for to be ensured.
 Eg:do..while
while loop
Eg:Print Even
Syntax: numbers upto 50
Initialization expression; i=0;
while (Test Expression) while(i<=50)
{ {
loop-body ; cout<<i<<“\t”;
Updating
i+=2;
expression;
}
}
for loop
Syntax:
for (Initialization ; Test Expression; Update
Expression)
{
body-of the-loop;
}
 Eg: display even numbers up to 50
for( int i=0;i<=50;i=i+2)
{
cout<<i<<“\t”;
}
do while loop
Eg:Print Even
Syntax: numbers upto 50
Initialization expression; i=0;
do do
{ {
loop-body ; cout<<i<<“\t”;
Updating
i+=2;
expression;
} while (Test
} while(i<=50);
Expression);
Nested Loops
Placing a loop inside the body of another loop is
called nesting of a loop. In a nested loop, the
inner loop statement will be executed repeatedly
as long as the condition of the outer loop is true.

Eg: for( i=1; i<=2; ++i)


//Outer loop
{
for(j=1; j<=3; ++j)
//Inner loop
{
cout<< "\n" << i << " and "
<< j;
}
}
Eg: Nested Loop
 To display a triangle of
stars Output:
#include<iostream>
using namespace std;
int main()
{
short int i, j;
for(i=1; i<=5; ++i)
//outer loop
{
cout<< "\n" ;
for(j=1; j<=i; ++j) //
inner loop
cout<< '*';
}
Jump statements
The statements used for the transfer of
program control from one place to another
are called jump statements.
Jump statements are:
 go to
 continue
 break
go to statement
The goto statement can transfer the program
control to anywhere in the function. A goto
statement is marked by a label, which is an
identifier. Eg: int i=1;
The syntax of goto statement is: start:
goto label; cout<<i;
++i;
............; If(i<=50)
............; goto start;
label: ............;
............;
where the label can appear in the program
break statement
break statement transfers the program control outside
the switch block or it takes the program control outside
the immediate enclosing loop.
Output:
for(i=1; i<=5; ++i) //outer loop
{
cout<<"\n";
for(j=1; j<=i; ++j) //inner loop
{
cout<<"* ";
if (j==3)
break;
}
}
continue statement
The statement continue is another jump statement
used for skipping over a part of the code within
the loop-body and forcing the next iteration.
for (i=1; i<=10; ++i)
{
if (i==6)
continue;
cout<<i<<"\t";
}
This code gives the following output: 1 2 3 4 5 7 8 9
10
break continue
 Used with switch and  Used only with loops.
loops.
 Takes the control to the
 Takes the control beginning of the loop by
outside the loop by skipping the remaining
skipping the remaining part of the body.
part of the body.  Program control goes
outside only when the
 Program control goes test expression of the
outside the loop even loop returns false.
though the test
expression returns true.
Thank you
SHAHIJA P V
HSST COMPUTER APPLICATION
ANJARAKANDY HSS

You might also like