Wednesday, May 20, 2009

Unconditional Statements

Unconditional statements
Transferring control from one place to another place with out checking any condition is called unconditional statement.

They are:

goto statement

break statement

continue statement


The goto Statement


A goto statements can cause program control to end up almost anywhere in the program unconditionally.
The got statement requires a label to identify the place to move the execution. A label is valid variable name and must be ended with colom.

SYNTAX:

goto label;

. . . . . . . .

. . . . . . . .

label: statements;


EXAMPLE:

goto t;

cout<<” This is goto statement”;


t: cout<<”Good Morning”;


The break Statement

The break statement is used to terminate the loop. When the keyword break is used inside any ‘C’ loop, control automatically transferred to the first statement after the loop. A break is usually associated with an if statement.

SYNTAX:


break;


Example for break statement

void main ()

{
int num;

for (num=1; num<=10; num++)

{

cout <<>

if (num==6)

{

cout << "count aborted";

break;

}

}


}

Output will be

1,2,3,4,5,6 count aborted


The continue Statement


In some situations, we want to take the control to the beginning of the loop, b’ passing the statement inside the loop which have not yet been executed. For this purpose the continue is used. When the statement continue is encountered inside any ‘C’ loop control automatically passes to the beginning of the loop.

SYNTAX:


continue;


Like break statement the continue statements also associated with “if” statement.


Example for Continue Statements


void main ()

{

int num;

for (int num=10; num>0; num--)

{

if (num ==5) continue;

cout <<>

}

cout << "good";


}


Output will be:

10, 9, 8, 7, 6, 4, 3, 2, 1, good



Difference between Break and Continue Statements.


Break Statements


Break statement takes the control
to the outside of the loop
It us also used up switch statement
Always associated with if condition in loops

Continue Statements


Continue statement takes the control to the beginning of the loop
This can be used up loop statement
This is also associated with if condition.

1 comment:

  1. After research a couple of of the blog posts on your web site now, and I actually like your method of blogging. I bookmarked it to my bookmark web site record and will likely be checking again soon. Pls check out my web site as effectively and let me know what you think. online casino slots

    ReplyDelete