You are on page 1of 15

ASSINGMENT C++

MOHAMMAD NAZRUL HAD BIN MOHAMAD AMRAN

MAY 4, 2016
IT-SKN-1A
INSTITUT KEMAHIRAN MARA LUMUT

1. Who is Written C++?

Stroustrup began developing C++ in 1978


(then called "C with Classes"), and, in his own
words, "invented C++, wrote its early definitions,
and produced its first implementation... chose and
formulated the design criteria for C++, designed all
its major facilities, and was responsible for the
processing of extension proposals in the C++

Stroustrup

standards committee."[11]
also
wrote a textbook for the language, The C++
Programming Language.

Stroustrup was the head of AT&T Bell Labs' Large-scale Programming


Research department, from its creation until late 2002. Stroustrup was elected
member of the National Academy of Engineering in 2004. He is a Fellow of the
ACM (1994) and an IEEE Fellow. He works at Texas A&M University as a
Distinguished Professor where he holds the College of Engineering Endowed
Chair in Computer Science.[12][13] He is also a visiting faculty in Computer
Science Department at Columbia University.[14] ITMO University noble doctor
since 2013[15]
In 2015, he was made a Fellow [16] of the Computer History Museum for his
invention of the C++ programming language.

2. State statements below and give an example application


in C++ Program.

a. C++ go to Statement
In C++ programming, goto statement is used for altering the normal sequence of program
execution by transferring control to some other part of the program.

Syntax of goto Statement


goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...

In syntax above, label is an identifier. When goto label; is encountered, the control of
program jumps to label: and executes the code below it.

Example 1: goto Statement


/* C++ program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* If user enters negative number, it ignores that number and
calculates the average of number entered before it.*/
# include <iostream>
using namespace std;
int main() {
float num, average, sum = 0.0;
int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;
for(i=1; i <= n; ++i) {
cout<<"Enter n"<<i<<": ";
cin>>num;
if(num < 0.0) {
goto jump; /* Control of the program moves to jump; */
}
sum += num;
}
jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return 0;
}

Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95

You can write any C++ program with use of goto statement and it is generally considered good idea
not to use goto statement.

Reason to Avoid goto Statement


The goto statement gives power to jump to any part of program but, makes the logic of the program
complex and tangled. In modern programming, goto statement is considered a harmful construct and a
bad programming practice.
The goto statement can be replaced in most of C++ program with the use of break and continue
statements.

b. While
While ( condition ) { Code to execute while the condition is true } The true
represents a boolean expression which could be x == 1 or while ( x != 7 ) (x
does not equal 7). It can be any combination of Boolean statements that are
legal. Even, (while x ==5 || v == 7) which says execute the code while x
equals five or while v equals 7. Notice that a while loop is the same as a for
loop without the initialization and update sections. However, an empty
condition is not legal for a while loop as it is with a for loop. Example : c)
Break and Continue It causes the execution flow to jump around and because
it can make the flow of logic harder to follow Example :

c. C++ break and continue Statement


There are two statements (break; and continue;) built in C++ programming to alter
the normal flow of program.
Loops 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
checking test condition. On these type of scenarios, continue; statement and break;
statement is used respectively. The break; statement is also used to terminate switch
statement.

C++ break Statement


The break; statement terminates the loop(for, while and do..while loop) and switch
statement immediately when it appears.

Syntax of break
break;

In real practice, break statement is almost always used inside the body of conditional
statement(if...else) inside the loop.

Working of break Statement

Example 1: C++ break


C++ program to add all number entered by user until user enters 0.
// C++ Program to demonstrate working of break statement
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
while (true) {
// test expression is always true
cout<<"Enter a number: ";
cin>>number;
if (number != 0.0) {
sum += number;
}
else {
break;
// terminating the loop if number equals to 0.0
}
}
cout<<"Sum = "<<sum;
return 0;
}

Output
Enter
Enter
Enter
Enter
Enter
Sum =

