You are on page 1of 8

ASSIGNMENT C++ PROGRAMMING

1. Who is written C++

Bjarne stroustrup is the person who written the C++. He was born on 30
December 1950 and he is the computer scientist. His most notable for
the creation and development is the C++ programming that is widely used
now. He is a Distinguished Research Professor and holds the College of
Engineering Chair in Computer Science at Texas A&M University. He is
also a visiting professor at Columbia University and works at Morgan
Stanley.

2. State statements below and give an example applications in


C++ Program.
a) Go to
The go to statement unconditionally transfers control to the
statement labelled by the specified identifier. It is good

programming style to use the break, continue,


and return statements instead of the go to statement whenever
possible. However, because the break statement exits from only
one level of a loop, you might have to use a go to statement to exit
a deeply nested loop.
Example:
void foo()
{
if (!doA())
goto exit;
if (!doB())
goto cleanupA;
if (!doC())
goto cleanupB;
/* everything has succeeded */
return;
cleanupB:
undoB();
cleanupA:
undoA();
exit:
return;
}

b) While
A while loop statement repeatedly executes a target statement as
long as a given condition is true. 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) Break and Continue


It needs a fuller treatment since it can be used with other types
of loops as well. The break statement causes a do, for, switch, or
while statement to terminate. This includes for each loops included
in C++. There is three type of breaking that is breaking a switch,
breaking a loop and break vs return.
Example:

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

d) While True
This is commonly called "infinite loop". Yet it is not technically
infinite - it will stop once control flows through break.
Example:
.section __TEXT,__text,regular,pure_instructions
.globl _main
.align 4, 0x90
_main:
## @main

.cfi_startproc
## BB#0:
pushq %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
movl $0, -4(%rbp)
LBB0_1:
## =>This Inner Loop Header: Depth=1
jmp LBB0_2
LBB0_2:
## in Loop: Header=BB0_1 Depth=1
movb $1, %al
testb $1, %al
jne LBB0_1
jmp LBB0_3
LBB0_3:
movl $0, %eax
popq %rbp
ret
.cfi_endproc

e) 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, go to,
or return statement is executed within the statement body.
Example:

#include <iostream>
using namespace std;

int main ()
{
// Local variable declaration:
int a = 10;

// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );

return 0;
}

f) Jump / loop
A C++ jump statement performs an immediate local transfer of
control. Use the for statement to construct loops that must
execute a specified number of times. The for statement consists
of three optional parts that is (init-expression, cond-expression
and loop-expression)
Example:

#include <stdio.h>
int main()
{
int i = 0;
do
{
i++;
printf_s("before the continue\n");
continue;
printf("after the continue, should never print\n");
} while (i < 3);
printf_s("after the do loop\n");
}

g) If / else
The value of expression is nonzero, statement1 is executed. If the
optional else is present, statement2 is executed if the value
of expression is zero. Expression must be of arithmetic or pointer
type, or it must be of a class type that defines an unambiguous
conversion to an arithmetic or pointer type. (For information about
conversions, see Standard Conversions.)In both forms of
the if statement, expression, which can have any value except a

structure, is evaluated, including all side effects. Control passes


from the if statement to the next statement in the program unless
one of the statements contains a break, continue, or go to.
The else clause of an if...else statement is associated with the
closest previous if statement in the same scope that does not have
a corresponding else statement.
Example:
// if-else statement
if (condition)
{
then-statement;
}
else
{
else-statement;
}
// Next statement in the program.

// if statement without an else


if (condition)
{
then-statement;
}
// Next statement in the program.

You might also like