Professional Documents
Culture Documents
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.
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"
Relational Operators
Symbol
Meaning
1
2
3
+
*
addition
subtraction
multiplication
2
3
(<)
(>)
(<=)
division
(>=)
equality
5
6
==
!=
Symbol
Meaning
Less than
Greather than
Less than or equal
to
Greather than or
equal to
Equal to
Not equal to
#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;
}
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;
}