Sie sind auf Seite 1von 15

ASSIGNMENT C++ PROGRAMMING

1. Who is written C++?


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 holds the college of engineering chair in computer science
at Texas a&m university, a visiting professor at Columbia university, and works
at Morgan Stanley. Stroustrup began developing c++ in 1978 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. 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.
He works at texas a & m university as a distinguished professor where he holds the
college of engineering endowed chair in computer science. He is also a visiting
faculty in computer science department at Columbia university. ITMO university
noble doctor since 2013. In 2015, he was made a fellow of the computer history
museum for his invention of the c++ programming language.

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

a ) Go to
The labeled statement designated by identifier must be in the current
function. All identifier names are members of an internal namespace and
therefore do not interfere with other identifiers.
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 );

Outer loop executing. i = 0


Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 1
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 2
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 3
Inner loop executing. j = 0
Jumped to stop. i = 3

b) WHILE
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
In this program, user is asked to enter a positive integer which is stored in
variable number (supposed user entered 4). Here is the working of while loop
in this program:

1.
2.
3.
4.
5.

Initially, i = 1, test expression i <= number is true, factorial becomes 1.


Variable i is updated to 2, test expression is true, factorial becomes 2.
Variable i is updated to 3, test expression is true, factorial becomes 6.
Variable i is updated to 4, test expression is true, factorial becomes 24.
Variable i is updated to 5, test expression is false and while loop is
terminated.
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.
Syntax of do...while Loop

do {
statement/s;
}
while (test expression);

How do...while loop works?


The statement/s inside body of loop is executed at least once, that is, the
statement/s inside braces { } is executed at least once. Then the test
expression is checked. If the test expression is true, the body of loop is
executed. This process continues until the test expression becomes
false. Since the body of loop is placed before the test expression in
do...while loop, the body of loop is executed at least once.

C) BREAK AND CONTINUE


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

In this C++ program, the test expression is always true. The user is asked to
enter a number which is stored in variable number. If the user enters any
number other than 0, that number is added to sumand stored to it and again
user is asked to enter another number. When user enters 0, the test
expression inside if statement is false and body of else is executed which
terminates the loop. Finally, the sum is displayed.

d) WHILE TRUE
In C++,a while loop is a control flow statement that allows code to be executed
repeatedly based on a given boolean condition. The while loop can be thought of as
a repeating if statement.
Syntax
while ( condition ) {
Code to execute while the condition is true
}

Example Program
/* Example Program For While Loop In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP

*/

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
cout<<"Enter the Number :";
cin>>a;
int counter = 1;
//while Loop Block
while (counter <= a)
{
cout<<"Execute While "<<counter<<" time"<<endl;
counter++;
}
// Wait For Output Screen
getch();
return 0;
}

Sample Output:
Enter the Number :4
Execute While 1 time
Execute While 2 time
Execute While 3 time
Execute While 4 time

e ) Do / While
Executes a statement repeatedly until the specified termination condition evaluates
to zero
Syntax
do statement
while ( expression ) ;

Do / While :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 break, goto, or return statement is executed within the statement body.
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 statement:

// do_while_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i++);
} while (i < 3);
}

f) JUMP/LOOP
In C++ a for loop is a programming language statement which allows code to be
repeatedly executed. A for loop is classified as an iteration statement.
Syntax:
for ( variable initialization; condition; variable increment/assignment ) {
Code to execute while the condition is true
}
Example Program
/* Example Program For for Loop In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP

*/

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
cout<<"Enter the Number :";
cin>>a;
//for Loop Block
for (int counter = 1; counter <= a; counter++)
{
cout<<"Execute "<<counter<<" time"<<endl;
}
// Wait For Output Screen
getch();
return 0;
}

Sample Output:
Enter the Number :5
Execute 1 time
Execute 2 time
Execute 3 time
Execute 4 time
Execute 5 time

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.

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

Das könnte Ihnen auch gefallen