Vous êtes sur la page 1sur 11

Assignment C++ PROGRAMMING

1. Bjarne Stroustrup, a Danish computer scientist.

2. State statement below.


a. Go to

The main purpose of go to statements is to have one cleanup section


in routine rather than multiple return statements. Like below
b.BOOL foo()
c. {
d. BOOL bRetVal = FALSE;
e. int *p=NULL;
f.
g. p = new int;
h. if(p==NULL)
i. {
j.
cout<<" OOM \n";
k.
goto Exit;
l. }
m.
n. // Lot of code...
o.Exit:
p. if(p)
q. {
r.
delete p;
s.
p= NULL;
t. }
u. return bRetVal;
v. }

This makes much easier as we can track our clean up code at one
section in code.ie after Exit label.

b. While

Here, statement(s) may be a single statement or a block of


statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line
immediately following the loop.

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

When the above code is compiled and executed, it produces the following
result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14

c. Break and Countinue


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

The break; statement terminates the loop(for, while and do..while


loop) and switch statement immediately when it appears.

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;

Output

Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6

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.

Example 2: C++ continue


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;
}
Output
1

7 8

10

d. While True
This is infinite loop. We must change the word in bracket.
while( true ) {
doSomething();
if( condition() ) {
break;
}
doSomethingElse();
}

e. Do / While

C++ while and do...while Loop In

computer programming, 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.
These types of task can be solved in programming using loops.
There are 3 types of loops in C++ Programming:

for Loop

while Loop

do...while Loop

How while loop works in C++ Programming?


The while loop checks whether the test expression is true or not. If it is true,
code/s inside the body of while loop is executed,that is, code/s inside the
braces { } are executed. Then again the test expression is checked whether
test expression is true or not. This process continues until the test expression
becomes false.

Example 1: C++ while Loop


C++ program to find factorial of a positive integer entered by user.
(Factorial of n = 1*2*3...*n)
#include <iostream>
using namespace std;
int main() {
int number, i = 1, factorial = 1;
cout<< "Enter a positive integer: ";
cin >> number;
while ( i <= number) {
factorial *= i;
//factorial = factorial * i;
++i;
}

cout<<"Factorial of "<<number<<" = "<<factorial;


return 0;

Output
Enter a positive integer: 4
Factorial of 4 = 24

C++ do...while Loop


The do...while Loop is similar to while loop with one very important difference.
In while loop, check expression is checked at first before body of loop but in
case of do...while loop, body of loop is executed first then only test expression
is checked. That's why the body of do...while loop is executed at least once

Example 2: do...while Loop


C++ program to add numbers entered by user until user enters 0.
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);
cout<<"Total sum = "<<sum;
}

return 0;

Output
Enter a number: 4.5
Enter a number: 2.34
Enter a number: 5.63
Enter a number: 6.34
Enter a number: 4.53
Enter a number: 5
Enter a number: 0

Total sum = 28.34

f. Jump/loop

Jump/loop
Jump statements allow altering the flow of a program by performing jumps to specific locations.

The break statement


break leaves a loop, even if the condition for its end is not fulfilled. It can
be used to end an infinite loop, or to force it to end before its natural end.
For example, let's stop the countdown before its natural end:
1 // break loop example
2 #include <iostream>
3 using namespace std;
4
5 int main ()
6{
7 for (int n=10; n>0; n--)
8 {
9
cout << n << ", ";
10
if (n==3)
11
{
12
cout << "countdown aborted!";
13
break;
14
}
15 }
16 }

10, 9, 8, 7, 6, 5, 4, 3, countdown
aborted!

The continue statement


The continue statement causes the program to skip the rest of the loop in
the current iteration, as if the end of the statement block had been
reached, causing it to jump to the start of the following iteration. For
example, let's skip number 5 in our countdown:
1 // continue loop example
2 #include <iostream>
3 using namespace std;
4
5 int main ()
6{

10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!

7 for (int n=10; n>0; n--) {


8
if (n==5) continue;
9
cout << n << ", ";
10 }
11 cout << "liftoff!\n";
12 }

The goto statement


goto allows to make an absolute jump to another point in the program.
This unconditional jump ignores nesting levels, and does not cause any
automatic stack unwinding. Therefore, it is a feature to use with care, and
preferably within the same block of statements, especially in the presence
of local variables.
The destination point is identified by a label, which is then used as an
argument for the goto statement. A label is made of a valid identifier
followed by a colon (:).
goto is generally deemed a low-level feature, with no particular use cases
in modern higher-level programming paradigms generally used with C++.
But, just as an example, here is a version of our countdown loop using
goto:
1 // goto loop example
2 #include <iostream>
3 using namespace std;
4
5 int main ()
6{
7 int n=10;
8 mylabel:
9 cout << n << ", ";
10 n--;
11 if (n>0) goto mylabel;
12 cout << "liftoff!\n";
13 }

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!

g. If/Else

If/Else Statement
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
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;
}

Vous aimerez peut-être aussi