Sei sulla pagina 1di 9

1.Who is Written C++ ?

Bjarne Stroustrup, a Danish computer scientist, began his work on C++'s predecessor "C with
Classes" in 1979.[6] The motivation for creating a new language originated from Stroustrup's
experience in programming for his Ph.D. thesis. Stroustrup found that Simula had features
that were very helpful for large software development, but the language was too slow for
practical use, while BCPL was fast but too low-level to be suitable for large software
development. When Stroustrup started working in AT&T Bell Labs, he had the problem of
analyzing the UNIX kernel with respect to distributed computing. Remembering his Ph.D.
experience, Stroustrup set out to enhance the C language with Simula-like features.[7] C was
chosen because it was general-purpose, fast, portable and widely used. As well as C and
Simula's influences, other languages also influenced C++, including ALGOL 68, Ada, CLU
and ML.

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


a. The goto statement unconditionally transfers control to the statement labeled by the
specified identifier.

A statement label is meaningful only to a goto statement; otherwise, statement


labels are ignored. Labels cannot be redeclared.
It is good programming style to use the break, continue, and return statements
instead of the goto statement whenever possible. However, because
the break statement exits from only one level of a loop, you might have to use
a goto statement to exit a deeply nested loop.
For more information about labels and the goto statement, see Labeled
Statements and Using Labels with the goto Statement.
In this example, a goto statement transfers control to the point
labeled stop when i equals 3.

// goto_statement.cpp
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 10; i++ )
{
printf_s( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf_s( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
}

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


Thecondition 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.
Here, key point of the while loop is that the loop might not ever run.
When the condition is tested and the result is false, the loop body will be
skipped and the first statement after the while loop will be executed.

Example:
#include <iostream>
using namespace std;

int main ()
{
// Local variable declaration:
int a = 10;

// while loop execution


while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
}

return 0;

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

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;

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.
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;
}

D. 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.
Thecondition 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.

Here, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.
Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}

E. Executes a statement repeatedly until the specified termination condition


(the expression) evaluates to zero.

Syntax
do

statement
while ( expression ) ;

The test of the termination condition is made after each execution of the loop; therefore,
a do-while loop executes one or more times, depending on the value of the termination

expression. The do-while statement can also terminate when a


or return statement is executed within the statement body.

break, goto,

The expression must have arithmetic or pointer type. Execution proceeds as follows:
1. The statement body is executed.
2. Next, expression is evaluated. If expression is false, the do-while statement
terminates and control passes to the next statement in the program.
If expression is true (nonzero), the process is repeated, beginning with step 1.

The following
sample
demonstrates
the do-while stat
ement:
//
do_while_statement
.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i+
+);
} while (i <
3);
}

F. Jump/Loop
A C++ jump statement performs an immediate
local transfer of control.
SYNTAX

break;
continue;
return [expression];
goto identifier;

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
}

If the boolean expression evaluates to true, then the if block of code


will be executed, otherwise else block of code will be executed.

Potrebbero piacerti anche