a number:
a number:
a number:
a number:
a number:
9.6

4
3.4
6.7
-4.5
0

In this C++ program, the test expression is always true. The user is asked to enter a
number which is stored in variable number. If the user enters any number other than 0,
that number is added to sum and stored to it and again user is asked to enter another
number. When user enters 0, the test expression inside if statement is false and body of
else is executed which terminates the loop. Finally, the sum is displayed.

C++ continue Statement


It is sometimes necessary to skip some statement/s inside the loop. In that case,
continue; statement is used in C++ programming.

Syntax of continue
continue;

In practice, continue; statement is almost always used inside conditional statement.

Working of continue Statement

Example 2: C++ continue


C++ program to display integer from 1 to 10 except 6 and 9.
// C++ Program to demonstrate working of continue statement
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
if ( i == 6 || i == 9) {
continue;
}
cout<<i<<"\t";
}
return 0;
}

Output
1

10

In above program, when i is 6 or 9, execution of statement cout<<i<<"\t"; is skipped


inside the loop using continue; statement.

d. While True
I'm curious about using a while statement with a true condition. 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? Example : e) Do \ White The
do...while Loop is similar to while loop with one very important difference. In
while loop, check expression is checked at first before body of loop but in case of
do...while loop, body of loop is executed first then only test expression is
checked. That's why the body of do...while loop is executed at least once.
Example :

e. Do while statements
One interesting thing about the while loop is that if the loop condition is initially false, the
while loop will not execute at all. It is sometimes the case that we know we want a loop to
execute at least once, such as when displaying a menu. To help facilitate this, C++ offers
the do-while loop:
do
statement;
while (condition);

The statement in a do-while loop always executes at least once. After the statement has
been executed, the do-while loop checks the condition. If the condition is true, the CPU
jumps back to the top of the do-while loop and executes it again.
Here is an example of using a do-while loop to display a menu to the user and wait for the
user to make a valid choice:
1 #include <iostream>
2
3 int main()
4 {
5
// selection must be declared outside do/while loop
6
int selection;
7
8
do
9
{
10
std::cout << "Please make a selection: \n";
11
std::cout << "1) Addition\n";
12
std::cout << "2) Subtraction\n";
13
std::cout << "3) Multiplication\n";
14
std::cout << "4) Division\n";
15
std::cin >> selection;
16 }
17 while (selection != 1 && selection != 2 &&
18
selection != 3 && selection != 4);
19
20 // do something with selection here
21 // such as a switch statement
22
23 std::cout << "You selected option #" << selection << "\n";
24
25 return 0;
26 }
One interesting thing about the above example is that the selection variable must be
declared outside of the do block. Why do you think that is?

If the selection variable were to be declared inside the do block, it would be destroyed
when the do block terminates, which happens before the while conditional is executed.
But we need the variable to use in the while conditional -- consequently, the selection
variable must be declared outside the do block.
Generally it is good form to use a do-while loop instead of a while loop when you
intentionally want the loop to execute at least once, as it makes this assumption explicit
-- however, its not that big of a deal either way.

f. Jump \ Loop
Cause a certain piece of program to be executed a certain number of times. Consider these
scenarios:
-You want to execute some code/s certain number of time.
-You want to execute some code/s certain number of times depending upon input from user.
Example :

Output :

g. C++ if/else statement


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
}

If the boolean expression evaluates to true, then the if block of code will be executed,
otherwise else block of code will be executed.

Flow Diagram:

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

The if...else if...else Statement:


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.
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.
Once an else if succeeds, none of he remaining else if's or else's will be tested.

Syntax:
The syntax of an if...else if...else statement in C++ is:
if(boolean_expression 1)
{
// Executes when the boolean
}
else if( boolean_expression 2)
{
// Executes when the boolean
}
else if( boolean_expression 3)
{
// Executes when the boolean
}
else
{
// executes when the none of
}

expression 1 is true

expression 2 is true

expression 3 is true

the above condition 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

You might also like