Vous êtes sur la page 1sur 7

ASSIGNMENT: C++ PROGRAMMING

QUESTIONS

1. Who is Written C++?

Born December 30, 1950 (age 64)


Aarhus, Denmark
Residence New York City, New York, U.S.[1]
Nationality Danish
InstitutionsAarhus University
University of Cambridge
Texas A&M University
Bell Labs
Morgan Stanley
Columbia University
Alma mater
Aarhus University (MSc)
Churchill College, Cambridge (PhD)
Thesis
Communication and control in distributed computer
systems (1979)
Doctoral advisor David Wheeler
Known for Creating C++
Notable awards
Grace Murray Hopper Award (1993)
IEEE Computer Society Computer Entrepreneur Award (2004)
William Procter Prize for Scientific Achievement (2005)
Dr. Dobb's Excellence in Programming award (2008)

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


Program.
a. Go to

C++ go to statement :
1. Go to statement is used for unconditional jump.
2. Go to statement makes difficult to trace the control flow of program
3. Go to statement should be avoided in order to make smoother program.
Example #1 : Goto Statement :
#include <iostream>
using namespace std;
int main ()
{
int num = 1;
STEP:do
{
if( num == 5)
{
num = num + 1;
goto STEP;
}
cout << "value of num : " << num << endl;
num = num + 1;

}while( num < 10 );


return 0;

}
b. While
A while loop statement repeatedly executes a target statement as long
as a given condition is true.
The syntax of a while loop in C++ is:
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value.
The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line
immediately following the loop.
c. Break and continue
break Statement
In C programming, break is used in terminating the loop immediately
after it is encountered. The break statement is used with conditional if
statement.
Example of break statement
Write a C program to find average of maximum of n positive numbers
entered by user. But, if the input is negative, display the
average(excluding the average of negative input) and end the
program.
/* C program to demonstrate the working of break statement by
terminating a loop, if user inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break;
//for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
continue Statement

It is sometimes desirable to skip some statements inside the loop. In


such cases, continue statements are used.
Example of continue statement
Write a C program to find the product of 4 integers entered by a user. If
user enters 0 skip it.
//program to demonstrate the working of continue statement in C
programming
# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips
the statement product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}

d. While true
I often use this code pattern:
while(true) {
//do something
if(<some condition>) {

break;
}
}
Another programmer told me that this was bad practice and that I
should replace it with the more standard:
while(!<some condition>) {
//do something
}
His reasoning was that you could "forget the break" too easily and have
an endless loop. I told him that in the second example you could just as
easily put in a condition which never returned true and so just as easily
have an endless loop, so both are equally valid practices.
Further, I often prefer the former as it makes the code easier to read
when you have multiple break points, i.e. multiple conditions which get
out of the loop.

e. Do/While
In most computer programming languages, a do while loop is a control
flow statement that executes a block of code at least once, and then
repeatedly executes the block, or not, depending on a given boolean
condition at the end of the block. Note though that unlike most
languages, Fortran's do loop is actually the same as the for loop.
The do while construct consists of a process symbol and a condition.
First, the code within the block is executed, and then the condition is
evaluated. If the condition is true the code within the block is executed
again. This repeats until the condition becomes false. Because do while
loops check the condition after the block is executed, the control
structure is often also known as a post-test loop. Contrast with the
while loop, which tests the condition before the code within the block is

executed, the do-while loop is an exit-condition loop. This means that


the code must always be executed first and then the expression or test
condition is evaluated. If it is true, the code executes the body of the
loop again. This process is repeated as long as the expression
evaluates to true. If the expression is false, the loop terminates and
control transfers to the statement following the do-while loop.
It is possible, and in some cases desirable, for the condition to always
evaluate to true, creating an infinite loop. When such a loop is created
intentionally, there is usually another control structure (such as a break
statement) that allows termination of the loop.
Some languages may use a different naming convention for this type of
loop. For example, the Pascal language has a "repeat until" loop, which
continues to run until the control expression is true (and then
terminates) whereas a "while" loop runs while the control expression
is true (and terminates once the expression becomes false).
f. Jump/loop
The Loop jump is a figure skating jump that takes off from a back
outside edge and lands on the same backwards outside edge. For a
jump with counterclockwise rotation, this is the right back outside
edge. It is named from its similarity to the loop compulsory figure. The
invention is widely credited to Werner Rittberger, and the jump is also
known as the "Rittberger" in Europe. However, evidence exists that it
may have been first done as early as the 1880s.

g. If/else
elseif, as its name suggests, is a combination of if and else. Like else, it
extends an if statement to execute a different statement in case the
original if expression evaluates to FALSE. However, unlike else, it will
execute that alternative expression only if the elseif conditional
expression evaluates to TRUE. For example, the following code would
display a is bigger than b, a equal to b or a is smaller than b:
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";

}
?>
There may be several elseifs within the same if statement. The first
elseif expression (if any) that evaluates to TRUE would be executed. In
PHP, you can also write 'else if' (in two words) and the behavior would
be identical to the one of 'elseif' (in a single word). The syntactic
meaning is slightly different (if you're familiar with C, this is the same
behavior) but the bottom line is that both would result in exactly the
same behavior.
The elseif statement is only executed if the preceding if expression and
any preceding elseif expressions evaluated to FALSE, and the current
elseif expression evaluated to TRUE.

Vous aimerez peut-être aussi