Statements of C++ Programming:-
Following statements are used in C++, normally inside the loops.
1. Continue Statement.
2. Break Statement.
3. Goto Statement.
Continue Statement:-
Continue statement is used inside the loop when we normally there is no need to display the specific output until the condition remains true. This statement does not execute the statements which are after this in the block of statements and transfer the control to the new iteration in the loop.
Syntax of Continue statement:-
Continue; (inside the body of a loop)
Example of a Continue Statement:-
#include<iostream.h> #include<conio.h> main() { for(int z=0; z<6; z++) { cout<<" The User is a very good programmer :"<<endl; continue; cout<<" The User is a very bad programmer :"<<endl; } getch(); }
Due to the continue statement, the statement “The user is a very bad programmer “Never executes because continue statement transfer the control to the next iteration.
Break Statement:-
In C++ the break statement is purely used to terminate the loop and completely transfer the control from the loop to the next written statement after the body of a loop. It can also use to stop executing further statements inside the body of conditional statements.
Syntax of Break statement:-
Break; (Inside the loops body)
Example of a Break Statement:-
#include<iostream.h> #include<conio.h> main() { for(int y=0; y<5; y++) { cout<<" The User is a very good programmer :"<<endl; break; } cout<<" The User is a very bad programmer :"<<endl; getch(); } [sourcecode language="C"] As you can see “The user is a good programmer” have been printed only one time and after that the statement “The user is not a good programmer” is printed because during first iteration due to break statement loop runs only one time. <strong>Consider another example:-</strong> [sourcecode language="C"] #include<iostream.h> #include<conio.h> main() { for(int y=0; y<3; y++) { cout<<" The User is a very good programmer :"<<endl; if(y==3) { cout<<" The User is a very bad programmer :"<<endl; break; cout<<" It was a Mistake :"<<endl; } } getch(); }
As you can see when the value of y becomes 3 the statement “The user is a very bad programmer” executes but due to break statement the statement “It was a mistake” never executed.
Goto Statement:-
In C++ Goto statement is used to transfer the control to anywhere in the program. This is an unconditional statement and should be used when you do not have any other choice.
Syntax of a Goto Statement:-
Goto A;
A:
{
Statements
};
Example of a Goto statement:-
#include<iostream.h> #include<conio.h> main() { for(int y=0; y<3; y++) { cout<<" The User is a very good programmer :"<<endl; if(y==3) { cout<<" It was a Mistake :"<<endl; goto a; } } cout<<endl; a: { cout<<" It is a result of goto statement "; } getch(); }
Let us consider the following example which elaborates the use of Loops as well as the break statement, switch statement and Goto statement:-
Let’s take a look towards the last example which elaborates the use of loops as well as the break statement, switch statement and Goto statement.
Example:-
//This program displays a Console Based Calculator // #include<iostream.h> #include<conio.h> main() { //initilize and declare variables and chracters double x,y; char z,u ; float k ; //Construct a do while loop for repetion do { //Take inputs from the user cout<<"Please enter the first number :" ; cin>>x; cout<<"Please enter the second number :" ; cin>>y; //Display the selection menu cout<<"Main Menu \n" ; cout<<"Enter 'a' for Addition \n" ; cout<<"Enter 's' for Subtraction \n" ; cout<<"Enter 'm' for Multiplication \n" ; cout<<"Enter 'd' for Division \n" ; cout<<"Enter 'e' for Exit \n" ; cin>>z ; k=x/y; //initlize the switch statement switch (z) { case('a'): cout<<"Answer ="<<x+y ; break; case('s'): cout<<"Answer ="<<x-y ; break; case ('m'): cout<<"Answer ="<<x*y; break; case('d'): cout<<"Answer ="<<k; break; case ('e'): //initilize the goto statement in order to close the program goto q; } cout<<"\n"; cout<<"if you want to do more calculations then press 'y' " ; cin>>u; } while(u=='y'); q: cout<<"press any key to close the program" ; getch(); }
Prime numbers Program:-
This code will show how to design logic for the program of prime numbers.
#include<iostream> #include<conio.h> using namespace std; bool prime(int); main() { int a; cout<<"Enter an interger greater then 1: "; cin>>a; if(prime(a)) cout<<a<< " is a prime Number "<< endl; else cout<<a<<" is not a prime Number"; getch(); } bool prime(int z) { for(int i=2; i<z; i++) { if(z%i==0) return false; } return true; }
[yop_poll id=”12″] If you like my work and wanted to read my other articles then visit my Homepage.
@ 2014 HellGeeks.com