Sie sind auf Seite 1von 8

Things to Remember

from
Chapter
Operators and
Expressions in C++

Prepared By : Prakhar Tandon


• An expression is composed of one or more
operators . It is a valid collection of operators,
constants & variables.
• Expressions can be arithmetic, relational, or
logical, compound etc…
• Type of operator used in a expression determine
its type.
• Arithmetic expressions can either be integer
expressions or real expressions or mixed mode
expressions.
• Integer expressions are combinations of
arithmetic operators, integer constants and
integer variables.
• Real expressions are combination of arithmetic,
real constants or real variable .
• Mixed – Mode variables are combination of
arithmetic operators, integer/real
constants/variables.
• Arithmetic operators can also consist of one or
more mathematical functions that are contained in
the header file math.h which is a part of c++
directory
• To use mathematical library function, the header file
math.h must be included.
• In a mixed – mode expression, different types of
variables/constants are converted to one same
type. This process is called type conversion.
• Type conversion cam take place in two forms
:implicit and explicit…
• In Implicit conversion , all the operands are
converted upto the type of the largest operand,
which is called as type promotion.
• The explicit conversion of an operand to a specific type is
called as type casting and it is done by using type-cast
operator () that is used as: (type) expression..
where type is a valid C++ type to which the
conversion is to be done.
• The expression that results into 0 is false and which
results into 1 is true.
• An expression terminated by a ; becomes an statement
that forms the smallest executable unit under C++.
• Variables are initialised using an assignment statement.
• C++ offers special shorthand to simplify a certain type of
assignment operators.
Some Important programs in C++ :

Program To accept three integers and print the largest of


the three…
#include<iostream.h>
#include<conio.h>
Int main()
{ clrscr();
Int x,y,v,max;
Cout<<“Enter Three numbers”;
Cin>>x>>y>>z;
max=x;
if(y>max)
max=y;
if(z>max)
max=z;
cout<<“The Largest of”<<x<<“,”<<y<<“and”<<z<<“is”<<max;
return 0;
}
Program to print first n natural numbers their
sum…
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int i,sum,n;
cout<<“How Many natural numbers ?”;
cin>>n;
for(i=1,sum=0;i<=n;++i)
{
cout<<i;
sum=sum+i;
}
cout<<“The sum o first ”<<n<<“ natural numbers is :”<<sum;
return 0;
}
Program to Find the largest between two unique
numbers …
# include<iostream.h>
void main()
{
int a,b;
cout<<“enter first number:”;
cin>>a;
cout<“enter second number”;
cin>>b;
If(a>b)
cout<<“a is bigger”;
else
cout<<“b is bigger”;
}
Program to print any number in reverse order….
#include<iostream.h>
#include<conio.h>
main()
{
Int value,r_digit;
do
{
cout<<“enter the number to be reversed “;
Cin>>value;
If(value<=0)
cout<<“the number must be positive”;
}
while(value<=0);
Do{
r_digit=value%10;
cout<<r_digit;
value=value/10;
}while(value!=0);
return 0;
}

Das könnte Ihnen auch gefallen