Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is C++?
The C++ computer programming language was created for UNIX, providing programmers
with the advantage of being able to modify code without actually changing it. The code is
reusable. Library creation is also cleaner. The language is considered portable and does not
require the use of a specific piece of hardware or just one operating system.
Another important feature of C++ is the use of classes. Classes help programmers organize
of their code and avoid mistakes. There are times when mistakes do slip through, but
classes can be instrumental in finding bugs and correcting them.
The original C++ compiler, called Cfront, was written in the C++ programming language.
Compilation in this language is considered efficient and fast. Its speed can be attributed to its
high-level features in conjunction with its low-level components. When compared to other
computer programming languages, it can be viewed as quite short. This is due to the fact
that it leans towards the use of special characters instead of keywords.
<
>
<=
>=
==
!=
Relational Operators
Symbol
Meaning
Less than
Greater than
Less than or equal to
Greater than or equal to
Equality (equal to)
Inequality (not equal to)
Example :
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
LOOP:do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
goto LOOP;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
b. While
-A while loop can also terminate when a break, goto, or return within the statement
body is executed. Use continue to terminate the current iteration without exiting
the while loop.Continue passes control to the next iteration of the while loop.
Example :
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
while(cReply != 'n');
}
Continue
//Program 2.6
//This program finds the square of the numbers less than 100
#include<iostream.h>
void main()
{
int iNum;
char cReply = 'y';
do
{
cout<<"Enter a number:";
cin>>iNum;
if(iNum > 100)
{
cout<<"The number is greater than 100, enter another"<<endl;
continue;
}
cout<<"The square of the number is: "<<iNum * iNum<<endl;
cout<<"Do you want to enter another(y/n)?";
cin>>cReply;
}
while(cReply != 'n');
}
d.
While True
-
Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
e.
Do / While
A do / while loop is similar to a while loop, except that a do...while loop is guaranteed
to execute at least one time.
Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
f.
-
Jump / Loop
g.
-
If / else
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
// check the boolean condition
if( a == 10 )
{
// if condition is true then print the following
cout << "Value of a is 10" << endl;
}
else if( a == 20 )
{
// if else if condition is true
cout << "Value of a is 20" << endl;
}
else if( a == 30 )
{
// if else if condition is true
cout << "Value of a is 30" << endl;
}
else
{
// if none of the conditions is true
cout << "Value of a is not matching" << endl;
}
cout << "Exact value of a is : " << a << endl;
return 0;
}