C++ Programming में Statement के सात प्रकार है
- if Statement
- if_else Statement
- else_if Statement
- switch case Statement
- break Statement
- continue Statement
- goto Statement
1. if Statement
- if Statement में अगर Condition true होती है तब Statement Execute होता है |
Syntax for if Statement
if(condition){statement(s);
}
for eg.
int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}
Example for if Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if (a < b)
{
cout<<"a is less than b";
}
return 0;
}
Output :
a is less than b
2. if_else Statement
- if_else Statement में अगर Condition true हो तो वो if का statement Execute करता है | अगर Condition false होती है तो else का statement execute करता है |
Syntax for if_else Statement
if(condition){statement(s);
}else{
statement(s);
}
for eg.
int a=10, b=20;
if( a == b ){
cout<<"a is equal to b";
}else{
cout<<"a is not equal to b";
}
Example for if_else Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if( a == b ){
cout<<"a is equal to b";
}else{
cout<<"a is not equal to b";
}
return 0;
}
Output :
a is not equal to b
3. else_if Statement
- else_if Statement में अगर if की Condition true होती है तो if का statement execute होता है | अगर if का condition false होता है तो वो अगले condition पर जाकर check करता है | अगर वो condition true होता है तो वो उसका statement execute करता है | अगर कोई भी condition true नहीं होती तो वो else का statement execute करता है |
Syntax for else_if Statement
if(condition){statement(s);
}else if(condition){
statement(s);
}else{
statement(s);
}
for eg.
int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}else if( a > b ){
cout<<"a is greater than b";
}else{
cout<<"a is equal to b";
}
Example for else_if Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int a=10, b=20;
if( a < b ){
cout<<"a is less than b";
}else if( a > b ){
cout<<"a is greater than b";
}else{
cout<<"a is equal to b";
}
return 0;
}
Output :
a is less than b
4. switch case Statement
- Switch case statement में expression होता है और उससे related कुछ cases होते है | जो case उस expression या declare किये हुए variable से match होती है तब वो output में print होता है | अगर कोई भी case expression से match नहीं होती तो वो default का statement output में print करेगा | आपको हर statement के बाद break लगाना पड़ता है, इसका मतलब वो उसके पहले का statement ही print करेगा | अगर आप break नहीं लगाते तो वो पहला और दूसरा ये दोनों statement को print करेगा | default case के बाद break नहीं लगाते |
Syntax for switch Statement
switch (expression){case value1 :
statement1 ;
break;
case value2 :
statement2 ;
break;
default :
statement3 ;
}
Example for switch Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
char Day='D';
switch(Day){
case 'A' :
cout<<"Today is Sunday";
break;
case 'B' :
cout<<"Today is Monday";
break;
case 'C' :
cout<<"Today is Tuesday";
break;
case 'D' :
case 'E' :
cout<<"Today is Wednesday";
break;
case 'F' :
cout<<"Today is Thurday";
break;
case 'G' :
cout<<"Today is Friday";
break;
case 'H' :
cout<<"Today is Saturday";
break;
default :
cout<<"Day is Not Found";
}
return 0;
}
Output :
Today is Wednesday
5. break Statement
- Break Statement Program के loops और switch case के execution का काम किसी condition पर बंद कर देता है |
Syntax for break Statement
break;Example for break Statement
Source Code :
#include <iostream.h>
using namespace std;
int main()
{
int i=0;
while ( i < 20 ){
cout<<"value of i is "<<i;
i++;
if ( i == 10)
break;
}
return 0;
}
Output :
value of i is 0 value of i is 1 value of i is 2 value of i is 3 value of i is 4 value of i is 5 value of i is 6 value of i is 7 value of i is 8 value of i is 9
6. continue Statement
- Continue Statement Program के loops के condition के हिसाब से बीचवाले statements को skip कर देता है और बादवाले statement को execute करता है |
Syntax for continue Statement
continue;Example for continue Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int i;
for(i=0;i<10;i++){
if(i==7){
cout<<"Number %d is skipped."<<i;
continue;
}
cout<<i<<endl;
}
return 0;
}
Output :
0 1 2 3 4 5 6 Number 7 is skipped. 8 9
7. goto Statement
Go to ये C++ Programming का statement है | इसमें labels का use किया जाता है |Goto Statement के दो प्रकार होते है |
- Forward
- Backward
Syntax for Forward and Backward goto Statement
Syntax for Forward
goto label ;statement ;
-----------
label ;
Syntax for Backward
label ;statement ;
-----------
goto label ;
Example for goto Statement
Source Code :
#include <iostream.h>
using namespace std;
int main(){
int num1, num2, num3;
char ch;
yes : cout<<"Enter two values"<<endl;
cin>>num1>>num2;
num3 = num1 + num2 ;
cout<<"Addition of "<<num1<<" and "<<num2<<" is "<<num3;
cout<<"\nDo you want to continue y(yes) or n(No)";
cin>>ch;
if(ch=='y'){
goto yes;
}
else if(ch=='n'){
goto no;
}
else{
cout<<"You entered other key";
}
no : cout<<"Do you want to exit ?, Press Enter";
return 0;
}
Output :
Enter two values 4 5 Addition of 4 and 5 is 9 Do you want to continue y(yes) or n(No)y Enter two values 6 4 Addition of 6 and 4 is 10 Do you want to continue y(yes) or n(No)n Do you want to exit ?, Press Enter







No comments: