Skip to main content

Basics of C++

 

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.

  1. Arithmetic Operators

  2. Increment And Decrement Operators

  3. Relational Operators

  4. Boolean Operators

  5. 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

  1. for

  2. while

  3. do...while

for Loop :
Syntax :
for
(initial value; condition; expression)
{
   statement1;
}

while Loop :
Syntax :
while
(condition is true)
{

     statement1;

}
do...while Loop :
Syntax :
do
{
     action 1;
}
while (condition is true);

Jumping Statements

  1. goto

  2. break

  3. 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

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

C++ Program to define a Class BOOK and accessing member function using its object.

  #include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST( int N) { float tcost; tcost=PRICE*N; cout<<tcost; } public : void INPUT() { cout<<" Enter Book Number "; cin>>BOOKNO; cout<<" Enter Book Title "; gets(BOOKTITLE); cout<<" Enter price per copy "; cin>>PRICE; } void PURCHASE() { int n; cout<<" Enter number of copies to purchase "; cin>>n; cout<<" Total cost is "; TOTAL_COST(n); } }; void main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); }

Addition of Two Number using servlet and jsp

index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > < title > JSP Page </ title > </ head > < body bgcolor ="yellow" > < center >< h1 > Addition of Two Number </ h1 ></ center > < form action =" additionservlet " method ="post" > < table border ="0" width ="100" align ="center" > < tr > < td > First Number </ td > < td >< input type ="text" name ="txtnum1" value ="" /></ td > ...