You are on page 1of 16

ASSIGNMENT : C++ PROGRAMMING

1. Who is Written C++ ?


o 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++ standards committee. Stroustrup 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. He is also a visiting faculty in Computer Science
Department at Columbia University. ITMO University noble doctor since 2013.
In 2015, he was made a Fellow 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
o In C++ programming, go to statement is used for altering the normal sequence of program execution by
transferring control to some other part of the program.
o

SYNTAX OF GO TO STATEMENT
Go to label;
.. .. ..
.. .. ..
.. .
label :
statement;
.. .. ..

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

FORWARD JUMP

BACKWARD JUMP
Example 1 : GO TO STATEMENT

OUTPUT

You can write any C++ program with use of go to statement and it is generally considered good idea not to use go to statement
Reason to Avoid go to Statement
o

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

b. WHILE
o 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 a 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


o 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
o

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(ifelse) inside the loop.

Working of break Statement

Example 1: C++ break


C++ program to add all number entered by user until user enters 0.

Output

In this C++ program, the test expression is always true. The user is asked to enter anumber 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 anothernumber. 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

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.

d. While True
o I'm curious about using a while statement with a true condition. What is theadvantage of using a
while(true) statement with break to exit the while statementover something like a for loop? I guess I'm
just curious when a good time to usethis technique would be? I saw it demonstrated in a quick sort
algorithm at schooltoday and was just curious about it. Insight? Example : e) Do \ White Thedo...while
Loop is similar to while loop with one very important difference. Inwhile loop, check expression is
checked at first before body of loop but in case ofdo...while loop, body of loop is executed first then
only test expression ischecked. That's why the body of do...while loop is executed at least
once. Example :

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

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 dowhile 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 theuser to make a valid choice:

o
o One interesting thing about the above example is that the selection variable must
bedeclared outside of the do block. Why do you think that is?

o If the selection variable were to be declared inside the do block, it would be destroyed when the do

f.

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.

Jump \ Loop
o 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


o An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.
Syntax:
o

The syntax of an if...else statement in C++ is:

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

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


o

An if statement can be followed by an optional else if...else statement, which is very use full 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:
o

The syntax of an if...else if...else statement in C++ is:

Example :

When the above code is compiled and executed, it produces the following result:

You might also like