+ All Categories
Home > Documents > ASSIGNMENT C++

ASSIGNMENT C++

Date post: 10-Jul-2016
Category:
Upload: sue-ima
View: 17 times
Download: 0 times
Share this document with a friend
Description:
C++
21
ASSIGNMENT : C++ PROGRAMMING 1. Who is Written C++ ? Bjarne Stroustrup born 30 December 1950) is 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, is a visiting professor at Columbia University, and works at Morgan Stanley as a Managing Director in New York. 2. State statements below and give an example application in C++ Program. A ) Go to C++ goto statement Advertisements Previous Page Next Page A goto statement provides an unconditional jump from the goto to a labeled statement in the same function. NOTE: Use of goto statement is highly discouraged because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto. Syntax: The syntax of a goto statement in C++ is: goto label; .. . label: statement;
Transcript
Page 1: ASSIGNMENT C++

ASSIGNMENT : C++ PROGRAMMING

1. Who is Written C++ ?

Bjarne Stroustrup born 30 December 1950) is 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, is a visiting professor at Columbia

University, and works at Morgan Stanley as a Managing Director in New York.

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

A ) Go to

C++ goto statement

Advertisements

Previous Page

Next Page

A goto statement provides an unconditional jump from the goto to a

labeled statement in the same function.

NOTE: Use of goto statement is highly discouraged because it makes

difficult to trace the control flow of a program, making the program hard

to understand and hard to modify. Any program that uses a goto can be

rewritten so that it doesn't need the goto.

Syntax: The syntax of a goto statement in C++ is:

goto label;

..

.

label: statement;

Page 2: ASSIGNMENT C++

Where label is an identifier that identifies a labeled statement. A labeled

statement is any statement that is preceded by an identifier followed by

a colon (:).

Flow Diagram:

Example: #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;

}

Page 3: ASSIGNMENT C++

cout << "value of a: " << a << endl;

a = a + 1;

}while( a < 20 );

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

value of a: 17

value of a: 18

value of a: 19

One good use for the goto is to exit from a deeply nested routine. For

example, consider the following code fragment:

for(...) {

for(...) {

while(...) {

if(...) goto stop;

.

.

.

}

}

}

stop:

cout << "Error in program.\n";

Page 4: ASSIGNMENT C++

Eliminating the goto would force a number of additional tests to be

performed. A simple break statement would not work here, because it

would only cause the program to exit from the innermost loop.

Page 5: ASSIGNMENT C++

B) While

A while loop statement repeatedly executes a target statement as long

as a given condition is true.

Syntax: 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.

Flow Diagram:

Page 6: ASSIGNMENT C++

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

Page 7: ASSIGNMENT C++

C) Break and Continue

C++ break and continue

Statement

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.

Syntax of break

break;

In real practice, break statement is almost always used inside the body of conditional statement(if...else) inside the

loop.

Page 8: ASSIGNMENT C++

Working of break Statement

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() {

Page 9: ASSIGNMENT C++

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;

Page 10: ASSIGNMENT C++

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 sum and stored to it and again user

is asked to enter another number. When user enters 0, the test expression inside ifstatement is false and body of else is

executed which terminates the loop. Finally, the sum is displayed.

Page 11: ASSIGNMENT C++

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.

Working of continue Statement

Page 12: ASSIGNMENT C++

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;

}

Page 13: ASSIGNMENT C++

Output

1 2 3 4 5 7 8 10

In above program, when i is 6 or 9, execution of

statement cout<<i<<"\t"; is skipped inside the loop

using continue; statement.

Page 14: ASSIGNMENT C++

D) While True

I'm curious about using a while statement with a true condition. What is

theadvantage of using a while(true) statement with break to exit the

while statementover something like a for loop? I guess I'm just curious when a

good time to usethis technique would be? I saw it demonstrated in a quick sort

algorithm at schooltoday and was just curious about it. Insight? Example : e) Do

White Thedo while Loop is similar to while loop with one very important

difference. Inwhile loop, check expression is checked at first before body of loop

but in case ofdo...while loop, body of loop is executed first then only test expression

ischecked. That's why the body of do...while loop is executed at least

once.

Page 15: ASSIGNMENT C++

E) Do / While

Do while statements One interesting thing about the while loop is that if the loop condition is initially false, thewhile loop will not execute at all. It is sometimes the case that we know we want a loop toexecute at least once, such as when displaying a menu. To help facilitate this, C++ offersthe do-while loop: dostatement;while (condition);

The statement in a do-while loop always executes at least once. After the statement has been executed, the do-while loop checks the condition. If the condition is true, the CPU jumps back to the top of the do-while loop and executes it again.Here is an example of using a do-while loop to display a menu to the user and wait for theuser to make a valid choice

Page 16: ASSIGNMENT C++

One interestingthing about the above example is that the selection variable must

bedeclared outside of the do block. Why do you think that is?

If the selection variable were to be declared inside the do block, it would be destroyedwhen the do block terminates, which happens before the while conditional is executed.But we need the variable to use in the while conditional -- consequently, the selectionvariable must be declared outside the do block.Generally it is good form to use a do-while loop instead of a while loop when youintentionally want the loop to execute at least once, as it makes this assumption explicit-- however, it’s not that big of a deal either way.

Page 17: ASSIGNMENT C++

F) Jump / Loop

Cause a certain piece of program to be executed a certain number of times. Consider

thesescenarios:-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.

Output :

Page 18: ASSIGNMENT C++

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 the boolean expression evaluates to true, then the if block, of code will be executed,otherwise else block of code will be executed. FLOW DIAGRAM

Page 19: ASSIGNMENT C++

EXAMPLE :

When the above code is compiled and executed, itproduces the following result:

Page 20: ASSIGNMENT C++

The if...elseif...else Statement: An if statement can be followed by an optional else if else statement, which is veryusefull 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

SYANTAX :

The syantax of an if…else if..else statement in C++ is:

Page 21: ASSIGNMENT C++

Example :

When the above code is compiled and executed, it produces the following

result :


Recommended