Variables In C++
Variables are used to allocate memory to some data. A variable must be declared and defined before it can be used in a program. Every variable can store a value. In C++ variables are used to store information.
The type of value which the variable store is called Data Types for example int, float, double etc. Variable name is a sequence of one or more letters, digits or underscore, for example: name_. Integer and char variable may be signed and unsigned.
Syntax : <datatype>
<variable names>
Declaration Of Variables :
int a;
float x, y, z;
Variable Initialization :
char name='mini';
Constants In C++
Constants are also used to store information. The value of constants can't be change. A constant must be initialize before use.C++ have two types of constants.
- Literal Constant
- Symbolic Constant
Literal Constants
A literal constant is a value used directly into the program and it can't be change.
For Example : int num=25; here num is a variable of int type, and 25 is a literal constant.
Symbolic Constants
Symbolic constants are represented by a name just like as a variable. Its value can't be change.
For Example : If our program has one integer chairs and another is classes, and we want to get the no. of chairs and we know there are 20 chairs in each class then we can easily compute.
chairs=classes*20
Enumerated Constants
Enumerated constants are used to create new types and then we have to define those variables.
For Example : if we declare day to be enumeration then we can define 7 values for days sun, mon, tues, wed, thurs, fri, sat
Syntax : enum days{sun, mon, tues, wed, thurs, fri, sat};
Operators In C++
The number of operators in C++ is quite large.
-
Arithmetic Operators
-
Increment And Decrement Operators
-
Relational Operators
-
Boolean Operators
-
Assignment Operators
Arithmetic Operators
Operators | Function |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Increment And Decrement Operators
Operators | Function |
A++ | Post Increment |
++A | Pre Increment |
A-- | Post Decrement |
--A | Pre Decrement |
Relational Operators
Operators | Function |
== | Equality |
!= | Not Equal |
< | Less Than |
> | Greater Than |
>= | Greater Than Equal |
<= | Less than Equal |
Boolean Operators
Operators | Function |
&& | And |
|| | Or |
! | Not |
Assignment Operators
Operators | Function |
= | Assign right side value to left side variable |
+= | A+=B means A=A+B |
- = | A-=B means A=A-B |
*= | A*=B means A=A*B |
/= | A/=B means A=A/B |
%= | A%=B means A=A%B |
Statements In C++
There are three types of statements in C++.
-
Conditional Statements
- Looping Statements
-
Jumping Statements
Conditional Statements
-
Simple if statements
-
if else statements
-
switch statements
Simple if Statements :
Syntax :
if (expression is true)
{
action 1;
}
if....else Statements :
Syntax :
if (expression is true)
{
action 1;
}
else
{
action 2;
}
switch Statements :
Syntax :
switch (expression)
{
case 1:statement;
break;
case 2:statement;
break;
case n:statement;
break;
default :statement;
break;
}
Looping Statements
-
for
-
while
-
do...while
for Loop :
Syntax :
for (initial value; condition; expression)
{
statement1;
}
while Loop :
Syntax :
while (condition is true)
{
}
do...while Loop :
Syntax :
do
{
action 1;
}
while (condition is true);
Jumping Statements
-
goto
-
break
-
continue
goto Statement :
A goto statement causes the program to unconditionally transfer control to the statement associated with the goto statement. You cannot use a goto statement to jump over initializations.
break Statement :
break statement is used to exit any kind of loop if a specific condition is met.
continue Statement :
The continue statement can be used to transfer control to the end of loop body.
Comments
Post a Comment