Sei sulla pagina 1di 9

C++ Programming

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;

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

of
of
of
of
of
of
of
of
of

a:
a:
a:
a:
a:
a:
a:
a:
a:

10
11
12
13
14
16
17
18
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";

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

Flow Diagram:

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

of
of
of
of
of
of
of
of
of
of

a:
a:
a:
a:
a:
a:
a:
a:
a:
a:

10
11
12
13
14
15
16
17
18
19

C. Break and Continue


The continue statement works somewhat like the break statement. Instead of forcing
termination, however, continue forces the next iteration of the loop to take place, skipping any
code in between.
For the for loop, continue causes the conditional test and increment portions of the loop to
execute. For the while and do...while loops, program control passes to the conditional tests.

Syntax:

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


continue;

Flow Diagram:

Example:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// do loop execution
do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
continue;
}
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
value
value
value
value
value
value
value
value

of
of
of
of
of
of
of
of
of

a:
a:
a:
a:
a:
a:
a:
a:
a:

10
11
12
13
14
16
17
18
19

D. While True
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.

Flow Diagram:

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
}
else if( boolean_expression 2)
{
// Executes when the boolean
}
else if( boolean_expression 3)
{
// Executes when the boolean
}
else
{
// executes when the none of
}

expression 1 is true

expression 2 is true

expression 3 is true

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

Conclusion
In a conclusion I make a decision to trying to the best in c++ .Thank
You to Encik Syukri because pity for we are to do the works.

Potrebbero piacerti anche