You are on page 1of 8

ASSIGNMENT IN C++ PROGRAMMING

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

2. Who is Written C++?


Bjarne Stroustrup

Bjarne Stroustrup (Danish: 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

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

3. Describe the purpose of the following Syntax and Grammar


a) #
: Number Sign

b) # include
: Include files can be "nested"; that is, an #include directive can appear in a file
that's named by another #include directive. For example, file2 could include file3.
In this case, file1 would still be the parent of file2, but it would be the
"grandparent" of file3.

c) Int main()
: The world main is followed in the code by a pair of parentheses () . That is because it is
a function declaration from other types of expressions are these are parentheses that
follow its name. Optionally , these parentheses may enclose a list of parameters within
them.

d) Function
: Function are building blocks of the programs. They make the programs. They make the
programs more modular and easy to read and manage. All C++ programs must contain
the function main (). The execution of the program starts from the function main (). A
C++ program can contain any number of function according to the needs.

e) Cout
: represent the standard output stream in C++ , and the meaning of the entire statement
is to insert a sequence of characters in this case the Hello Word sequence of character
into the standard output stream which usually is the screen

f) <<
: The bitwise left shift (<<) shifts operator bits to the left.

g) 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) \n
: (new line) Moves the active position to the initial
position of the next line.

i) ;
: 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.

j) return 0;
: return 0 means normal exit. return x with x != 0 means termination with "error"

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

4.Give 5 mathematical operators and 6 relational operators:


Mathematical Operators

Relational Operators

Symbol

Meaning

1
2
3

+
*

addition
subtraction
multiplication

2
3

(<)
(>)
(<=)

division

(>=)

equality

5
6

==
!=

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

Symbol

Meaning

Less than
Greather than
Less than or equal
to
Greather than or
equal to
Equal to
Not equal to

5.Statements below and give an example application in C++ Program


a. Go to
: provides an unconditional jump from the go to 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;
}

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

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

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

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

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

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

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

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

Muhammad Fairuz Bin Ilias


Pembentangan Pangkalan Data

You might also like