Você está na página 1de 7

ASSIGNMENT : C++ PROGRAMMING

QUESTIONS
1. Who is written C++

Bjarne Stroustrup
Bjarne Stroustrup is a Danish computer scientist, most notable for
the creation and development of the widely used C++ programming
language. He is a Distinguished Research Professor and holdsAt the
College of Engineering Chair in Computer Science at Texas A&M
University, a visiting professor at Columbia University, and works
at Morgan Stanley.
Born at December 30, 1950 (age 64), Aarhus, Denmark.
Education learning at Aarhus University, Churchill College,
Cambridge, University of Cambridge.
Live at New York City, New York, United States.
Has been reward a Grace Murray Hopper Award.

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


program.
a) Go to

A go to statement provides an unconditional jump from


the go to a labelled statement in the same function.
#include <iostream>
using namespace std;

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

// do loop execution
LOOP:do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
goto LOOP;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

b) WHILE
A while loop statement repeatedly executes a target
statement as long as a given condition is true.

#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) BREAK AND CONTINUE


I. CONTINUE

The continue statement works somewhat like the break


statement. Instead of forcing termination, however, continue
forces the next iteration of the loop to take place, skipping
any code in between.

#include <iostream>
using namespace std;

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

// do loop execution
do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

II. BREAK
The break statement has the following two usages in C++:

When the break statement is encountered inside a loop, the


loop is immediately terminated and program control resumes
at the next statement following the loop.
It can be used to terminate a case in the switch statement
(covered in the next chapter).
If you are using nested loops (i.e., one loop inside another loop), the break
statement will stop the execution of the innermost loop and start executing the
next line of code after the block.
#include <iostream>
using namespace std;

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

// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
{
// terminate the loop
break;
}
}while( a < 20 );

return 0;
}

d) WHILE TRUE
unconditional loop

while( true ) {
doSomething();
if( condition() ) {
break;
}
doSomethingElse();
}

e) DO / WHILE
Unlike for and while loops, which test the loop condition at
the top of the loop, the do...while loop checks its condition at
the bottom of the loop.
A do...while loop is similar to a while loop, except that a
do...while loop is guaranteed to execute at least one time.
The syntax of a do...while loop in C++ is:
// continue_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
i++;
printf_s("before the continue\n");
continue;
printf("after the continue, should never print\n");
} while (i < 3);
printf_s("after the do loop\n");
}

f) JUMP / LOOP
A C++ jump statement performs an immediate local transfer of
control.
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.
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
}

Você também pode gostar