Statements that transfers control from on part of the program to another part of a program or a function unconditionally are known as unconditional control statement. There are different unconditional statement in c++ are:
- goto
- break
- continue
goto :
goto statement is used for unconditional branching or transfer of the program execution to the labelled statement. The goto statement is to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name, and must be followed by colon. The is placed immediately before the statement where the control is to be transferred.
General form of goto statement:
Goto label;
——-
——-
——-
label:
statement;
Label can be placed anywhere in the program either before or after the goto statement.
Example:
# include<iostream.h>
int main()
{
int i,sum = 0,n;
count<<”Enter N”;
cin>>n;
i=1;
L1:
sum = sum+I;
i++;
if(i<=n)
goto L1;
count<<”Sum is”<<sum;
return 0 ;
}
break :
Break statement is encountered within a loop, a loop is immediately exited an the program continues with the statement immediately following loop. In simple we words we can say that when we want to stop the iteration we use break statement.
Example:
# include<iostream.h>
int main()
{
int i,sum=0,n;
cout<<”Enter Number”;
cin>>n;
i=1;
L1:
sum = sum+i;
i++;
if(i>n)
break;
goto L1;
count<<”Sum is”<<sum;
return 0;
}
continue
It is used to continue the iteration of the loop statement by skipping the statements after continue statement. It causes the control to go directly to the test condition and then to continue the loop. In simple we can say that it is used for skipping the particular number in loops or statement.
Example :
#include<iostream.h>
int main()
{
int sum=0,i,n;
cout<<”Enter number”;
cin>>n;
i=1;
L1:
sum = sum+i;
i++;
if(i>n)
break;
goto L1;
cout<<”Sum is”<<sum;
return0;
}
Pingback: do-While loop in C++ - AskAtul.com
Pingback: Classes in C++ - AskAtul.com