Assignment: C++ PROGRAMMING: Bjarne Stroustrup
Assignment: C++ PROGRAMMING: 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.
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.
if ( i == 3 )
goto stop;
}
// This message does not print:
Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
When the above code is compiled and executed, it produces the following
result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
if (number != 0.0) {
sum += number;
}
else {
break; // terminating the loop if number equals to 0.0
}
}
cout<<"Sum = "<<sum;
return 0;
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
are used to perform repetitive task until test expression is false but sometimes it is
desirable to skip some statement/s inside loop or terminate the loop immediately with
7 8
10
D ) While True
I'm curious about using a while statement with a truecondition. What is the
advantage of using a while(true) statement with break to exit the while
statement over something like a for loop? I guess I'm just curious when a good
time to use this technique would be? I saw it demonstrated in a quick sort
algorithm at school today and was just curious about it. Insight?
Edit: In my Java course we were taught that using break was a bad habit.
Seems like this code violates something or is it acceptable to use break often in
C++.
1 #include <iostream>
2 using namespace std;
3
4 int main (int argc, char * const argv[]) {
5
6
int counter = 9;
7
8
while (true) {
9
10
cout << "Counter: " << counter << endl;;
11
counter--;
12
13
if (counter == 0) {
14
break;
15
}
16
17
18
19
20 }
21
}
return 0;
E ) Do / While
Executes a statement repeatedly until the specified termination condition
evaluates to zero
Syntax
do
statement
while ( expression ) ;
F ) Jump / Loop
A simple C++ statement is each of the individual instructions of a program, like
the variable declarations and expressions seen in previous sections. They
always end with a semicolon (;), and are executed in the same order in which
they appear in a program.
But programs are not limited to a linear sequence of statements. During its
process, a program may repeat segments of code, or take decisions and
bifurcate. For that purpose, C++ provides flow control statements that serve to
specify what has to be done by our program, when, and under which
circumstances.
Many of the flow control statements explained in this section require a generic
(sub)statement as part of its syntax. This statement may either be a simple C+
+ statement, -such as a single instruction, terminated with a semicolon (;) - or
a compound statement. A compound statement is a group of statements (each
of them terminated by its own semicolon), but all grouped together in a block,
enclosed in curly braces: {}:
{ statement1; statement2; statement3; }
G ) If / Else
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
Syntax:
The syntax of an if...else statement in C++ is:
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}
Example:
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
// check the boolean condition
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following
result:
a is not less than 20;
value of a is : 100
When using if , else if , else statements there are few points to keep in
mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Syntax:
The syntax of an if...else if...else statement in C++ is:
if(boolean_expression 1)
{
// Executes when the boolean expression 1 is true
}
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;
When the above code is compiled and executed, it produces the following
result:
Value of a is not matching
Exact value of a is : 100