Do - While Loop

In the while loop test expression is evaluated at the beginning of loop where, in case of do while loop, test, expression is evaluated at the end of loop. Following program illustrate this.


void main()
{
int n,sqr;
char reply='y';
do
{
cout<<"Enter a no";
cin>>n;
sqr=n*n;
cout<<"Square of a no is"<cout<<"Do you want to find square of another no(y/n)?";
cin>>reply;
}while(reply!='n');
}

While Loop

Following program illustrate the use of while constructs.

// program to find sum of first 10 numbers
#include
void main()
{
int n,sum,n=1;
sum=0;
while(n<11)
{
sum=sum+n;
n=n+1;
}
cout<<"Sum of first 10 numbers is"<}

The Conditional Operator

The expression ?: operator is called conditional operator and takes the form.
Exp1? Exp2?:Exp3;

Exp1,Exp2&Exp3 are expressions.
Exp1 is evaluated for true condition, if it is found true Exp2 becomes value of the expression, if false Exp3 becomes value of the expression. For example consider the following statements.

A=10
N=(a>5)?15:0;
N will be assigned the value 15.

The Break Statement

The break statement causes exit from switch body. Controls goes to first statement following the end of the switch construct. If the break statement is not used the controls passes to the statement for the next case, and the remaining statements in the switch construct are executed.

Switch Case Statement

This is a multi- branching statement. This statement tests the value of an expression against the list of constants. When a match is found, control is transferred to that constant for execution.

If Statement

If statement is implemented in two ways.
  • Simple if statement
  • if...else statement
Example:-
if...........else statement

#include
void main()
{
int a,b;
cout<<"Enter values for a and b\n";
cin>>a;
cin>>b;
if(a>b)
{
cout<<"Value of a is more than b\n";
else
{
cout<<"Value of b is more than a\n";
}
}
}

Manipulator in C++

Manipulators are the operators that are used to format the data display. The most commonly used endl and setw. The endl manipulator used to insert a new line. It has the same effect as using the newline character "\n".

Operators in C++

A variable, when declare of a particular type is a set of legal operations that can be performed on it. For example, when integer are declared operation such add, subtract, multiply and divide can be performed them. Operators perform those operations on the variables.
Different types of operators available in C++ are:-
  1. Arithmetic operators - are +, -, *, / and % operator. % operator divide the second value from the first and return the remainder.
  2. Relational operators - are >, >=, <, <=, ==, !=. These operators compare two values and return the appropriate result.
  3. Logical operators - are && and ||. This operator evaluates two expression, and evaluation stops as soon as the true or false value of result is known.
  4. Unary operators - The unary operators operate on one operand- variable or constant. C++ provides the two unary operators ++ and -- for incrementing and decrementing variables. The increment operator add 1 to its operand while decrement operator subtracts 1. The operators can also be used either prefix or postfix operators.

Variables in C++

Variables are fundamental to any language. Values can be assigned to variables, which can be changed during program execution. The value assigned to a variable is placed in the memory allocated to that variable. Variable can be created using key word float, int, and char.

Different between C and C++

C++ is superset of ANSI C. C++ improved version of C by expanding its features to support new software development concepts such as object oriented programming. Many features have been added to C such as comment - C++ supports one or more type of comment in addition to the existing comment style /*.......*/. The new comment style('//' double slash) is useful for one line comment.Unlike C, which requires all the declaration to be made at the beginning of the scope, C++ permits us to make declaration at any point in the program. Many new keywords have been added. It also define new operators new and delete to manage dynamic allocation functions they can be used in place of malloc() and free().

Evolution of C++

C++ is an object-oriented language. C++ was developed by Bjarne Stroustrup at AT & T Bell laboratories in USA in the early 80's. He wanted to combine the object-oriented features of language called Simula 67 and power and elegance of C. The result was C++. Therefore, C++ is an extension of C with a major edition of the class construct feature Simula 67, so it was initially named as 'C with classes'.
The most important features that C++ adds on to C are classes inheritance, function overloading and operator overloading. These features enable programmers to create abstract data types, inherits properties from data types and support polymorphism.
C++ is versatile language for handling very large programs. It is suitable for virtually any programming task including development of editor, compilers, database, communication systems & any complex real life applications.