Sie sind auf Seite 1von 14

ASSIGNMENT C++ PROGRAMING

1:WHO IS WRITTEN C++


Stroustrup also wrote a textbook for the language, The C++ Programming Language.
In 2015, he was made a Fellow of the Computer History Museum for his invention of the
C++ programming language.
2:State statements below and give an example application in c++ program.

a)
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.

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


goto label;
..
.
label: statement;

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 (:).

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;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

b)
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. The condition 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.

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

c)
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.
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

d)
A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used
for this purpose. Since none of the three expressions that form the for loop are required, you can
make an endless loop by leaving the conditional expression empty.
#include <iostream>
using namespace std;

int main ()
{

for( ; ; )
{
printf("This loop will run forever.\n");
}

return 0;
}

When the conditional expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but C++ programmers more commonly use the for(;;) construct to signify
an infinite loop.

E)

do-while Statement (C++)


Executes a statement repeatedly until the specified termination condition (the expression)
evaluates to zero.
do
statement
while ( expression ) ;

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

Example
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 Statements In C#
In this article I am going to explain three jump statements; they are goto, break and
continue.
The jump statements in C# are the statements that allow us to move the program control
from one point to another at any particular instance during the execution of the program.
There are five types of jump statements used in C# as follows:

1. Goto
2. Break

3. Continue
4. Return
5. Throw
Now, let's discuss each of the jump statements (goto, break and continue) in detail one by
one.
1. Goto Statement
The goto statement branches unconditionally from one point to another in the block. In other
words the goto statement transfers execution to another label within the statement block.
The goto requires a label to identify the place where the branch is to be made to. A label is
any valid identifier and must be followed by a colon. The label is placed immediately before
the statement where the control is to be transferred. The goto statement can transfer the
control to any place in a program.
There are two forms of goto statements Forward jump and Backward jump.

For example we create a program that transfers execution to the "displayInfo" label:
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Goto Example Start");
goto displayInfo;
Console.WriteLine("This code skipped code");
displayInfo:
{
Console.WriteLine("It is displayInfo block");

}
}

}
Console.Read();

g)

C++ if...else statement


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

Das könnte Ihnen auch gefallen