You are on page 1of 14

ASSIGNMENT: C++ PROGRAMMING

QUESTIONS
1.

Who is written C++ ?

Bjarne Stroustrup is written C++.He born on December 30, 1950 is a computer


scientist and creator of the C++ programming language.He is a Danish computer scientist,
most notable for the creation and development of the widely used C++ programming
language.[4] He is a Distinguished Research Professor and holds the College of Engineering
Chair in Computer Science at Texas A&M University,[5] is a visiting professor at Columbia
University, and works at Morgan Stanley as a Managing Director in New York.[6][7][8]

Educations of Bjarne Stroustrup isStroustrup has a master's


degree in mathematics and computer science (1975) from Aarhus University, Denmark, and
a Ph.D. in computer science (1979) from the University of Cambridge, England.[5] His thesis
advisor in Cambridge was David Wheeler.[9][10]
Career of him isStroustrup 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."[11] 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 IEEEFellow. 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]0

2.

State statements below and give an example application in C++Program.

A.

Go to :-

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

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

B.

While

There are 3 types of loops in C++ Programming:


for Loop
while Loop
do...while Loop
The for loop
The for loop loops from one number to another number and increases by a specified value each time.
The for loop uses the following structure:

for (Start value; continue or end condition; increase value)


statement;

Look at the example below:

#include<stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf ("Hello\n");
printf ("World\n");
}
return 0;
}

The while loop


The while loop can be used if you dont know how many times a loop must run. Here is an example:

#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
while ( counter < howmuch)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
Lets take a look at the example: First you must always initialize the counter before the while loop starts (
counter = 1). Then the while loop will run if the variable counter is smaller then the variable howmuch. If
the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to
increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.

As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix
incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment
while loop example:

#include<stdio.h>
int main(void) {
int i;
i = 0;
while(i++ < 5) {
printf("%d\n", i);
}
printf("\n");
i = 0;
while(++i < 5) {
printf("%d\n", i);
}
return 0;
}
The output of the postfix and prefix increment example will look like this:

1
2
3
4
5
1
2
3
4
i++ will increment the value of i, but is using the pre-incremented value to test against < 5.
Thats why we get 5 numbers.
++i will increment the value of i, but is using the incremented value to test against < 5. Thats why we get
4 numbers.

The do while loop


The do while loop is almost the same as the while loop. The do while loop has the following form:

do
{
do something;
}
while (expression);
Do something first and then test if we have to continue. The result is that the loop always runs once.
(Because the expression test comes afterward). Take a look at an example:

#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
do
{
counter++;
printf("%d\n", counter);
}
while ( counter < howmuch);
return 0;
}
Note: There is a semi-colon behind the while line.

C.

Break and Continue

Break and continue


To exit a loop you can use the break statement at any time. This can be very useful if you want to stop
running a loop because a condition has been met other than the loop end condition. Take a look at the
following example:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
if ( i == 10)
break;

}
return 0;
}
In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an
if statement that states that if i equals ten the while loop must stop (break).
With continue; it is possible to skip the rest of the commands in the current loop and start from the
top again. (the loop variable must still be incremented). Take a look at the example below:

#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
continue;
printf("Nothing to see\n");
}
return 0;
}

In the example above, the printf function is never called because of the continue;.

D.

While True

These loops are used when one wants to loop forever and the breaking out condition from loop is not known.
Certiain conditions are set inside the loop along with either break or return statements to come out of the
loop. For example:
while(true){
//run this code
if(condition satisfies)
break;
//return;
}

These loops are just like any other while loop with condition to stop the loop is in the body of the
while loop otherwise it will run forever (which is never the intention of a part of the code until required so). It
depends upon the logic of the programmer only what he/she wants to do.

E.

Do / While

Difference between while and do-while loop?


->do-while loop is similar to while loop, however there is one basic difference between them do-while runs at
least one even if the test condition is false at first place. lets understand this with an example
Using while loop:
main()
{
int i=0
while(i==1)
{
printf("while vs do-while");
}
printf("Out of loop");
}

Output:
Out of loop

Same example using do-while loop


main()
{
int i=0
do
{
printf("while vs do-while\n");
}while(i==1);
printf("Out of loop");
}

Output:
while vs do-while
Out of loop

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 :
Enter a positive integer : 5
Factorial of 5 = 120

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 ifelse statement in C++ is :
If (Boolean_expression)
{
// statement (s) will excute 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.

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 expression 1 is true
}
else if( boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
else if( boolean_expression 3)
{
// Executes when the boolean expression 3 is true
}
else
{
// executes when the none of 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

Ghg

You might also like