You are on page 1of 15

Assignment: C++ PROGRAMMING

1.Who is written c++?

Bjarne Stroustrup
(Danish: born 30 December 1950) 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.

C++ is an object oriented programming (OOP) language, developed by Bjarne Stroustrup, and is
an extension of C language. It is therefore possible to code C++ in a "C style" or "object-oriented
style." In certain scenarios, it can be coded in either way and is thus an effective example of a
hybrid language.
C++ is a general purpose object oriented programming language. It is considered to be an
intermediate level language, as it encapsulates both high and low level language features.
Initially, the language was called 'C with classes as it had all properties of C language with an
additional concept of 'classes. However, it was renamed to C++ in 1983.
It is pronounced "C-Plus-Plus.

State statements below and give an example


application in c ++ program
2.

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

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
value of a: 15
value of a: 16
value of a: 17

value of a: 18
value of a: 19

C ) Break and Continue


There are two statements (break; and continue;) built in C++ programming to alter
the normal flow of program.
Loops 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.
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;

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

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.

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
I'm curious about using a while statement with a truecondition. What is the
advantage of using a while(true) statement with break to exit the while
statement over something like a for loop? I guess I'm just curious when a good
time to use this technique would be? I saw it demonstrated in a quick sort
algorithm at school today and was just curious about it. Insight?
Edit: In my Java course we were taught that using break was a bad habit.
Seems like this code violates something or is it acceptable to use break often in
C++.
1 #include <iostream>
2 using namespace std;
3
4 int main (int argc, char * const argv[]) {
5
6
int counter = 9;
7
8
while (true) {
9
10
cout << "Counter: " << counter << endl;;
11
counter--;
12
13
if (counter == 0) {
14
break;
15
}

16
17
18
19
20 }
21

}
return 0;

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 dowhile 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
A simple C++ statement is each of the individual instructions of a program, like
the variable declarations and expressions seen in previous sections. They
always end with a semicolon (;), and are executed in the same order in which
they appear in a program.
But programs are not limited to a linear sequence of statements. During its
process, a program may repeat segments of code, or take decisions and
bifurcate. For that purpose, C++ provides flow control statements that serve to
specify what has to be done by our program, when, and under which
circumstances.
Many of the flow control statements explained in this section require a generic
(sub)statement as part of its syntax. This statement may either be a simple C+
+ statement, -such as a single instruction, terminated with a semicolon (;) - or
a compound statement. A compound statement is a group of statements (each
of them terminated by its own semicolon), but all grouped together in a block,
enclosed in curly braces: {}:
{ statement1; statement2; statement3; }

The entire block is considered a single statement (composed itself of multiple

substatements). Whenever a generic statement is part of the syntax of a flow


control statement, this can either be a simple statement or a compound
statement.

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

You might also like