You are on page 1of 8

ASSIGNMENT C++ PROGRAMMING

1. WHAT IS C++.
C++ is an object oriented programming (OOP) language, developed by Bjarne Stroustrup, and is
an extension of C language. It is therefore possible to code C++ in a "C style" or "object-oriented
style." In certain scenarios, it can be coded in either way and is thus an effective example of a
hybrid language.
C++ is a general purpose object oriented programming language. It is considered to be an
intermediate level language, as it encapsulates both high and low level language features.
Initially, the language was called 'C with classes as it had all properties of C language with an
additional concept of 'classes. However, it was renamed to C++ in 1983.
It is pronounced "C-Plus-Plus."

2. WHO IS WRITTEN C++?

Bjarne Stroustrup , born 30 December 1950) is a Danish computer scientist,


most notable for the creation and development of the widely used C++ programming
language. He is a Distinguished Research Professor and holds the College of Engineering
Chair in Computer Science at Texas A&M University, a visiting professor at Columbia
University , and works at Morgan Stanley.

3. DESCRIBETHE PURPOSE OF THE FOLLOWING SYNTAX AND


GRAMMAR.(11m)
A) #
Line beginning with a hash sign(#)are directives for the preprocessor. They are not regular

code lines with expressions but indications for the compiler's preprocessor

B) #include
-

Beginning with a hash sign (#) are directives for the preprocessor. They are not regular
code lines with expressions but indications for the compiler's preprocessor. In this case
the directive #include

C) Iostream.h
-

Usually the compiler inserts the contents of a header file called iostream into the
program. Where it puts it depends on the system. The location of such files may
be described in your compiler's documentation. A list of standard C++ header
files is in the standrat headers reference

D) Int main()
-

Given the name main. Such a named block of code is called a function in C++
parlance. The contents of the block are called the body of the function.

E) Function
-

Allow to structure program in segment of code to perfom individual taks.

F) <<
-

The << operator insert the data that follows it into the stream that preceds it .

G) Hello World\n
-

This line is a C++ statement. C++ statements are terminated by a semicolon (;).
Within the statement <<, called the insertion operator is used to output the string
using the std::cout stream.

H) \n
-

\n is the newline character , when the newline character is output to the console
breaks a newline .

I) ;
-

J)

This character is used to mark the ends in facts it must be include at the end of all
expression statesments in all c++ programs (one of the most common syntax
errors is indeed to forget to include some semicolon after a statements )

Return0;
-

The return statement causes the main function to finish. return may be
followed by a return code (in ourexample is followed by the return code 0) . A
return code of 0 for the main function is generally interpreted as the program
worked as expected without any errors during its execution. This is the most
usual way to end a C++ console program.You may have noticed that not all
the lines of this program perform actions when the code is executed. There
were lines containing only comments (those beginning by //). There were
lines with directives for the compiler's preprocessor (those beginning by #).
Then there were lines that began the declaration of a function (in this
case,the main function) and, finally lines with statements (like the insertion
into cout), which were all included within the block delimited by the braces
({}) of the main function.The program has been structured in different lines in
order to be more readable, but in C++, we do not have strict rules on how to
separate instructions in different lines.

4. GIVE 5 MATHEMATICAL SYMBOL AND 6 RELATIONAL OPERATORS.


i.
MATHEMATICAL OPERATORS
SYMBOL

MEANING

1)+

ADDITION OR UNRY PLUS

2)-

SUBTRACTION OR UNARY MINUS

3)*

MULTIPLICATION

4)/

DIVISION

5)%

REMAINDER AFTER DIVISION

ii.

RELATIONAL OPERATORS
SYMBOL

MEANING

1)==

EQUAL TO

2)>

GREATER THAN

3)<

LESS THAN

4)!=

NOT EQUAL TO

5)>=

GREATER THAN OR EQUAL TO

6)<=

LESS THAN OR EQUAL TO

5. STATESTATEMENTS BELOW AND GIVE AN EXMPLE APPLICATION IN C++


PROGRAM.(14m)
A) Go to
-

The goto statement is a control flow statement that causes the CPU to jump
to another spot in the code. This spot is identified through use of a statement
label.

// goto_statement.cpp
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf_s( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf_s( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
}

B) While

a)
b)
c)
d)
e)
f)
g)
h)
i)
j)

// do_while_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i++);
} while (i < 3);
}

C) Break and Continue

intnPrinted = 0;

C) 1
D) 2
E) 3
F) 4
G) 5
H) 6
I) 7
J) 8
K) 9
L) 10
M) 11
N) 12
O) 13

for(intiii=0; iii < 100; iii++)


{
// messy!
if((iii % 3) && (iii % 4))
{
cout<< iii <<endl;
nPrinted++;
}
}

cout<<nPrinted<< " numbers were found"<<endl;

However, this can be rewritten as the following, which is easier to read:

intnPrinted = 0;

2
3
4

for(intiii=0; iii < 100; iii++)


{

// if the number is divisible by 3 or 4, skip this iteration

if((iii % 3)==0 || (iii % 4)==0)


continue;

7
8

cout<< iii <<endl;

nPrinted++;

10
}

11
12
13

cout<<nPrinted<< " numbers were found"<<endl;

D) While true
- Excutes statements repeatedly until the spectified termination condition evaluates

E) Do/While
- Excutes statements repeatedly until the spectified termination condition the
evaluates to zeros

F) Jump/Loop
- Exucutes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
G) If/else
- An if statements can be followed by an optional else statements , which executes
when the boolen expression if false

You might also like