Sei sulla pagina 1di 7

ASSIGNMENT IN C++ PROGRAMMING

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.

Who is Written C++?


Bjarne Stroustrup born 30 December 1950 is a Danish computer scientist, most
notable forthe creation and development of the widely used C++ programming
language. He is aDistinguished Research Professor and holds the College of
Engineering Chair in ComputerScience at Texas A&M University, a visiting professor
at Columbia University, and worksat Morgan Stanley

Describe the purpose of the following Syntax and Grammar


a) # = A hash sign (#) are directives for the pre-processor.
b) #include = The directive #include tells the pre-processor to include a standard file.
c) iostream.h = Header that defines the standard input/output stream objects and used for
cout.
d) int main() = The main function is the point by where all C++ programs start their
execution, independently of its location within the source code.
e) function = A function is a block of code that has a name and it has a property that it is
reusable i.e. it can be executed from as many different points in a C Program as required.
f) cout = The standard output, normally the screen.
g) << = The bitwise left shift (<<) shifts operator bits to the left.
"Hello World\n"; = In fact, this statement performs the only action that generates a
visible effect in our first program. A statement is a simple or compound expression that can
actually produce some effect.
h)

i) \n = (new line) Moves the active position to the initial


position of the next line.
j) ; = the separation between statements is specified with an ending semicolon (;) at the end
of each one, so the separation in different code lines does not matter at all for this purpose.

k) return 0; = The return statement causes the main function to finish

Give 5 mathematical operators and 6 relational operators:


Mathematical Operators
Symbol
Meaning
// +
Add
// Substract
// *
Multiply
// /
Divide
// +=
Add and assign

<
>
<=
>=
==
!=

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)

State statements below and give an example application in C + +


Program++
a. Go to
- provides an unconditional jump from the goto to a labeled statement in the same
function.

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;
}

c. Break and Continue


-The break statement is used to terminate a case in a switch construct. The break
statement is also for termination of a loop, bypassing the normal loop conditional test.
-The continue statement forces the next iteration of the loop to take place, skipping
any code following the continue statement in the loop body. In the for loop, the
continue statement causes the conditional test and then the re-initialization portion of
the loop to be executed. In the while and do..while loops, program control passes to
the coditional test.
Example :
Break
//Program 2.5
//This program finds the inverse of a number
#include<iostream.h>
void main()
{
float fNum;
char cReply;
do
{
cout<<"Enter a number:";
cin>>fNum;
if(fNum == 0)
break;
cout<<"Inverse of the number is "<<1/fNum<<endl;
cout<<"Do you want to input another number(y/n)?";
cin>>cReply;
}

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
-

A while(true) statement repeatedly executes a target statement as long as a


given condition is 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

A C++ jump statement performs an immediate local transfer of control.


Example :
break;
continue;
return [expression];
goto identifier;

g.
-

If / else

An if statement can be followed by an optional else if...else statement, which is very


usefull to test various conditions using single if...else if statement.
Example:

#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;
}

Potrebbero piacerti anche